Modify array to another given array by replacing array elements with the sum of the array
Last Updated :
24 Jan, 2023
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++
#include <bits/stdc++.h>
using namespace std;
bool isPossible( int target[], int n)
{
int max = 0;
int index = 0;
for ( int i = 0; i < n; i++) {
if (max < target[i]) {
max = target[i];
index = i;
}
}
if (max == 1)
return true ;
for ( int i = 0; i < n; i++) {
if (i != index) {
max -= target[i];
if (max <= 0)
return false ;
}
}
target[index] = max;
return isPossible(target,n);
}
int main()
{
int target[] = { 9, 3, 5 };
int n = sizeof (target) / sizeof (target[0]);
bool res = isPossible(target,n);
if (res)
{
cout << "YES" ;
}
else
{
cout << "NO" ;
}
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static boolean isPossible( int [] target)
{
int max = 0 ;
int index = 0 ;
for ( int i = 0 ; i < target.length; i++) {
if (max < target[i]) {
max = target[i];
index = i;
}
}
if (max == 1 )
return true ;
for ( int i = 0 ; i < target.length; i++) {
if (i != index) {
max -= target[i];
if (max <= 0 )
return false ;
}
}
target[index] = max;
return isPossible(target);
}
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
def isPossible(target):
max = 0
index = 0
for i in range ( len (target)):
if ( max < target[i]):
max = target[i]
index = i
if ( max = = 1 ):
return True
for i in range ( len (target)):
if (i ! = index):
max - = target[i]
if ( max < = 0 ):
return False
target[index] = max
return isPossible(target)
target = [ 9 , 3 , 5 ]
res = isPossible(target)
if (res):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG
{
public static bool isPossible( int [] target)
{
int max = 0;
int index = 0;
for ( int i = 0; i < target.Length; i++) {
if (max < target[i])
{
max = target[i];
index = i;
}
}
if (max == 1)
return true ;
for ( int i = 0; i < target.Length; i++) {
if (i != index) {
max -= target[i];
if (max <= 0)
return false ;
}
}
target[index] = max;
return isPossible(target);
}
static public void Main()
{
int [] target = { 9, 3, 5 };
bool res = isPossible(target);
if (res)
{
Console.WriteLine( "YES" );
}
else
{
Console.WriteLine( "NO" );
}
}
}
|
Javascript
<script>
function isPossible(target)
{
var max = 0;
var index = 0;
for (i = 0; i < target.length; i++) {
if (max < target[i]) {
max = target[i];
index = i;
}
}
if (max == 1)
return true ;
for (i = 0; i < target.length; i++) {
if (i != index) {
max -= target[i];
if (max <= 0)
return false ;
}
}
target[index] = max;
return isPossible(target);
}
var target = [ 9, 3, 5 ];
res = isPossible(target);
if (res) {
document.write( "YES" );
}
else {
document.write( "NO" );
}
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: This type of problem are purely intuition based and can be solved easily just by doing some observations …
Notice that we are getting a pattern in the target array if we make it sorted.Take a look at examples
Examples:
1: input[] = { 1, 1, 1 }, target[] = { 9, 3, 5 }
sorted(target[])={3,5,9} and the output will be for this array is YES
2: input[] = { 1, 1, 1,1 }, target[] = { 4,,25,7,13}
sorted(target[])={4,7,13,25} and the output will be for this array is YES
3: input[]={1,1,1,1,1} ,target[]={33,65,5,9,17}
sorted(target[])={5,9,17,33,65} and the output will be for this array is YES
From the above mentioned examples, it is clear that for size(target[])=n, sorted(target[]) should start from n and successive elements of the target[] array will be as : target[i]=2*target[i-1]-1 and so on.
if sorted(target) array follows this pattern, then result will be YES ,else NO….
Approach Steps:
- Sort the target[] array first.
- Store length of target[] array into n.
- if sorted(target[]) does not start from n, then return “NO”.
- Traverse sorted(target[]) from index 1 to last.
- If target[i] != 2* target[ i-1]-1 at any step,then return “NO”, else after completing traversal return “YES”………….improved by Rajat Kumar.
Below is the implementation of the above approach:
Java
import java.io.*;
import java.util.*;
class GFG {
public static boolean isPossible( int [] target)
{
Arrays.sort(target);
int n = target.length;
if (target[ 0 ] != n) {
return false ;
}
for ( int i = 1 ; i < n; i++) {
if (target[i] != 2 * target[i - 1 ] - 1 ) {
return false ;
}
}
return true ;
}
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
def isPossible(target):
target.sort()
n = len (target)
if target[ 0 ] ! = n:
return False
for i in range ( 1 , n):
if target[i] ! = 2 * target[i - 1 ] - 1 :
return False
return True
target = [ 9 , 3 , 5 ]
res = isPossible(target)
if (res):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
public class GFG {
public static bool IsPossible( int [] target)
{
Array.Sort(target);
int n = target.Length;
if (target[0] != n) {
return false ;
}
for ( int i = 1; i < n; i++) {
if (target[i] != 2 * target[i - 1] - 1) {
return false ;
}
}
return true ;
}
static public void Main()
{
int [] target = { 9, 3, 5 };
bool res = IsPossible(target);
if (res) {
Console.WriteLine( "YES" );
}
else {
Console.WriteLine( "NO" );
}
}
}
|
Javascript
function isPossible(target)
{
target.sort( function (a, b)
{
return a - b;
})
let n = target.length
if (target[0] != n)
return false
for ( var i = 1; i < n; i++)
if (target[i] != 2*target[i-1]-1)
return false
return true
}
let target = [9, 3, 5]
let res = isPossible(target)
if (res)
console.log( "YES" )
else
console.log( "NO" )
|
C++14
#include <algorithm>
#include <iostream>
#include <vector>
bool isPossible(std::vector< int > target) {
std::sort(target.begin(), target.end());
int n = target.size();
if (target[0] != n) {
return false ;
}
for ( int i = 1; i < n; i++) {
if (target[i] != 2 * target[i - 1] - 1) {
return false ;
}
}
return true ;
}
int main() {
std::vector< int > target = {9, 3, 5};
bool res = isPossible(target);
if (res) {
std::cout << "YES" << std::endl;
}
else {
std::cout << "NO" << std::endl;
}
return 0;
}
|
Time Complexity: O(nlogn) as sorting takes O(nlogn)
Auxiliary Space: O(1).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...