Given two integers N and X. Then the job is to return Yes or NO by examining irrespective of whether there exists a subarray in any permutation of duration N such that it contains a subarray, the place A*B is equivalent to the X. In this article A and B denote the range of aspects in sub-array and the to start with element of sorted subarray respectively.
Illustrations:
Enter: N = 5, X = 3
Output: Indeed
Rationalization: Viewed as the permutation is: 5, 2, 1, 3, 4. Just take the sub-array A4. . . .A4 = 3 . Then A = 1 (As only 1 component is there), B = 3 (If the sub-array is sorted then initially component of that sub-array will be 3). So, A * B = 1 * 3 = 3, Which is equal to Y. Consequently, output is Of course.Enter: N = 7, X = 56
Output: NO
Clarification: It can be verified that no permutation of length N exists these types of that, It gives value of A * B as 56. Therefore, output is NO.
Approach: To fix the problem abide by the beneath plan:
The problem is centered on Greedy logic and observation primarily based. It can be solved by utilizing all those observations by applying them in a code. The observation is, there will surely exist a subarray if the ailment (X % i == && (X / i) ≥ 1 && (( X / i) ≤ N – i + 1) correctly fulfill, where by i is the present element.
Below are the measures for the over technique:
- Produce a Boolean Flag and mark it as Untrue originally.
- Operate a for loop from i = 1 to i ≤ N and follow the down below-described ways underneath the scope of the loop:
- If (X % i == && (X / i) ≥ 1 && (( X / i) ≤ N – i + 1) is correct then mark the flag as true and split the loop.
- Verify if the flag is true, print Indeed else, print NO.
Down below is the code to apply the solution:
Java
|
Time Complexity: O(N)
Auxiliary Space: O(1), As no extra space is used.
Source link