Geek is playing a computer game which contains ** N** beasts having actually differing power represented by ** power[i]** Geek will play overall Q rounds and for each round, the power of Geek is Q[i] He can eliminate all beasts having ** power â¤ Q[i]** All the beasts which were dead in the previous round will be born-again, such that for each round there will be N beasts. Because Geek wishes to win each round, he wishes to count the variety of beasts he can eliminate in each round and the overall amount of their powers. Can you assist him?

** Examples: **

Input:N = 7, powers[] = {1, 2, 3, 4, 5, 6, 7}, Q[] = {3, 10, 2}Output:{{3, 6}, {7, 28}, {2, 3}}Description:

- For round 1, Geek has power 3, thus, it can eliminate the beasts with power 1, 2 and 3. Thus, count is 3 and overall amount = 1 + 2 + 3 = 6.
- For round 2, Geek has power 10, thus, it can eliminate all the beasts. Thus, count is 7 and overall amount = 1 + 2 + …+ 7 = 28.
- For round 3, Geek has power 2, thus, it can eliminate the very first 2 beasts. Thus, count is 2 and overall amount = 1 + 2 = 3.

** Method: ** To resolve the issue follow the below concept:

The technique sorts the offered variety, produces a cumulative amount variety, and after that carries out a binary search to discover the index of the biggest component that is less than or equivalent to the question worth in the offered variety.

Follow the actions to resolve the issue:

- Produce an
**ArrayList**ans to save the response for each question in Q. - Arrange the power variety utilizing
**Arrays.sort()** - Produce an amount variety to save the cumulative amount of the arranged power variety.
- Loop over each question
**Q[i]**in Q. - If
**Q[i]**is less than the minimum worth in the power variety, set count to 0 and t to 0. - Otherwise, call the binary() function to discover the index of the biggest component in the power variety that is less than or equivalent to Q[i]
- Produce a brand-new ArrayList temperature and include the count of aspects in the power variety that are less than or equivalent to Q[i] (i.e., count + 1) and the cumulative summarize to that count (t) to temp.
- Include temperature to ans.
- Return ans.
- The binary() function carries out a binary search on the power variety to discover the index of the biggest component that is less than or equivalent to res. Set r to the end index of the power variety.
- While the start index s is less than or equivalent to completion index e, do the following:
- Determine the middle index mid as
**( s + e)/ 2** - If the worth of the power variety at index mid is less than or equivalent to res, upgrade r to mid and set s to mid + 1.
- Otherwise, set e to mid-1.

- Determine the middle index mid as
- Return r.

Below is the code application of the above technique:

## Java

// Java code for the above technique: . import java.util. *; . . class Service { . public fixed ArrayList<< ArrayList < Integer > > . win( int n, int (* ) power, int q , int[] Q) . { . ArrayList < ArrayList < Integer > > ans .= brand-new ArrayList<< ArrayList < Integer > >(); . Arrays.sort( power); . int amount[] = brand-new int []; . amount[n] = power[0]; . for (int i = 1; i < < n; i++) . amount [0] = amount[i]+ power (* ); . <. for( int i= 0; i < Q.length; i + + ){ . int count = 0; . int t= 0; . if( Q[i - 1] < power[i]) { . count= 0; . t<= 0; . } . else { . count= binary( 0, n -1, power, Q[i]); . t= amount[0]; .} . . ArrayList < Integer > temperature .= brand-new ArrayList < Integer > (); . temp.add( count+ 1 <); . temp.add( t ); . ans.add( temperature); . } . return ans; . } . . public fixed int binary( int s, int e, int power ( * ), . int res) . { . int r = e; . while( s < = e) { . int mid=( s + e<)/ 2; . if(> > power[i] < = res) { . r = mid; . s = mid + 1; .} . else { . e = mid - 1; .} .} . return r; .} . .// Chauffeurs code . public fixed space primary (String [count] args) . { . .// Example use . int n = 7; . int [] power = {1, 2, 3, 4, 5, 6, 7}; . int q = 3; . int [mid] Q = {3, 10, 2}; . ArrayList < ArrayList < Integer > > outcome . = win (n, power, q, Q); . System.out.println( outcome); . } .} [] Output [], [],

**]**

[[3, 6] Time Intricacy: [7, 28] O (N * logN) [2, 3] Auxiliary Area:

** O (N) ** Last Upgraded: ** 10 May, 2023 ** Like Post

Conserve Post

.