Advertisement
saurav_kalsoor

Find Power - JAVA

Sep 26th, 2021
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.88 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.*;
  4.  
  5.  
  6. public class Problem {
  7.  
  8.     public static void main(String[] args){
  9.         Scanner sc = new Scanner(System.in);
  10.         int n = sc.nextInt();
  11.         int[] power = new int[n];
  12.  
  13.         for(int i=0; i < n; i++){
  14.             power[i] = sc.nextInt();
  15.         }
  16.         int result = findPower(power, n);
  17.         System.out.println(result);
  18.     }
  19.  
  20.  
  21.     public static int findPower(int[] power, int n) {
  22.         PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
  23.  
  24.         for(int a : power)
  25.             maxHeap.add(a);
  26.        
  27.         while (maxHeap.size() > 1){
  28.             int a = maxHeap.poll();
  29.             int b = maxHeap.poll();
  30.  
  31.             if(a == b)
  32.                 maxHeap.add(a);
  33.             else
  34.                 maxHeap.add((a+b)/4);
  35.         }
  36.         return maxHeap.poll();
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement