Check if there exist 4 indices in the array satisfying the given condition
Last Updated :
05 Dec, 2022
Given an array A[] of N positive integers and 3 integers X, Y, and Z, the task is to check if there exist 4 indices (say p, q, r, s) such that the following conditions are satisfied:
- 0 < p < q < r < s < N
- Sum of the subarray from A[p] to A[q – 1] is X
- Sum of the subarray from A[q] to A[r – 1] is Y
- Sum of the subarray from A[r] to A[s – 1] is Z
Examples:
Input: N = 10, A[] = {1, 3, 2, 2, 2, 3, 1, 4, 3, 2}, X = 5, Y = 7, Z = 5
Output: YES
Explanation: The 4 integers p, q, r, s are {1, 3, 6, 8}.
- A[1] + A[2] = 5
- A[3] + A [4] + A[5] = 7
- A[6] + A[7] = 5
Input: N = 9, A[] = {31, 41, 59, 26, 53, 58, 97, 93, 23}, X = 100, Y = 101, Z = 100
Output: NO
Approach: The problem can be solved easily with the help of cumulative sum and Binary search.
If we calculate the cumulative sum of the array, then the sum of any subarray can be computed in O(1). Say S[i] is cumulative sum till ith index, then S[j] – S[i] = A[i] + A[i + 1] + …. + A[j – 1].
So, given conditions can be converted into the following:
We need to find 4 integers p, q, r, s such that:
S[q] – S[p] = X
S[r] – S[q] = Y
S[s] – S[r] = Z
Now, for any fixed index (say p), we can find another index (say q) using binary search in a monotonically increasing array (cumulative sum), such that S[q] = S[p] + X. Similarly, we can find r and s. We can perform this search for all possible indices.
NOTE: A set can be used so that we won’t need to perform a binary search explicitly each time.
Thus, the problem can be solved using the following steps :
- Declare a set (say S).
- Initialize a variable (say curr) by 0, to calculate the cumulative sum at each iteration.
- Iterate through the given array and insert the cumulative sum into the set.
- Iterate through the set and check if the current element of the set satisfies the given condition along with 3 other elements (which are also in the set). If so, return true.
- Else, return false.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPossible( int N, int A[], int X, int Y, int Z)
{
set< int > S({ 0 });
int curr = 0;
for ( int i = 0; i < N; i++) {
curr += A[i];
S.insert(curr);
}
for ( auto it : S) {
if (S.find(it + X) != S.end()
&& S.find(it + X + Y) != S.end()
&& S.find(it + X + Y + Z) != S.end()) {
return true ;
}
}
return false ;
}
int main()
{
int N = 10, X = 5, Y = 7, Z = 5;
int A[] = { 1, 3, 2, 2, 2, 3, 1, 4, 3, 2 };
int answer = isPossible(N, A, X, Y, Z);
if (answer == true ) {
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean isPossible( int N, int A[], int X, int Y, int Z)
{
HashSet<Integer> S = new HashSet<>();
int curr = 0 ;
for ( int i = 0 ; i < N; i++) {
curr += A[i];
S.add(curr);
}
for ( int it : S) {
if (!S.contains(it + X)
&& !S.contains(it + X + Y)
&& !S.contains(it + X + Y + Z) ) {
return true ;
}
}
return false ;
}
public static void main(String[] args)
{
int N = 10 , X = 5 , Y = 7 , Z = 5 ;
int A[] = { 1 , 3 , 2 , 2 , 2 , 3 , 1 , 4 , 3 , 2 };
boolean answer = isPossible(N, A, X, Y, Z);
if (answer == true ) {
System.out.print( "YES" + "\n" );
}
else {
System.out.print( "NO" + "\n" );
}
}
}
|
Python3
def isPossible(N, A, X, Y, Z) :
S = set ()
curr = 0
for i in range (N):
curr + = A[i]
S.add(curr)
for it in S:
if ((it + X) in S
and (it + X + Y) in S
and (it + X + Y + Z) in S) :
return True
return False
if __name__ = = "__main__" :
N = 10
X = 5
Y = 7
Z = 5
A = [ 1 , 3 , 2 , 2 , 2 , 3 , 1 , 4 , 3 , 2 ]
answer = isPossible(N, A, X, Y, Z)
if (answer = = True ) :
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static bool isPossible( int N, int [] A, int X, int Y, int Z)
{
HashSet< int > S = new HashSet< int >();
int curr = 0;
for ( int i = 0; i < N; i++) {
curr += A[i];
S.Add(curr);
}
foreach ( int it in S) {
if (!S.Contains(it + X)
&& !S.Contains(it + X + Y)
&& !S.Contains(it + X + Y + Z) ) {
return true ;
}
}
return false ;
}
static public void Main ()
{
int N = 10, X = 5, Y = 7, Z = 5;
int [] A = { 1, 3, 2, 2, 2, 3, 1, 4, 3, 2 };
bool answer = isPossible(N, A, X, Y, Z);
if (answer == true ) {
Console.Write( "YES" + "\n" );
}
else {
Console.Write( "NO" + "\n" );
}
}
}
|
Javascript
function isPossible(N, A, X, Y, Z)
{
let S = new Set([0]);
let curr = 0;
for (let i = 0; i < N; i++) {
curr += A[i];
S.add(curr);
}
for (const it of S.values()) {
if (S.has(it + X) && S.has(it + X + Y) && S.has(it + X + Y + Z)) {
return true ;
}
}
return false ;
}
let N = 10,
X = 5,
Y = 7,
Z = 5;
let A = [1, 3, 2, 2, 2, 3, 1, 4, 3, 2];
let answer = isPossible(N, A, X, Y, Z);
if (answer == true ) {
console.log( "YES" );
} else {
console.log( "NO" );
}
|
Time Complexity: O(N * log(N))
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...