Check if there exists any subarray with the given conditions
Last Updated :
27 Apr, 2023
Given two integers N and X. Then the task is to return YES or NO by checking whether there exists a subarray in any permutation of length N such that it contains a subarray, where A*B is equal to the X. Here A and B denote the number of elements in sub-array and the first element of sorted subarray respectively.
Examples:
Input: N = 5, X = 3
Output: YES
Explanation: Considered the permutation is: {5, 2, 1, 3, 4}. Take the sub-array {A4. . . .A4} = { 3 }. Then A = 1 (As only 1 element is there), B = 3 (If the sub-array is sorted then first element of that sub-array will be 3). So, A * B = 1 * 3 = 3, Which is equal to Y. Therefore, output is YES.
Input: N = 7, X = 56
Output: NO
Explanation: It can be verified that no permutation of length N exists such that, It gives value of A * B as 56. Therefore, output is NO.
Approach: To solve the problem follow the below idea:
The problem is based on Greedy logic and observation based. It can be solved by implementing those observations by implementing them in a code. The observation is, there will surely exist a subarray if the condition (X % i == 0 && (X / i) ≥ 1 && (( X / i) ≤ N – i + 1) successfully meet, where i is the current element.
Below are the steps for the above approach:
- Create a Boolean Flag and mark it as False initially.
- Run a for loop from i = 1 to i ≤ N and follow the below-mentioned steps under the scope of the loop:
- If (X % i == 0 && (X / i) ≥ 1 && (( X / i) ≤ N – i + 1) is true then mark the flag as true and break the loop.
- Check if the flag is true, print YES else, print NO.
Below is the code to implement the approach:
C++
#include <bits/stdc++.h>
using namespace std;
void SubArrayExists( int N, long X, bool Flag)
{
for ( int i = 1; i <= N; i++) {
if (X % i == 0 && (X / i) >= 1
&& ((X / i) <= N - i + 1)) {
Flag = true ;
break ;
}
}
cout << (Flag ? "YES" : "NO" ) << endl;
}
int main()
{
int N = 5;
long X = 3;
bool Flag = false ;
SubArrayExists(N, X, Flag);
}
|
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int N = 5 ;
long X = 3 ;
Boolean Flag = false ;
SubArrayExists(N, X, Flag);
}
static void SubArrayExists( int N, long X, boolean Flag)
{
for ( int i = 1 ; i <= N; i++) {
if (X % i == 0 && (X / i) >= 1
&& ((X / i) <= N - i + 1 )) {
Flag = true ;
break ;
}
}
System.out.println(Flag ? "YES" : "NO" );
}
}
|
Python3
def subarray_exists(N, X):
flag = False
for i in range ( 1 , N + 1 ):
if X % i = = 0 and (X / / i) > = 1 and ((X / / i) < = N - i + 1 ):
flag = True
break
print ( "YES" if flag else "NO" )
N = 5
X = 3
subarray_exists(N, X)
|
C#
using System;
public class GFG {
static public void Main()
{
int N = 5;
long X = 3;
bool Flag = false ;
SubArrayExists(N, X, Flag);
}
static void SubArrayExists( int N, long X, bool Flag)
{
for ( int i = 1; i <= N; i++) {
if (X % i == 0 && (X / i) >= 1
&& ((X / i) <= N - i + 1)) {
Flag = true ;
break ;
}
}
Console.WriteLine(Flag ? "YES" : "NO" );
}
}
|
Javascript
function SubArrayExists(N, X) {
let Flag = false ;
for (let i = 1; i <= N; i++) {
if (X % i == 0 && (X / i) >= 1
&& ((X / i) <= N - i + 1)) {
Flag = true ;
break ;
}
}
console.log(Flag ? "YES" : "NO" );
}
let N = 5;
let X = 3;
SubArrayExists(N, X);
|
Time Complexity: O(N)
Auxiliary Space: O(1), As no extra space is used.
Share your thoughts in the comments
Please Login to comment...