Check if Array has 2 distinct subsequences where smaller one has higher Sum
Last Updated :
22 Aug, 2023
Given an array A[] of N integers. Check if there exist 2 distinct sub-sequences X and Y of the given array, such that the sum of elements of X is greater than the sum of elements of Y, but the number of elements in X is less than the number of elements in Y.
Examples:
Input: N = 5, A[] = {2, 3, 8, 1, 6}
Output: YES
Explanation: Consider the sequences: X = {6}, Y = {2, 3}.
It can be easily seen that size of X(i.e. 1) < size of Y(i.e 2), whereas sum of elements of X (i.e 6) > sum of elements of Y(i.e 5).
Input: N = 6, A[] = {1, 1, 1, 1, 1, 1}
Output: NO
Approach: The problem can be solved by using two pointer approach based on the following idea:
In a sorted array if the sum of first K (where K > N/2) smaller elements is less than the sum of the remaining elements then there can be such subsequences, otherwise not.
Follow the steps mentioned below to solve the problem:
- Sort the given array.
- Declare two variables beg and end to store the sum of elements from the beginning and the end of the sorted array.
- Initialize beg with arr[0] and end with 0 because there should be at least 1 element more in the subsequence with less sum.
- Use two pointers from i = 1 and j = N-1:
- Add arr[i] with beg and arr[j] with end.
- If beg is less than end, then break the iteration. Because it is possible to find such subsequences as all the other elements on the higher side is bigger than the first half and the lower sides have already one more element.
- If after the total iteration no such sequence is found, return it is not possible.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool haveSubsequences( int N, int A[])
{
sort(A, A + N);
int beg = A[0], end = 0;
int flag = 0;
int i = 1, j = N - 1;
while (i < j) {
beg += A[i];
end += A[j];
if (beg < end) {
flag = 1;
break ;
}
i++;
j--;
}
if (flag == 1) {
return true ;
}
else {
return false ;
}
}
int main()
{
int N = 5;
int A[] = { 2, 3, 8, 1, 6 };
bool answer = haveSubsequences(N, A);
if (answer == true ) {
cout << "YES" ;
}
else {
cout << "NO" ;
}
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG
{
public static boolean haveSubsequences( int N, int A[])
{
Arrays.sort(A);
int beg = A[ 0 ], end = 0 ;
int flag = 0 ;
int i = 1 , j = N - 1 ;
while (i < j) {
beg += A[i];
end += A[j];
if (beg < end) {
flag = 1 ;
break ;
}
i++;
j--;
}
if (flag == 1 ) {
return true ;
}
else {
return false ;
}
}
public static void main(String[] args)
{
int N = 5 ;
int A[] = { 2 , 3 , 8 , 1 , 6 };
boolean answer = haveSubsequences(N, A);
if (answer == true ) {
System.out.println( "YES" );
}
else {
System.out.println( "NO" );
}
}
}
|
Python3
def haveSubsequences(N, A):
A.sort()
beg = A[ 0 ]
end = 0
flag = 0
i = 1
j = N - 1
while i < j:
beg + = A[i]
end + = A[j]
if (beg < end):
flag = 1
break
i + = 1
j - = 1
if (flag = = 1 ):
return True
else :
return False
if __name__ = = '__main__' :
N = 5
A = [ 2 , 3 , 8 , 1 , 6 ]
answer = haveSubsequences(N, A)
if (answer = = True ):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG
{
static bool haveSubsequences( int N, int []A)
{
Array.Sort(A);
int beg = A[0], end = 0;
int flag = 0;
int i = 1, j = N - 1;
while (i < j) {
beg += A[i];
end += A[j];
if (beg < end) {
flag = 1;
break ;
}
i++;
j--;
}
if (flag == 1) {
return true ;
}
else {
return false ;
}
}
public static void Main()
{
int N = 5;
int []A = { 2, 3, 8, 1, 6 };
bool answer = haveSubsequences(N, A);
if (answer == true ) {
Console.WriteLine( "YES" );
}
else {
Console.WriteLine( "NO" );
}
}
}
|
Javascript
<script>
const haveSubsequences = (N, A) => {
A.sort();
let beg = A[0], end = 0;
let flag = 0;
let i = 1, j = N - 1;
while (i < j) {
beg += A[i];
end += A[j];
if (beg < end) {
flag = 1;
break ;
}
i++;
j--;
}
if (flag == 1) {
return true ;
}
else {
return false ;
}
}
let N = 5;
let A = [2, 3, 8, 1, 6];
let answer = haveSubsequences(N, A);
if (answer == true ) {
document.write( "YES" );
}
else {
document.write( "NO" );
}
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(1)
Another Approach:
- Initialize four variables – smallest, second_smallest, largest, and second_largest – to the maximum and minimum values possible for integers.
- Traverse the given array from the first element to the last element:
a. If the current element is smaller than the smallest value seen so far, update the smallest and second_smallest variables accordingly.
b. If the current element is between the smallest and second_smallest values seen so far, update the second_smallest variable.
c. If the current element is larger than the largest value seen so far, update the largest and second_largest variables accordingly.
d. If the current element is between the largest and second_largest values seen so far, update the second_largest variable.
- After the traversal, compare the sum of the largest and second_smallest variables with the sum of the second_largest and smallest variables:
a. If the former is greater than the latter, return true.
b. Otherwise, return false.
- End of the function.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool haveSubsequences( int N, int A[])
{
int smallest = INT_MAX, second_smallest = INT_MAX;
int largest = INT_MIN, second_largest = INT_MIN;
for ( int i = 0; i < N; i++) {
if (A[i] < smallest) {
second_smallest = smallest;
smallest = A[i];
}
else if (A[i] < second_smallest) {
second_smallest = A[i];
}
if (A[i] > largest) {
second_largest = largest;
largest = A[i];
}
else if (A[i] > second_largest) {
second_largest = A[i];
}
}
if (largest + second_smallest > second_largest + smallest) {
return true ;
}
else {
return false ;
}
}
int main()
{
int N = 5;
int A[] = { 2, 3, 8, 1, 6 };
bool answer = haveSubsequences(N, A);
if (answer == true ) {
cout << "YES" ;
}
else {
cout << "NO" ;
}
}
|
Java
import java.util.*;
public class Main {
public static boolean haveSubsequences( int N, int A[]) {
int smallest = Integer.MAX_VALUE, second_smallest = Integer.MAX_VALUE;
int largest = Integer.MIN_VALUE, second_largest = Integer.MIN_VALUE;
for ( int i = 0 ; i < N; i++) {
if (A[i] < smallest) {
second_smallest = smallest;
smallest = A[i];
} else if (A[i] < second_smallest) {
second_smallest = A[i];
}
if (A[i] > largest) {
second_largest = largest;
largest = A[i];
} else if (A[i] > second_largest) {
second_largest = A[i];
}
}
if (largest + second_smallest > second_largest + smallest) {
return true ;
} else {
return false ;
}
}
public static void main(String[] args) {
int N = 5 ;
int A[] = { 2 , 3 , 8 , 1 , 6 };
boolean answer = haveSubsequences(N, A);
if (answer == true ) {
System.out.println( "YES" );
} else {
System.out.println( "NO" );
}
}
}
|
Python
def haveSubsequences(N, A):
smallest = float ( 'inf' )
second_smallest = float ( 'inf' )
largest = float ( '-inf' )
second_largest = float ( '-inf' )
for i in range (N):
if A[i] < smallest:
second_smallest = smallest
smallest = A[i]
elif A[i] < second_smallest:
second_smallest = A[i]
if A[i] > largest:
second_largest = largest
largest = A[i]
elif A[i] > second_largest:
second_largest = A[i]
if largest + second_smallest > second_largest + smallest:
return True
else :
return False
N = 5
A = [ 2 , 3 , 8 , 1 , 6 ]
answer = haveSubsequences(N, A)
if answer:
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG
{
public static bool HaveSubsequences( int N, int [] A)
{
int smallest = int .MaxValue, second_smallest = int .MaxValue;
int largest = int .MinValue, second_largest = int .MinValue;
for ( int i = 0; i < N; i++)
{
if (A[i] < smallest)
{
second_smallest = smallest;
smallest = A[i];
}
else if (A[i] < second_smallest)
{
second_smallest = A[i];
}
if (A[i] > largest)
{
second_largest = largest;
largest = A[i];
}
else if (A[i] > second_largest)
{
second_largest = A[i];
}
}
if (largest + second_smallest > second_largest + smallest)
{
return true ;
}
else
{
return false ;
}
}
public static void Main( string [] args)
{
int N = 5;
int [] A = new int [] { 2, 3, 8, 1, 6 };
bool answer = HaveSubsequences(N, A);
if (answer == true )
{
Console.WriteLine( "YES" );
}
else
{
Console.WriteLine( "NO" );
}
}
}
|
Javascript
function haveSubsequences(N, A) {
let smallest = Number.MAX_SAFE_INTEGER;
let second_smallest = Number.MAX_SAFE_INTEGER;
let largest = Number.MIN_SAFE_INTEGER;
let second_largest = Number.MIN_SAFE_INTEGER;
for (let i = 0; i < N; i++) {
if (A[i] < smallest) {
second_smallest = smallest;
smallest = A[i];
} else if (A[i] < second_smallest) {
second_smallest = A[i];
}
if (A[i] > largest) {
second_largest = largest;
largest = A[i];
} else if (A[i] > second_largest) {
second_largest = A[i];
}
}
if (largest + second_smallest > second_largest + smallest) {
return true ;
} else {
return false ;
}
}
const N = 5;
const A = [2, 3, 8, 1, 6];
const answer = haveSubsequences(N, A);
if (answer === true ) {
console.log( "YES" );
} else {
console.log( "NO" );
}
|
Time complexity: O(n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...