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++
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Method for checking whether
// SubArray exists or not
void SubArrayExists(int N, long X, bool Flag)
{
// Loop for traversing
// from 1 to N
for (int i = 1; i <= N; i++) {
// Testing the condition
if (X % i == 0 && (X / i) >= 1
&& ((X / i) <= N - i + 1)) {
// Marking the flag true and
// breaking the loop
Flag = true;
break;
}
}
cout << (Flag ? "YES" : "NO") << endl;
}
int main()
{
// Inputs
int N = 5;
long X = 3;
bool Flag = false;
// Function call
SubArrayExists(N, X, Flag);
}
// This code is contributed by Rahul Bhardwaj @rahulbhardwaj.rb
Java
import java.util.*;
public class GFG {
// Driver Function
public static void main(String[] args)
{
// Inputs
int N = 5;
long X = 3;
Boolean Flag = false;
// Function call
SubArrayExists(N, X, Flag);
}
// Method for checking whether
// SubArray exists or not
static void SubArrayExists(int N, long X, boolean Flag)
{
// Loop for traversing
// from 1 to N
for (int i = 1; i <= N; i++) {
// Testing the condition
if (X % i == 0 && (X / i) >= 1
&& ((X / i) <= N - i + 1)) {
// Marking the flag true and
// breaking the loop
Flag = true;
break;
}
}
System.out.println(Flag ? "YES" : "NO");
}
}
Python3
def subarray_exists(N, X):
flag = False
# Loop for traversing from 1 to N
for i in range(1, N+1):
# Testing the condition
if X % i == 0 and (X // i) >= 1 and ((X // i) <= N - i + 1):
# Marking the flag true and breaking the loop
flag = True
break
print("YES" if flag else "NO")
# Inputs
N = 5
X = 3
# Function call
subarray_exists(N, X)
C#
// C# code implementation
using System;
public class GFG {
static public void Main()
{
// Inputs
int N = 5;
long X = 3;
bool Flag = false;
// Function call
SubArrayExists(N, X, Flag);
}
// Method for checking whether SubArray exists or not
static void SubArrayExists(int N, long X, bool Flag)
{
// Loop for traversing from 1 to N
for (int i = 1; i <= N; i++) {
// Testing the condition
if (X % i == 0 && (X / i) >= 1
&& ((X / i) <= N - i + 1)) {
// Marking the flag true and breaking the
// loop
Flag = true;
break;
}
}
Console.WriteLine(Flag ? "YES" : "NO");
}
}
// This code is contributed by sankar.
JavaScript
// JavaScript code to implement the approach
function SubArrayExists(N, X) {
let Flag = false;
// Loop for traversing from 1 to N
for (let i = 1; i <= N; i++) {
// Testing the condition
if (X % i == 0 && (X / i) >= 1
&& ((X / i) <= N - i + 1)) {
// Marking the flag true and breaking the loop
Flag = true;
break;
}
}
console.log(Flag ? "YES" : "NO");
}
// Inputs
let N = 5;
let X = 3;
// Function call
SubArrayExists(N, X);
Time Complexity: O(N)
Auxiliary Space: O(1), As no extra space is used.
Similar Reads
Check if a subarray exists with sum greater than the given Array Given an array of integers arr, the task is to check if there is a subarray (except the given array) such that the sum of its elements is greater than or equal to the sum of elements of the given array. If no such subarray is possible, print No, else print Yes.Examples: Input: arr = {5, 6, 7, 8} Out
7 min read
Check if there exist 4 indices in the array satisfying the given condition 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 < NSum of the subarray from A[p] to A[q - 1] is XSum of the subarray from A[q] to A[
8 min read
Check if it is possible to construct an array with the given conditions Given integers N and K and an array A[] of M integers. The task is to check if it is possible to construct an array of size N such that- All the K consecutive elements of the array are distinct.Considering 1-based indexing, the integer i can be used A[i] times to construct the array.Note that sum of
9 min read
Check if a non-contiguous subsequence same as the given subarray exists or not Given an array arr[] consisting of N integers and two integer values L and R, indicating the starting and ending indices of a subarray, the task is to check if there exists a non-contiguous subsequence which is same as the given subarray or not. If found to be true, print "Yes". Otherwise, print "No
7 min read
Count of elements which is the sum of a subarray of the given Array Given an array arr[], the task is to count elements in an array such that there exists a subarray whose sum is equal to this element.Note: Length of subarray must be greater than 1. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7} Output: 4 Explanation: There are 4 such elements in array - arr[2] = 3
7 min read