Convert Array a[] to b[] through Subsequence addition
Last Updated :
07 Dec, 2023
Given an array a[] equal = {1} and a target array b[], the task is to check whether you can transform a to b or not by performing an operation on array a[]. The operation is defined as choosing any subsequence of array a and insert the sum of the numbers of the subsequence to the array a at any position.
Examples:
Input: b[] = {5, 1, 3, 2, 1}
Output: Yes
Explanation: Initially, a = [1]
By choosing the subsequence [1], and inserting 1 in the array, a changes to [1, 1]
By choosing the subsequence [1, 1], and inserting 1+1 = 2 in the middle of the array, a changes to [1, 2, 1]
By choosing the subsequence [1, 2], and inserting 1+2 = 3 after the first 1 of the array, a changes to [1, 3, 2, 1]
By choosing the subsequence [1, 3, 1] and inserting 1+3+1 = 5 at the beginning of the array, a changes to [5, 1, 3, 2, 1]
Input: b[] = {7, 1, 5, 2, 1}
Output: No
Explanation: Initially, a = [1]
By choosing the subsequence [1], and inserting 1 in the array, a changes to [1, 1]
By choosing the subsequence [1, 1], and inserting 1+1 = 2 in the middle of the array, a changes to [1, 2, 1]
Now you can’t form 5 from any subsequence
Approach: To solve the problem follow the below idea:
It can be observed that if the first element of b is not 1, then the transformation is not possible as in order to generate all the other elements using the given operation, the first element of b must be 1. Now we have to check whether each element in b can be generated by adding elements of a subsequence of arr. Now sort the array b and iterates through each element and check if it is possible to generate the current element by adding elements in subset of arr. If it is not possible, then the transformation is not possible. This is because each element in b must be generated using the elements of arr in order to transform arr to b.
Below are the steps for the above approach:
- Sort the target vector.
- Check if the first element of the target vector is not equal to 1, and return false.
- Initialize a variable say currentSum = 1.
- Iterate the target vector from the second element and check if currentSum ≥ current element,
- Add the current element to the current sum
- Else, return false.
- If iteration over the target vector is completed without returning false, return true, indicating that the vector arr can be represented as the target vector.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
bool checkerFunction(vector< int > b)
{
sort(b.begin(), b.end());
if (b[0] != 1)
return false ;
int currentSum = 1;
for ( int i = 1; i < b.size(); i++) {
if (currentSum >= b[i]) {
currentSum += b[i];
}
else
return false ;
}
return true ;
}
int main()
{
vector< int > b = { 5, 1, 3, 2, 1 };
bool flag = checkerFunction(b);
if (flag) {
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
return 0;
}
|
Java
import java.io.*;
import java.util.Arrays;
class GFG {
static boolean checkerFunction( int b[], int n)
{
Arrays.sort(b);
if (b[ 0 ] != 1 )
return false ;
int currentSum = 1 ;
for ( int i = 1 ; i < n; i++) {
if (currentSum >= b[i]) {
currentSum += b[i];
}
else
return false ;
}
return true ;
}
public static void main(String[] args)
{
int [] b = { 5 , 1 , 3 , 2 , 1 };
int n = b.length;
if (checkerFunction(b, n))
System.out.print( "YES" );
else
System.out.print( "NO" );
}
}
|
Python
def checkerFunction(b):
b.sort()
if b[ 0 ]! = 1 :
return False
currentSum = 1
for i in range ( len (b)):
if currentSum> = b[i]:
currentSum + = b[i]
else :
return False
return True
b = [ 5 , 1 , 3 , 2 , 1 ]
flag = checkerFunction(b)
if flag:
print ( 'YES' )
else :
print ( 'NO' )
|
C#
using System;
using System.Linq;
class GFG {
static bool CheckerFunction( int [] b, int n)
{
Array.Sort(b);
if (b[0] != 1)
return false ;
int currentSum = 1;
for ( int i = 1; i < n; i++)
{
if (currentSum >= b[i]) {
currentSum += b[i];
}
else {
return false ;
}
}
return true ;
}
static void Main( string [] args)
{
int [] b = { 5, 1, 3, 2, 1 };
int n = b.Length;
if (CheckerFunction(b, n))
Console.Write( "YES" );
else
Console.Write( "NO" );
}
}
|
Javascript
function checkerFunction(b) {
b.sort((a, b) => a - b);
if (b[0] !== 1) {
return false ;
}
let currentSum = 1;
for (let i = 1; i < b.length; i++) {
if (currentSum >= b[i]) {
currentSum += b[i];
} else {
return false ;
}
}
return true ;
}
const b = [5, 1, 3, 2, 1];
const flag = checkerFunction(b);
if (flag) {
console.log( "YES" );
} else {
console.log( "NO" );
}
|
Javascript
function checkerFunction(b) {
b.sort((a,b) => a-b);
if (b[0] !== 1)
return false ;
let currentSum = 1;
for (let i = 1; i < b.length; i++) {
if (currentSum >= b[i]) {
currentSum += b[i];
}
else
return false ;
}
return true ;
}
let b = [5, 1, 3, 2, 1];
let flag = checkerFunction(b);
if (flag) {
console.log( "YES" );
}
else {
console.log( "NO" );
}
|
Time complexity: O(n*logn)
Auxiliary Space: 0(1)
Share your thoughts in the comments
Please Login to comment...