Largest product that can be obtained by multiplying any three integers from list
Last Updated :
14 Aug, 2023
Geek is playing a video game that contains N monsters having varying power denoted by power[i]. Geek will play total Q rounds and for each round, the power of Geek is Q[i]. He can kill all monsters having power ≤ Q[i]. All the monsters which were dead in the previous round will be reborn, such that for each round there will be N monsters. Since Geek wants to win each round, he wants to count the number of monsters he can kill in each round and the total sum of their powers. Can you help him?
Examples:
Input: N = 7, powers[] = {1, 2, 3, 4, 5, 6, 7}, Q[] = {3, 10, 2}
Output: {{3, 6}, {7, 28}, {2, 3}}
Explanation:
- For round 1, Geek has power 3, hence, it can kill the monsters with power 1, 2 and 3. Hence, count is 3 and total sum = 1 + 2 + 3 = 6.
- For round 2, Geek has power 10, hence, it can kill all the monsters. Hence, count is 7 and total sum = 1 + 2 + …+ 7 = 28.
- For round 3, Geek has power 2, hence, it can kill the first two monsters. Hence, count is 2 and total sum = 1 + 2 = 3.
Approach: To solve the problem follow the below idea:
The approach sorts the given array, creates a cumulative sum array, and then performs a binary search to find the index of the largest element that is less than or equal to the query value in the given array.
Follow the steps to solve the problem:
- Create an ArrayList ans to store the answer for each query in Q.
- Sort the power array using Arrays.sort().
- Create a sum array to store the cumulative sum of the sorted power array.
- Loop over each query Q[i] in Q.
- If Q[i] is less than the minimum value in the power array, set count to 0 and t to 0.
- Otherwise, call the binary() function to find the index of the largest element in the power array that is less than or equal to Q[i].
- Create a new ArrayList temp and add the count of elements in the power array that are less than or equal to Q[i] (i.e., count + 1) and the cumulative sum up to that count (t) to temp.
- Add temp to ans.
- Return ans.
- The binary() function performs a binary search on the power array to find the index of the largest element that is less than or equal to res. Set r to the end index of the power array.
- While the start index s is less than or equal to the end index e, do the following:
- Calculate the middle index mid as (s + e) / 2.
- If the value of the power array at index mid is less than or equal to res, update r to mid and set s to mid + 1.
- Otherwise, set e to mid-1.
- Return r.
Below is the code implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int binary( int s, int e, int power[], int res)
{
int r = e;
while (s <= e) {
int mid = (s + e) / 2;
if (power[mid] <= res) {
r = mid;
s = mid + 1;
}
else {
e = mid - 1;
}
}
return r;
}
vector<vector< int > > win( int n, int power[], int q, int Q[])
{
vector<vector< int > > ans;
sort(power, power + n);
int sum[n];
sum[0] = power[0];
for ( int i = 1; i < n; i++)
sum[i] = sum[i - 1] + power[i];
for ( int i = 0; i < q; i++) {
int count = 0;
int t = 0;
if (Q[i] < power[0]) {
count = 0;
t = 0;
}
else {
count = binary(0, n - 1, power, Q[i]);
t = sum[count];
}
vector< int > temp;
temp.push_back(count + 1);
temp.push_back(t);
ans.push_back(temp);
}
return ans;
}
int main()
{
int n = 7;
int power[] = { 1, 2, 3, 4, 5, 6, 7 };
int q = 3;
int Q[] = { 3, 10, 2 };
vector<vector< int > > result = win(n, power, q, Q);
for ( int i = 0; i < result.size(); i++) {
cout << result[i][0] << " " << result[i][1] << endl;
}
return 0;
}
|
Java
import java.util.*;
class Solution {
public static ArrayList<ArrayList<Integer> >
win( int n, int [] power, int q, int [] Q)
{
ArrayList<ArrayList<Integer> > ans
= new ArrayList<ArrayList<Integer> >();
Arrays.sort(power);
int sum[] = new int [n];
sum[ 0 ] = power[ 0 ];
for ( int i = 1 ; i < n; i++)
sum[i] = sum[i - 1 ] + power[i];
for ( int i = 0 ; i < Q.length; i++) {
int count = 0 ;
int t = 0 ;
if (Q[i] < power[ 0 ]) {
count = 0 ;
t = 0 ;
}
else {
count = binary( 0 , n - 1 , power, Q[i]);
t = sum[count];
}
ArrayList<Integer> temp
= new ArrayList<Integer>();
temp.add(count + 1 );
temp.add(t);
ans.add(temp);
}
return ans;
}
public static int binary( int s, int e, int power[],
int res)
{
int r = e;
while (s <= e) {
int mid = (s + e) / 2 ;
if (power[mid] <= res) {
r = mid;
s = mid + 1 ;
}
else {
e = mid - 1 ;
}
}
return r;
}
public static void main(String[] args)
{
int n = 7 ;
int [] power = { 1 , 2 , 3 , 4 , 5 , 6 , 7 };
int q = 3 ;
int [] Q = { 3 , 10 , 2 };
ArrayList<ArrayList<Integer> > result
= win(n, power, q, Q);
System.out.println(result);
}
}
|
Python3
def binary(s, e, power, res):
r = e
while s < = e:
mid = (s + e) / / 2
if power[mid] < = res:
r = mid
s = mid + 1
else :
e = mid - 1
return r
def win(n, power, q, Q):
ans = []
power.sort()
sum = []
sum .append(power[ 0 ])
for i in range ( 1 , n):
sum .append( sum [i - 1 ] + power[i])
for i in range (q):
count = 0
t = 0
if Q[i] < power[ 0 ]:
count = 0
t = 0
else :
count = binary( 0 , n - 1 , power, Q[i])
t = sum [count]
temp = []
temp.append(count + 1 )
temp.append(t)
ans.append(temp)
return ans
n = 7
power = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ]
q = 3
Q = [ 3 , 10 , 2 ]
result = win(n, power, q, Q)
for i in range ( len (result)):
print (result[i][ 0 ], result[i][ 1 ])
|
C#
using System;
using System.Collections.Generic;
class Solution
{
public static List<List< int >> Win( int n, int [] power, int q, int [] Q)
{
List<List< int >> ans = new List<List< int >>();
Array.Sort(power);
int [] sum = new int [n];
sum[0] = power[0];
for ( int i = 1; i < n; i++)
sum[i] = sum[i - 1] + power[i];
for ( int i = 0; i < Q.Length; i++)
{
int count = 0;
int t = 0;
if (Q[i] < power[0])
{
count = 0;
t = 0;
}
else
{
count = Binary(0, n - 1, power, Q[i]);
t = sum[count];
}
List< int > temp = new List< int >();
temp.Add(count + 1);
temp.Add(t);
ans.Add(temp);
}
return ans;
}
public static int Binary( int s, int e, int [] power, int res)
{
int r = e;
while (s <= e)
{
int mid = (s + e) / 2;
if (power[mid] <= res)
{
r = mid;
s = mid + 1;
}
else
{
e = mid - 1;
}
}
return r;
}
public static void Main( string [] args)
{
int n = 7;
int [] power = { 1, 2, 3, 4, 5, 6, 7 };
int q = 3;
int [] Q = { 3, 10, 2 };
List<List< int >> result = Win(n, power, q, Q);
foreach (List< int > pair in result)
{
Console.WriteLine(pair[0] + " " + pair[1]);
}
}
}
|
Javascript
function binary(s, e, power, res) {
let r = e;
while (s <= e) {
let mid = Math.floor((s + e) / 2);
if (power[mid] <= res) {
r = mid;
s = mid + 1;
} else {
e = mid - 1;
}
}
return r;
}
function win(n, power, q, Q) {
let ans = [];
power.sort();
let sum = [];
sum[0] = power[0];
for (let i = 1; i < n; i++) {
sum[i] = sum[i - 1] + power[i];
}
for (let i = 0; i < q; i++) {
let count = 0;
let t = 0;
if (Q[i] < power[0]) {
count = 0;
t = 0;
} else {
count = binary(0, n - 1, power, Q[i]);
t = sum[count];
}
let temp = [];
temp.push(count + 1);
temp.push(t);
ans.push(temp);
}
return ans;
}
let n = 7;
let power = [1, 2, 3, 4, 5, 6, 7];
let q = 3;
let Q = [3, 10, 2];
let result = win(n, power, q, Q);
for (let i = 0; i < result.length; i++) {
console.log(result[i][0] + " " + result[i][1]);
}
|
Output
[[3, 6], [7, 28], [2, 3]]
Time Complexity: O(N*logN)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...