Check if given Array can be formed from initial Array having one element
Last Updated :
05 Oct, 2023
Given array A[], the task for this problem is to check whether A[] can be formed from array B[] = {1} by performing the following operation any number of times, select a subsequence of B[] and insert total sum of this subsequence in B[] to any position. Print “YES” if it is possible “NO otherwise.
Examples:
Input: A[] = {5, 1, 3, 2, 1}
Output: YES
Explanation:
- Initially, B[] = [1]
- Choosing the subsequence [1] and inserting 1 in the array B[], B[] changes to [1, 1].
- Choosing the subsequence [1, 1] and inserting 1+1 = 2 in the middle of the array B[], B[] changes to [1, 2, 1].
- Choosing the subsequence [1, 2] and inserting 1+2 = 3 after the first 1 of the array B[], B[] changes to [1, 3, 2, 1].
- Choosing the subsequence [1, 3, 1] and inserting 1+3+1 = 5 at the beginning of the array B[], B[] changes to [5, 1, 3, 2, 1]
which is the array we needed to obtain.
Input: A[] = {7, 1, 5, 2, 1}
Output: NO
Approach: To solve the problem follow the below idea:
Priority Queue can be used to solve this problem. Observation is that it is possible construct given array stepwise in ascending order, if sum of all other elements present in B[] is greater than new element that is to be added.
Below are the steps for the above approach:
- Create a priority queue and insert all elements of array A[] in that priority queue.
- Create variable curSum = 1.
- If the first element of the priority queue is not 1 then return “NO”.
- Else erase the first element of the priority queue. Run a while loop until the priority queue becomes empty and store the first element of the priority queue in variable FIRST and then erase the first element of the priority queue. if FIRST is greater than curSum the return “NO” else add FIRST to curSum. If while loop ends return “YES”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string isPossible( int A[], int N)
{
multiset< int > ms;
for ( int i = 0; i < N; i++) {
ms.insert(A[i]);
}
int curSum = 1;
if (*ms.begin() == 1) {
ms.erase(ms.begin());
while (ms.size()) {
int first = *ms.begin();
ms.erase(ms.begin());
if (curSum >= first)
curSum += first;
else
return "NO" ;
}
return "YES" ;
}
else
return "NO" ;
}
int32_t main()
{
int N = 5;
int A[] = { 5, 1, 3, 2, 1 };
cout << isPossible(A, N) << endl;
int N1 = 5;
int A1[] = { 7, 1, 5, 2, 1 };
cout << isPossible(A1, N1) << endl;
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.Collections;
public class GFG {
public static void main(String[] args) {
int N = 5 ;
int [] A = { 5 , 1 , 3 , 2 , 1 };
System.out.println(isPossible(A, N));
int N1 = 5 ;
int [] A1 = { 7 , 1 , 5 , 2 , 1 };
System.out.println(isPossible(A1, N1));
}
public static String isPossible( int [] A, int N) {
ArrayList<Integer> ms = new ArrayList<>();
for ( int i = 0 ; i < N; i++) {
ms.add(A[i]);
}
Collections.sort(ms);
int curSum = 1 ;
if (ms.get( 0 ) == 1 ) {
ms.remove( 0 );
while (!ms.isEmpty()) {
int first = ms.get( 0 );
ms.remove( 0 );
if (curSum >= first) {
curSum += first;
} else {
return "NO" ;
}
}
return "YES" ;
} else {
return "NO" ;
}
}
}
|
Python3
def isPossible(A, N):
ms = []
for i in range (N):
ms.append(A[i])
ms.sort()
curSum = 1
if ms[ 0 ] = = 1 :
ms.pop( 0 )
while ms:
first = ms[ 0 ]
ms.pop( 0 )
if curSum > = first:
curSum + = first
else :
return "NO"
return "YES"
else :
return "NO"
N = 5
A = [ 5 , 1 , 3 , 2 , 1 ]
print (isPossible(A, N))
N1 = 5
A1 = [ 7 , 1 , 5 , 2 , 1 ]
print (isPossible(A1, N1))
|
C#
using System;
using System.Collections.Generic;
public class Program
{
public static string IsPossible(List< int > A, int N)
{
List< int > ms = new List< int >();
for ( int i = 0; i < N; i++)
{
ms.Add(A[i]);
}
ms.Sort();
int curSum = 1;
if (ms[0] == 1)
{
ms.RemoveAt(0);
while (ms.Count > 0)
{
int first = ms[0];
ms.RemoveAt(0);
if (curSum >= first)
{
curSum += first;
}
else
{
return "NO" ;
}
}
return "YES" ;
}
else
{
return "NO" ;
}
}
public static void Main( string [] args)
{
int N = 5;
List< int > A = new List< int > {5, 1, 3, 2, 1};
Console.WriteLine(IsPossible(A, N));
int N1 = 5;
List< int > A1 = new List< int > {7, 1, 5, 2, 1};
Console.WriteLine(IsPossible(A1, N1));
}
}
|
Javascript
function isPossible(A, N) {
let ms = [];
for (let i = 0; i < N; i++) {
ms.push(A[i]);
}
ms.sort();
let curSum = 1;
if (ms[0] === 1) {
ms.shift();
while (ms.length > 0) {
let first = ms[0];
ms.shift();
if (curSum >= first) {
curSum += first;
} else {
return "NO" ;
}
}
return "YES" ;
} else {
return "NO" ;
}
}
let N = 5;
let A = [5, 1, 3, 2, 1];
console.log(isPossible(A, N));
let N1 = 5;
let A1 = [7, 1, 5, 2, 1];
console.log(isPossible(A1, N1));
|
Time Complexity: O(N*logN)
Auxiliary Space: O(N)
Related Articles:
Share your thoughts in the comments
Please Login to comment...