Thursday, March 2, 2023
HomeSoftware DevelopmentTest if there exists any subarray with the given circumstances

# Test if there exists any subarray with the given circumstances

Given two integers N and X. Then the duty is to return YES or NO by checking whether or not there exists a subarray in any permutation of size N such that it comprises a subarray, the place A*B is the same as the X. Right here A and B denote the variety of parts in sub-array and the primary aspect of sorted subarray respectively.

Examples:

Enter: N = 5, X = 3
Output: YES
Rationalization: Thought-about the permutation is: {5, 2, 1, 3, 4}. Take the sub-array {A4. . . .A4} = { 3 }. Then A = 1 (As just one aspect is there), B = 3 (If the sub-array is sorted then first aspect of that sub-array shall be 3). So, A * B = 1 * 3 = 3, Which is the same as Y. Subsequently, output is YES.

Enter: N = 7, X = 56
Output: NO
Rationalization: It may be verified that no permutation of size N exists such that, It offers worth of A * B as 56. Subsequently, output is NO.

Strategy: To unravel the issue observe the under concept:

The issue relies on Grasping logic and statement primarily based. It may be solved by implementing these observations by implementing them in a code. The statement is, there’ll absolutely exist a subarray if the situation (X % i == 0 && (X / i) â‰¥ 1 && (( X / Â i) â‰¤ N â€“ i + 1) efficiently meet, the place i is the present aspect.

Under are the steps for the above strategy:

• Create a Boolean Flag and mark it as False initially.Â Â
• Run a for loop from i = 1 to i â‰¤ N and observe the below-mentioned steps below 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.
• Test if the flag is true, print YES else, print NO.

Under is the code to implement the strategy:

## Java

 `import` `java.util.*;` `Â `Â  `public` `class` `GFG {` `Â `Â  `Â Â Â Â ` `Â Â Â Â ``public` `static` `void` `fundamental(String[] args)` `Â Â Â Â ``{` `Â `Â  `Â Â Â Â Â Â Â Â ` `Â Â Â Â Â Â Â Â ``int` `N = ``5``;` `Â Â Â Â Â Â Â Â ``lengthy` `X = ``3``;` `Â Â Â Â Â Â Â Â ``Boolean Flag = ``false``;` `Â `Â  `Â Â Â Â Â Â Â Â ` `Â Â Â Â Â Â Â Â ``SubArrayExists(N, X, Flag);` `Â Â Â Â ``}` `Â `Â  `Â Â Â Â ` `Â Â Â Â ` `Â Â Â Â ``static` `void` `SubArrayExists(``int` `N, ``lengthy` `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"``);` `Â Â Â Â ``}` `}`

Time Complexity: O(N)
Auxiliary Area: O(1), As no additional house is used.

RELATED ARTICLES