Minimum number of array indices required to be selected to make the sum of a set greater than another
Last Updated :
09 Feb, 2023
Given two arrays arr[] and brr[] of size N and an integer K. Consider two sets A, contains K initially, and B, initially empty. In each operation, an index is required to be selected. For every selected index, say i, arr[i] and brr[i] is added to B. For every unselected index, arr[i] is added to A. The task is to find the minimum number of indices required to be selected to make sum of B greater than sum of A. If it is not possible to do so, then print -1.
Examples:
Input: arr[] = {3, 2, 5, 6}, brr[] = {4, 4, 2, 3}, K = 12
Output: 3
4 3 1
Explanation: Indices 2, 3 and 0 are selected. Sum of B = arr[0] + brr[0] + arr[2] + brr[2] + arr[3] + brr[3] = 3 + 4 + 5 + 2 + 6 + 3 = 23. Sum of A = K + arr[1] = 12 + 2 = 14.
Input: arr[] = {3, 2, 5, 6}, brr[] = {4, 4, 2, 3}, K = 34
Output: -1
Approach: The idea is to use a Greedy Approach. Follow the steps below to solve the problem:
- Initialize a vector of pair, B[] to keep track of indexes.
- Initialize a variable, A with K to store the value of set A.
- Traverse the array arr[] using the variable i,
- Add the value arr[i] to A, if the index was not chosen.
- Insert {brr[i] + 2 * arr[i], i} as a pair in vector B.
- Sort vector B in decreasing order.
- Initialize a vector, ans to store the indexes that are chosen.
- Run a while loop and keep on choosing the indices until A’s value is bigger than B.
- If all indexes are chosen but B’s value is still less than A, print “-1”.
- Otherwise, print the size of the vector ans as the minimum number of moves.
- Traverse the vector, ans, and print the chosen indices.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void numberOfIndexes( int arr[], int brr[],
int N, int K)
{
vector<pair< int , int > > B;
int A = K;
for ( int i = 0; i < N; i++) {
A += arr[i];
B.push_back({ brr[i] + 2 * arr[i], i });
}
sort(B.begin(), B.end());
reverse(B.begin(), B.end());
int tot = 0, idx = 0;
vector< int > ans;
while (A >= tot && idx < B.size()) {
tot += B[idx].first;
ans.push_back(B[idx].second);
idx++;
}
if (tot <= A) {
cout << -1 << endl;
return ;
}
cout << ans.size() << endl;
for ( auto c : ans)
cout << c + 1 << " " ;
}
int main()
{
int arr[] = { 3, 2, 5, 6 };
int brr[] = { 4, 4, 2, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 12;
numberOfIndexes(arr, brr, N, K);
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static void numberOfIndexes( int [] arr, int [] brr, int N, int K) {
List<Pair<Integer, Integer>> B = new ArrayList<>();
int A = K;
for ( int i = 0 ; i < N; i++) {
A += arr[i];
B.add( new Pair<>(brr[i] + 2 * arr[i], i));
}
B.sort((pair1, pair2) -> pair2.getKey() - pair1.getKey());
int tot = 0 , idx = 0 ;
List<Integer> ans = new ArrayList<>();
while (A >= tot && idx < B.size()) {
tot += B.get(idx).getKey();
ans.add(B.get(idx).getValue());
idx++;
}
if (tot <= A) {
System.out.println(- 1 );
return ;
}
System.out.println(ans.size());
for ( int c : ans) {
System.out.print((c + 1 ) + " " );
}
System.out.println();
}
public static void main(String[] args) {
int [] arr = { 3 , 2 , 5 , 6 };
int [] brr = { 4 , 4 , 2 , 3 };
int N = arr.length;
int K = 12 ;
numberOfIndexes(arr, brr, N, K);
}
}
class Pair<K, V> {
private K key;
private V value;
public Pair(K key, V value) {
this .key = key;
this .value = value;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
}
|
Python3
def numberOfIndexes(arr, brr, N, K):
B = []
A = K
for i in range (N):
A + = arr[i]
B.append([brr[i] + 2 * arr[i], i])
B.sort()
B = B[:: - 1 ]
tot = 0
idx = 0
ans = []
while (A > = tot and idx < len (B)):
tot + = B[idx][ 0 ]
ans.append(B[idx][ 1 ])
idx + = 1
if (tot < = A):
print ( - 1 )
return
print ( len (ans))
for c in ans:
print (c + 1 ,end = " " )
if __name__ = = '__main__' :
arr = [ 3 , 2 , 5 , 6 ]
brr = [ 4 , 4 , 2 , 3 ]
N = len (arr)
K = 12
numberOfIndexes(arr, brr, N, K)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void numberOfIndexes( int [] arr, int [] brr, int N, int K)
{
List<Tuple< int , int >> B = new List<Tuple< int , int >>();
int A = K;
for ( int i = 0; i < N; i++)
{
A += arr[i];
B.Add(Tuple.Create(brr[i] + 2 * arr[i], i));
}
B.Sort((pair1, pair2) => pair2.Item1 - pair1.Item1);
int tot = 0, idx = 0;
List< int > ans = new List< int >();
while (A >= tot && idx < B.Count)
{
tot += B[idx].Item1;
ans.Add(B[idx].Item2);
idx++;
}
if (tot <= A)
{
Console.WriteLine(-1);
return ;
}
Console.WriteLine(ans.Count);
foreach ( int c in ans)
{
Console.Write((c + 1) + " " );
}
Console.WriteLine();
}
static void Main( string [] args)
{
int [] arr = { 3, 2, 5, 6 };
int [] brr = { 4, 4, 2, 3 };
int N = arr.Length;
int K = 12;
numberOfIndexes(arr, brr, N, K);
}
}
|
Javascript
<script>
function numberOfIndexes(arr, brr, N, K) {
let B = [];
let A = K;
for (let i = 0; i < N; i++) {
A += arr[i];
B.push([brr[i] + 2 * arr[i], i]);
}
B.sort((a, b) => a[0] - b[0]).reverse();
let tot = 0,
idx = 0;
let ans = [];
while (A >= tot && idx < B.length) {
tot += B[idx][0];
ans.push(B[idx][1]);
idx++;
}
if (tot <= A) {
document.write(-1 + "<br>" );
return ;
}
document.write(ans.length + "<br>" );
for (let c of ans) document.write(Number(c) + 1 + " " );
}
let arr = [3, 2, 5, 6];
let brr = [4, 4, 2, 3];
let N = arr.length;
let K = 12;
numberOfIndexes(arr, brr, N, K);
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...