Modify array to another given array by replacing array elements with the sum of the array | Set-2
Last Updated :
09 Jun, 2022
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++
#include <bits/stdc++.h>
using namespace std;
bool createTarget(vector< int >& target)
{
int n = target.size();
int sum = 0;
int lastSum;
priority_queue< int > maxHeap(target.begin(),
target.end());
for ( int i = 0; i < n; i++) {
sum = sum + target[i];
}
while ( true ) {
lastSum = maxHeap.top();
maxHeap.pop();
sum = sum - lastSum;
if (lastSum == 1 || sum == 1) {
return true ;
}
if (lastSum < sum || sum == 0
|| lastSum - sum == 0) {
return false ;
}
lastSum = lastSum - sum;
sum = sum + lastSum;
maxHeap.push(lastSum);
}
}
int main()
{
int N = 2;
vector< int > target = { 2, 3 };
bool ans = createTarget(target);
if (ans)
cout << "YES" ;
else
cout << "NO" ;
return 0;
}
|
Java
import java.util.*;
class GFG {
static boolean createTarget( int [] target)
{
int n = target.length;
int sum = 0 ;
int lastSum = 0 ;
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>();
for ( int i = 0 ; i < target.length; i++)
maxHeap.add(- 1 * target[i]);
for ( int i = 0 ; i < n; i++)
sum += target[i];
while ( true )
{
lastSum = - 1 * maxHeap.poll();
sum -= lastSum;
if (lastSum == 1 || sum == 1 )
{
return true ;
}
if (lastSum <= sum || sum == 0 )
{
return false ;
}
lastSum = lastSum - sum;
sum += lastSum;
maxHeap.add(- 1 * lastSum);
}
}
public static void main(String[] args) {
int N = 2 ;
int [] target = { 2 , 3 };
boolean ans = createTarget(target);
if (ans)
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
|
Python3
from queue import PriorityQueue
def createTarget(target):
n = len (target)
sum = 0
lastSum = 0
maxHeap = PriorityQueue()
for itm in target:
maxHeap.put( - itm)
for i in range ( 0 , n):
sum = sum + target[i]
while True :
lastSum = - maxHeap.get()
sum = sum - lastSum
if (lastSum = = 1 or sum = = 1 ):
return True
if (lastSum < sum or sum = = 0
or lastSum - sum = = 0 ):
return False
lastSum = lastSum - sum
sum = sum + lastSum
maxHeap.put( - lastSum)
if __name__ = = "__main__" :
N = 2
target = [ 2 , 3 ]
ans = createTarget(target)
if (ans):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static bool createTarget( int [] target)
{
int n = target.Length;
int sum = 0;
int lastSum = 0;
List< int > maxHeap = new List< int >();
for ( int i = 0; i < target.Length; i++)
maxHeap.Add(-1 * target[i]);
for ( int i = 0; i < n; i++)
sum += target[i];
while ( true )
{
lastSum = maxHeap[0];
maxHeap.RemoveAt(0);
sum -= lastSum;
if (lastSum == 1 || sum == 1)
{
return true ;
}
if (lastSum <= sum || sum == 0 )
{
return false ;
}
lastSum = lastSum - sum;
sum += lastSum;
maxHeap.Add(-1 * lastSum);
}
}
public static void Main()
{
int N = 2;
int [] target = {2, 3};
bool ans = createTarget(target);
if (ans == false )
Console.Write( "YES" );
else
Console.Write( "NO" );
}
}
|
Javascript
function createTarget(target)
{
var n = target.length;
var sum = 0;
var lastSum = 0;
maxHeap = [];
for ( var i = 0; i < target.length; i++)
maxHeap.push(-1 * target[i]);
for ( var i = 0; i < n; i++)
sum += target[i];
while ( true ) {
lastSum = maxHeap[0];
maxHeap.splice(0, 1);
sum -= lastSum;
if (lastSum == 1 || sum == 1) {
return true ;
}
if (lastSum <= sum || sum == 0) {
return false ;
}
lastSum = lastSum - sum;
sum += lastSum;
maxHeap.pushh(-1 * lastSum);
}
}
var N = 2;
var target = [ 2, 3 ];
var ans = createTarget(target);
if (ans == false )
console.log( "YES" );
else
console.log( "NO" );
|
Time Complexity: O(N * log(N) + (K / N * log(N))), where K is the maximum element of the array.
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...