[ad_1]
Given two integers N and Ok, the duty is to verify whether it is attainable to type a permutation of N integers such that it comprises atleast 1 subarray such that the product of size of that subarray with minimal ingredient current in it’s Ok.
A permutation of dimension N have all of the integers from 1 to N current in it solely as soon as.
Examples:
Enter: N = 5, Ok = 6
Output: True
Clarification: {4, 2, 1, 3, 5} is a sound array containing integers from 1 to five. The required subarray is {3, 5}.
Size of subarray = 2, minimal ingredient in subarray = 3.
Their product = 2 x 3 = 6, which is the same as Ok.Enter: N = 4, Ok = 10
Output: False
Strategy: The issue could be solved based mostly on the next commentary:
Suppose in a N dimension array having integers from 1 to N, there exist a subarray of dimension L, having minimal ingredient M such that M * L = Ok. Subsequently, M = Ok / L or Ok have to be divisible by the size of the subarray. Additionally, M needs to be minimal ingredient in subarray of dimension L.
In a permutation of N integers, there are N – M + 1 components, that are better than or equal to M. So, for M to be minimal in subarray of dimension L, N – M + 1 ≥ L
Observe the steps talked about under to implement the above concept:
- Iterate the array from i = 1 to N
- Let i be the size of subarray satisfying the required circumstances.
- Calculate the minimal ingredient within the subarray.
- As, L * M = Ok, so, M=Ok / L, (the place M is the minimal ingredient in present subarray)
- Examine if circumstances acknowledged in commentary are happy or not i.e. M < N – L + 1.
- In that case, return true.
Under is the implementation of the above strategy.
C++
|
Time Complexity: O(N)
Auxiliary Area: O(1)
[ad_2]