Skip to content
Related Articles

Related Articles

Modify array to another given array by replacing array elements with the sum of the array
  • Last Updated : 23 Feb, 2021
GeeksforGeeks - Summer Carnival Banner

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. If found to be true, then print “YES”. Otherwise, print “NO”.

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

Approach: The problem can be solved using Greedy technique. The idea is to always decrement the largest element of target[] array by the sum of the remaining array elements and check if the largest element of the target[]. If found to be true then print “YES”. Otherwise, print “NO”. Following are the observations:



If target[] = { 9, 3, 5 } and input[] = { 1, 1, 1 } 
Decrementing target[0] by (target[1] + target[2]) modifies target[] to { 1, 3, 5 } 
Decrementing target[2] by (target[0] + target[1]) modifies target[] to { 1, 3, 1 } 
Decrementing target[1] by (target[0] + target[2]) modifies target[] to { 1, 1, 1 } 
Since input[] array and target[] equal, the required output is YES. 
 

  • If the largest element in the array target[] is less than 1, then print “NO”.
  • If the largest element in the array target[] is equal to 1, then print “YES”.
  • Otherwise, decrement the largest element in the array target[] by the sum of remaining elements present in the array target[] while the largest element of the array is greater than 1.

Below is the implementation of the above approach:

C++




// CPP program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if the arr[] can be
// converted to target[] by replacing
// any element in arr[] by the sum of arr[]
bool isPossible(int target[], int n)
{
 
  // Store the maximum element
  int max = 0;
 
  // Store the index of
  // the maximum element
  int index = 0;
 
  // Traverse the array target[]
  for (int i = 0; i < n; i++) {
 
    // If current element is
    // greater than max
    if (max < target[i]) {
      max = target[i];
      index = i;
    }
  }
 
  // If max element is 1
  if (max == 1)
    return true;
 
  // Traverse the array, target[]
  for (int i = 0; i < n; i++) {
 
    // If current index is not equal to
    // maximum element index
    if (i != index) {
 
      // Update max
      max -= target[i];
 
      // If max is less than
      // or equal to 0,
      if (max <= 0)
        return false;
    }
  }
 
  // Update the maximum element
  target[index] = max;
 
  // Recursively call the function
  return isPossible(target,n);
}
 
// Driver Code
int main()
{
  int target[] = { 9, 3, 5 };
   
  // Size of the array
   int n = sizeof(target) / sizeof(target[0]);
 
  bool res = isPossible(target,n);
 
  if (res)
  {
 
    cout << "YES";
  }
  else
  {
    cout << "NO";
  }
  return 0;
}
 
// This code is contributed by 29AjayKumar

Java




// Java program to implement
// the above approach
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to check if the arr[] can be
    // converted to target[] by replacing
    // any element in arr[] by the sum of arr[]
    public static boolean isPossible(int[] target)
    {
 
        // Store the maximum element
        int max = 0;
 
        // Store the index of
        // the maximum element
        int index = 0;
 
        // Traverse the array target[]
        for (int i = 0; i < target.length; i++) {
 
            // If current element is
            // greater than max
            if (max < target[i]) {
                max = target[i];
                index = i;
            }
        }
 
        // If max element is 1
        if (max == 1)
            return true;
 
        // Traverse the array, target[]
        for (int i = 0; i < target.length; i++) {
 
            // If current index is not equal to
            // maximum element index
            if (i != index) {
 
                // Update max
                max -= target[i];
 
                // If max is less than
                // or equal to 0,
                if (max <= 0)
                    return false;
            }
        }
 
        // Update the maximum element
        target[index] = max;
 
        // Recursively call the function
        return isPossible(target);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] target = { 9, 3, 5 };
 
        boolean res = isPossible(target);
 
        if (res) {
 
            System.out.println("YES");
        }
        else {
            System.out.println("NO");
        }
    }
}

Python3




# Python program to implement the above approach
 
# Function to check if the arr[] can be
# converted to target[] by replacing
# any element in arr[] by the sum of arr[]
def isPossible(target):
   
  # Store the maximum element
  max = 0
 
  # Store the index of
  # the maximum element
  index = 0
 
  # Traverse the array target[]
  for i in range(len(target)):
     
    # If current element is
    # greater than max
    if (max < target[i]):
      max = target[i]
      index = i
 
  # If max element is 1
  if (max == 1):
    return True
 
  # Traverse the array, target[]
  for i in range(len(target)):
     
    # If current index is not equal to
    # maximum element index
    if (i != index):
       
      # Update max
      max -= target[i]
       
      # If max is less than
      # or equal to 0,
      if (max <= 0):
        return False
       
  # Update the maximum element
  target[index] = max
 
  # Recursively call the function
  return isPossible(target)
 
# Driver Code
target = [ 9, 3, 5 ]
res = isPossible(target)
if (res):
  print("YES")
else:
  print("NO")
 
  # This code is contributed by RohitSingh07052.

C#




// C# program for the above approach
using System;
class GFG
{
 
    // Function to check if the arr[] can be
    // converted to target[] by replacing
    // any element in arr[] by the sum of arr[]
    public static bool isPossible(int[] target)
    {
 
        // Store the maximum element
        int max = 0;
 
        // Store the index of
        // the maximum element
        int index = 0;
 
        // Traverse the array target[]
        for (int i = 0; i < target.Length; i++) {
 
            // If current element is
            // greater than max
            if (max < target[i])
            {
                max = target[i];
                index = i;
            }
        }
 
        // If max element is 1
        if (max == 1)
            return true;
 
        // Traverse the array, target[]
        for (int i = 0; i < target.Length; i++) {
 
            // If current index is not equal to
            // maximum element index
            if (i != index) {
 
                // Update max
                max -= target[i];
 
                // If max is less than
                // or equal to 0,
                if (max <= 0)
                    return false;
            }
        }
 
        // Update the maximum element
        target[index] = max;
 
        // Recursively call the function
        return isPossible(target);
    }
 
   
// Driver Code
static public void Main()
{
        int[] target = { 9, 3, 5 };
 
        bool res = isPossible(target);
 
        if (res)
        {
            Console.WriteLine("YES");
        }
        else
        {
            Console.WriteLine("NO");
        }
    }
}
 
// This code is contributed by jana_sayantan.
Output: 
YES

 

Time Complexity: O(N2)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :