Find the indices which will hold the Array sum after given operations
Given an array arr[], the task is to print the indices that have the highest probability of holding the entire sum of the array after performing the given operations:
- Choose any two elements say arr[i] and arr[j], store their sum in a variable K
- Assign K at index of max(arr[i], arr[j]) and assign 0 at index of min(arr[i], arr[j])
In this way, after choosing all elements, the last element left will store the sum of the whole array. The task is to return the indices of the array in the sequence of the highest possibility of holding the sum of the array.
Examples:
Input: arr[] = {2, 1, 4}
Output: {2}
Explanation: Choosing of elements can be done in the following ways:Way 1:
- Choose elements at indices {0, 1}, so arr[] = {3, 0, 4}
- Choose elements at indices {0, 2}, so arr[] = {0, 0, 7}
Way 2:
- Choose elements at indices {0, 2}, so arr[] = {0, 1, 6}
- Choose elements at indices {1, 2}, so arr[] = {0, 0, 7}
Way 3:
- Choose elements at indices {1, 2}, so arr[] = {2, 0, 5}
- Choose elements at indices {0, 2}, so arr[] = {0, 0, 7}
In this case, output = {2}, index 0 and index 1 has zero possibility of holding the sum, so it is excluded.
Input: arr[] = {1, 2, 4, 3}
Output: {1, 2, 3}
Approach: The task can be solved by sorting the given array elements, and checking if the sum till i-1th is greater than the sum till ith element or not, if it is greater, then that index has a probability to be the valid index.
- Take a vector of pair say v, store all the elements along with the respective indices in pair, also take a variable say sum that will store the sum of the entire array.
- Sort the vector in ascending order and take a counter say c, initialize with 1 because one index will always have a possibility to hold the sum of the array.
- Iterate over the vector from the second last element and check if the sum till the second last element is greater than the sum till the end(i.e the sum of the array), if it is greater, then increment the counter which means it has a possibility of holding the sum, else break the loop as it is not a valid index.
- Take a vector say ans to store the valid indices and store the indices by iterating from the end till counter, then sort the ans vector and print it.
Below is the implementation of the above approach:
C++
// C++ implementation for the above approach #include <bits/stdc++.h> using namespace std; // Function to predict the indices which // will hold the sum of the given array void predictIndices( int arr[], int N) { // Variable for sum of the array int sum = 0; // Vector to store the elements along // with the indices in pair vector<pair< int , int > > v; for ( int i = 0; i < N; i++) { v.push_back({ arr[i], i }); sum += arr[i]; } // Sort the vector sort(v.begin(), v.end()); // Take a counter c, initialize // it with 1 int c = 1; // Iterate over the vector from the end // excluding the last element and each time // update sum by decrementing its value // by the element in the sorted vector for ( int i = N - 2; i >= 0; i--) { sum -= v[i + 1].first; // If element is greater than the sum // break the loop if (sum < v[i + 1].first) { break ; } // Else increment the counter c else { c++; } } // Vector to store all the indices // which can hold the sum vector< int > ans; // Iterate the ans vector in reverse order // till index is greater or equal to N - c // and store the indices in ans vector for ( int i = N - 1; i >= N - c; i--) { ans.push_back(v[i].second); } // Sort the ans vector sort(ans.begin(), ans.end()); // Print the desired indices for ( int i = 0; i < ans.size(); i++) { cout << ans[i] << " " ; } } // Driver Code int main() { int arr[] = { 1, 2, 4, 3 }; int N = sizeof (arr) / sizeof (arr[0]); predictIndices(arr, N); return 0; } |
Java
// Java implementation for the above approach import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; class GFG { public static class Pair { int first = 0 ; int second = 0 ; public Pair( int first, int second) { this .first = first; this .second = second; } } // Function to predict the indices which // will hold the sum of the given array public static void predictIndices( int arr[], int N) { // Variable for sum of the array int sum = 0 ; // Vector to store the elements along // with the indices in pair ArrayList<Pair> v = new ArrayList<Pair>(); for ( int i = 0 ; i < N; i++) { v.add( new Pair(arr[i], i)); sum += arr[i]; } // Sort the vector Collections.sort(v, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2){ return p1.first - p2.first; } }); // Take a counter c, initialize // it with 1 int c = 1 ; // Iterate over the vector from the end // excluding the last element and each time // update sum by decrementing its value // by the element in the sorted vector for ( int i = N - 2 ; i >= 0 ; i--) { sum -= v.get(i + 1 ).first; // If element is greater than the sum // break the loop if (sum < v.get(i + 1 ).first) { break ; } // Else increment the counter c else { c++; } } // Vector to store all the indices // which can hold the sum ArrayList<Integer> ans = new ArrayList<Integer>(); // Iterate the ans vector in reverse order // till index is greater or equal to N - c // and store the indices in ans vector for ( int i = N - 1 ; i >= N - c; i--) { ans.add(v.get(i).second); } // Sort the ans vector Collections.sort(ans); // Print the desired indices for ( int i = 0 ; i < ans.size(); i++) { System.out.print(ans.get(i) + " " ); } } // Driver Code public static void main(String args[]) { int arr[] = { 1 , 2 , 4 , 3 }; int N = arr.length; predictIndices(arr, N); } } // This code is contributed by saurabh_jaiswal. |
Python3
# python3 implementation for the above approach # Function to predict the indices which # will hold the sum of the given array def predictIndices(arr, N): # Variable for sum of the array sum = 0 # Vector to store the elements along # with the indices in pair v = [] for i in range (N): v.append([arr[i], i]) sum + = arr[i] # Sort the vector v.sort() # Take a counter c, initialize # it with 1 c = 1 # Iterate over the vector from the end # excluding the last element and each time # update sum by decrementing its value # by the element in the sorted vector for i in range (N - 2 , - 1 , - 1 ): sum - = v[i + 1 ][ 0 ] # If element is greater than the sum # break the loop if ( sum < v[i + 1 ][ 0 ]): break # Else increment the counter c else : c + = 1 # Vector to store all the indices # which can hold the sum ans = [] # Iterate the ans vector in reverse order # till index is greater or equal to N - c # and store the indices in ans vector for i in range (N - 1 , N - c - 1 , - 1 ): ans.append(v[i][ 1 ]) # Sort the ans vector ans.sort() # Print the desired indices for i in range ( len (ans)): print (ans[i], end = " " ) # Driver Code if __name__ = = "__main__" : arr = [ 1 , 2 , 4 , 3 ] N = len (arr) predictIndices(arr, N) # This code is contributed by ukasp. |
C#
// C# implementation for the above approach using System; using System.Collections.Generic; public class GFG { class Pair { public int first = 0; public int second = 0; public Pair( int first, int second) { this .first = first; this .second = second; } } // Function to predict the indices which // will hold the sum of the given array public static void predictIndices( int [] arr, int N) { // Variable for sum of the array int sum = 0; // Vector to store the elements along // with the indices in pair List<Pair> v = new List<Pair>(); for ( int i = 0; i < N; i++) { v.Add( new Pair(arr[i], i)); sum += arr[i]; } // Sort the vector v.Sort((Pair x, Pair y) => x.first.CompareTo(y.first)); // Take a counter c, initialize // it with 1 int c = 1; // Iterate over the vector from the end // excluding the last element and each time // update sum by decrementing its value // by the element in the sorted vector for ( int i = N - 2; i >= 0; i--) { sum -= v[i + 1].first; // If element is greater than the sum // break the loop if (sum < v[i + 1].first) { break ; } // Else increment the counter c else { c++; } } // Vector to store all the indices // which can hold the sum List< int > ans = new List< int >(); // Iterate the ans vector in reverse order // till index is greater or equal to N - c // and store the indices in ans vector for ( int i = N - 1; i >= N - c; i--) { ans.Add(v[i].second); } // Sort the ans vector ans.Sort(); // Print the desired indices for ( int i = 0; i < ans.Count; i++) { Console.Write(ans[i] + " " ); } } // Driver Code public static void Main() { int [] arr = { 1, 2, 4, 3 }; int N = arr.Length; predictIndices(arr, N); } } // This code is contributed by saurabh_jaiswal. |
Javascript
<script> // Javascript implementation for the above approach // Function to predict the indices which // will hold the sum of the given array function predictIndices(arr, N) { // Variable for sum of the array var sum = 0; // Vector to store the elements along // with the indices in pair var v = []; for ( var i = 0; i < N; i++) { v.push([arr[i], i ]); sum += arr[i]; } // Sort the vector v.sort(); // Take a counter c, initialize // it with 1 var c = 1; // Iterate over the vector from the end // excluding the last element and each time // update sum by decrementing its value // by the element in the sorted vector for ( var i = N - 2; i >= 0; i--) { sum -= v[i + 1][0]; // If element is greater than the sum // break the loop if (sum < v[i + 1][0]) { break ; } // Else increment the counter c else { c++; } } // Vector to store all the indices // which can hold the sum var ans = []; // Iterate the ans vector in reverse order // till index is greater or equal to N - c // and store the indices in ans vector for ( var i = N - 1; i >= N - c; i--) { ans.push(v[i][1]); } // Sort the ans vector ans.sort(); // Print the desired indices for ( var i = 0; i < ans.length; i++) { document.write(ans[i]+ " " ); } } // Driver Code var arr = [1, 2, 4, 3]; var N = arr.length; predictIndices(arr, N); // This code is contributed by rutvik_56. </script> |
1 2 3
Time Complexity: O(NlogN)
Auxiliary Space: O(N)
Please Login to comment...