Modify array to another given array by replacing array elements with the sum of the array | Set-2
Given an Array input[] consisting only of 1s initially and an array target[] of size N, the task is to check if the array input[] can be converted to target[] by replacing input[i] with the sum of array elements in each step.
Examples:
Input: input[] = { 1, 1, 1 }, target[] = { 9, 3, 5 }
Output: YES
Explanation:
Replacing input[1] with (input[0] + input[1] + input[2]) modifies input[] to { 1, 3, 1 }
Replacing input[2] with (input[0] + input[1] + input[2]) modifies input[] to { 1, 3, 5 }
Replacing input[0] with (input[0] + input[1] + input[2]) modifies input[] to { 9, 3, 5 }
Since the array input[] equal to the target[] array, the required output is “YES”.Input: input[] = { 1, 1, 1, 1 }, target[] = { 1, 1, 1, 2 }
Output: NO
Naive Approach: The naive approach and the Greedy approach are already mentioned in Set-1 of this article.
Efficient Approach: The idea to solve the problem efficiently is based on the below intuition:
- Instead of trying to check if target[] array can be reached, work backward and try to generate the array of 1s from the target[].
- While working backward the maximum element of the array will be the sum of elements after the last turn. To keep track of the maximum element, use a max heap.
- After every turn, remove the maximum element from the heap and determine the previous maximum element value. To do this find the sum of all elements of the array.
Follow the steps to solve the problem:
- Create variables sum and lastSum to store the sum of all elements the sum of the array on previous step.
- To determine the previous element, find the difference of “sum” and “lastSum” from “lastSum”, i.e (lastSum – (sum – lastSum)).
- Then put this value back to the heap and update the sum.
- Continue this until either the sum is equal to one or the lastSum is equal to one.
- In case, lastSum becomes less than sum or sum becomes equal to zero or the difference of lastSum and sum becomes zero, return false.
Below is the implementation of the above approach :
C++
// C++ code for the above approach: #include <bits/stdc++.h>using namespace std; // Function to find if target[] can be reachedbool createTarget(vector<int>& target){ // Initialise size of target array int n = target.size(); // Initialise variable to store // sum of values int sum = 0; // Initialise variable to store // last sum int lastSum; // Initialise a max-heap to keep track // of the maximum element priority_queue<int> maxHeap(target.begin(), target.end()); // Start traversing to find the sum for (int i = 0; i < n; i++) { sum = sum + target[i]; } // While heap has element traverse while (true) { // Update last sum with // maximum value of heap lastSum = maxHeap.top(); // Pop the maximum element // of the heap maxHeap.pop(); // Update sum of values sum = sum - lastSum; // If either sum or last sum is // equal to 1, then // target array possible if (lastSum == 1 || sum == 1) { // Return true return true; } // If last sum becomes less than // sum or if sum becomes equal // to 0 or if difference of last // sum and sum becomes 0 if (lastSum < sum || sum == 0 || lastSum - sum == 0) { // Return false return false; } // Update last sum lastSum = lastSum - sum; // Update sum sum = sum + lastSum; // Push last sum into the queue maxHeap.push(lastSum); }} // Driver codeint main(){ int N = 2; vector<int> target = { 2, 3 }; bool ans = createTarget(target); if (ans) cout << "YES"; else cout << "NO"; return 0;} |
true
Time Complexity: O(N * log(N) + (K / N * log(N))), where K is the maximum element of the array.
Auxiliary Space: O(N)


