- Arrays, Greedy, Sorting

Find all possible unique indices after reducing the array based on given conditions

Given an array arr of N integers, and an array of keys, such that key for each element in arr is equal to its index in arr initially. Given that one can perform certain moves where in one move, choose two indices (i, j), remove corresponding elements from the array, and add a new element equal to sum of the removed elements (arr[i] + arr[j]), with a key equal to the key of larger element among arr[i] and arr[j]. If both elements are equal, then any key out of the two can be used. The task is to print all possible unique keys at the end, when the array contains only a single element.Examples: Input: N = 3, arr[] = {2, 3, 4}Output: 1, 2Explanation: Initially all elements have keys equal to their index. (2->0, 3->1, 4->2).Case 1:Remove 2 and 3 and insert 5 with key 1. The new array will be (5->1, 4->2)Remove 5 and 4 and insert 9 with key 1. The new array will be (9->1)The final index in the array will be 1 for these operations.Case 2:Remove 2 and 4 and insert 6 with key 2. The new array will be (3->1, 6->2)Remove 6 and 3 and insert 9 with key 2. The new array will be (9->2)The final index in the array will be 2 for these operations.So there are a total of 2 possible unique keys (1 and 2).Input: N = 3, arr[] = {1, 1, 4}Output: 2Explanation: The final index left will be 2 in all cases. So there is a total of 1 possible index.Approach: Maintain a vector of pairs which will be storing {value, i} for each arr[i] and a variable sum which will be storing the remaining sum of the left part. We sort the vector in non-decreasing order of value and start traversing from the end. Whenever the remaining sum of the left part of the array is less than the value of the current element that means all the elements in the left part cannot be the answer ever so we break the loop from here, because those elements on the left part will be at the end replaced by the keys of larger elements on the right. Follow the below steps as the algorithm for the above approach:Maintain a pair vector that stores the elements of the array in the form of {value, index}Sort the pair vector in non-decreasing order of value.Take a variable sum which will be initially storing the sum of all the elements in the array.Iterate from right to left in the sorted vector.If the value of the current element is greater than the sum of all elements in its left then break the loop, otherwise, add the current index as a valid answer and decrease the sum by the value of the current element and continue for the remaining elements.At the end return all possible indices in the answer.Below is the implementation of the above approach: C++#include using namespace std;  int totalFinalIndexes(int A[], int N){        int res = 0;          int sum = 0;          vector ans;              vector elements;    for (int i = 0; i < N; i++) {        sum += A[i];        elements.push_back(make_pair(A[i], i));    }      sort(elements.begin(), elements.end());              for (int i = N - 1; i >= 0; i–) {                res++;                  sum -= elements[i].first;                          ans.push_back(elements[i].second);                          if (sum < elements[i].first)            break;    }          for (auto x : ans) {        cout