Open In App

Convert Array a[] to b[] through Subsequence addition

Last Updated : 07 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ code for the above approach:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
 
bool checkerFunction(vector<int> b)
{
 
    // Sort the array
    sort(b.begin(), b.end());
 
    // Check whether the first element is
    // equal to one or not
    if (b[0] != 1)
        return false;
 
    // Initializing the current sum as 1
    int currentSum = 1;
 
    // Iterating over the array b
    for (int i = 1; i < b.size(); i++) {
 
        // Checking whether the current sum
        // is more than or equal to
        // the current element
        if (currentSum >= b[i]) {
            currentSum += b[i];
        }
        else
            return false;
    }
 
    return true;
}
 
// Driver code
int main()
{
 
    vector<int> b = { 5, 1, 3, 2, 1 };
 
    // Function Call
    bool flag = checkerFunction(b);
 
    if (flag) {
        cout << "YES" << endl;
    }
 
    else {
        cout << "NO" << endl;
    }
    return 0;
}


Java




/*package whatever // do not write package name here */
 
import java.io.*;
import java.util.Arrays;
 
class GFG {
 
    static boolean checkerFunction(int b[], int n)
    {
 
        // sort the array
        Arrays.sort(b);
 
        // check whether the first element is equal to one or not
        if (b[0] != 1)
            return false;
        // initializing the current sum as 1
        int currentSum = 1;
 
        for (int i = 1; i < n; i++) {
            // checking whether the current sum is more than the or equal tothe current element
            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




# Python code for the above approach
 
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')
 
 
# Code is Contributed By Gaurav Kumar


C#




using System;
using System.Linq;
 
class GFG {
    static bool CheckerFunction(int[] b, int n)
    {
 
        // sort the array
        Array.Sort(b);
        // check whether the first element is equal to one
        // or not
 
        if (b[0] != 1)
            return false;
 
        int currentSum = 1;
 
        for (int i = 1; i < n; i++)
        // checking whether the current sum is more than the
        // or equal tothe current element
        {
            if (currentSum >= b[i]) {
                currentSum += b[i];
            }
            else {
                return false;
            }
        }
 
        return true;
    }
    // Driver code
 
    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) {
// Sort the array
b.sort((a, b) => a - b);
 
// Check whether the first element is
// equal to one or not
if (b[0] !== 1) {
return false;
}
 
// Initializing the current sum as 1
let currentSum = 1;
 
// Iterating over the array b
for (let i = 1; i < b.length; i++) {
// Checking whether the current sum
// is more than or equal to
// the current element
if (currentSum >= b[i]) {
currentSum += b[i];
} else {
return false;
}
}
return true;
}
 
// Driver code
const b = [5, 1, 3, 2, 1];
 
// Function Call
const flag = checkerFunction(b);
 
if (flag) {
console.log("YES");
} else {
console.log("NO");
}


Javascript




function checkerFunction(b) {
    // Sort the array
    b.sort((a,b) => a-b);
 
    // Check whether the first element is
    // equal to one or not
    if (b[0] !== 1)
        return false;
 
    // Initializing the current sum as 1
    let currentSum = 1;
 
    // Iterating over the array b
    for (let i = 1; i < b.length; i++) {
 
        // Checking whether the current sum
        // is more than or equal to
        // the current element
        if (currentSum >= b[i]) {
            currentSum += b[i];
        }
        else
            return false;
    }
 
    return true;
}
 
// Driver code
let b = [5, 1, 3, 2, 1];
 
// Function Call
let flag = checkerFunction(b);
 
if (flag) {
    console.log("YES");
}
 
else {
    console.log("NO");
}


Output

YES

Time complexity: O(n*logn)
Auxiliary Space: 0(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads