# Check if permutation of N exists with product of atleast 1 subarray’s size and min as K

Given two integers N and K, the task is to check if it is possible to form a permutation of N integers such that it contains atleast 1 subarray such that the product of length of that subarray with minimum element present in it is K.

A permutation of size N have all the integers from 1 to N present in it only once.

Examples:

Input: N = 5, K = 6
Output: True
Explanation: {4, 2, 1, 3, 5} is a valid array containing integers from 1 to 5. The required subarray is {3, 5}.
Length of subarray = 2, minimum element in subarray = 3.
Their product = 2 x 3 = 6, which is equal to K.

Input: N = 4, K = 10
Output: False

Approach: The problem can be solved based  on the following observation:

Suppose in a N size array having integers from 1 to N, there exist a subarray of size L, having minimum element M such that M * L = K. Therefore, M = K / L or K must be divisible by the length of the subarray. Also, M should be minimum element in subarray of size L

In a permutation of N integers, there are N – M + 1 elements, which are greater than or equal to M. So, for M to be minimum in subarray of size L, N – M + 1 â‰¥ L

Follow the steps mentioned below to implement the above idea:

• Iterate the array from i = 1 to N
• Let i be the length of subarray satisfying the required conditions.
• Calculate the minimum element in the subarray.
• As, L * M = K, so, M=K / L, (where M is the minimum element in current subarray)
• Check if conditions stated in observation are satisfied or not i.e. M < N – L + 1.
• If so, return true.

Below is the implementation of the above approach.

## C++

 `// C++ code for above approach` `#include ``using` `namespace` `std;` `// Function toCheck if there can be``// a subarray such that product of its``// length with its minimum element is K``bool` `isPossible(``int` `N, ``int` `K)``{``    ``// Variable to store answer``    ``bool` `ans = ``true``;` `    ``for` `(``int` `i = 1; i <= N; i++) {` `        ``// Variable to store length of``        ``// current candidate subarray``        ``int` `length = i;` `        ``// Since minimum element * length``        ``// of subarray should be K,``        ``// that means K should be divisible``        ``// by length of candidate subarray``        ``if` `(K % length == 0) {` `            ``// Candidate for minimum element``            ``int` `min_element = K / length;` `            ``// Checking if candidate for``            ``// minimum element can actually``            ``// be a minimum element in a``            ``// sequence on size "length"``            ``if` `(min_element < N - length + 1) {``                ``ans = ``true``;``                ``break``;``            ``}``        ``}``    ``}` `    ``// Returning answer``    ``return` `ans;``}` `// Driver code``int` `main()``{``    ``int` `N = 5;``    ``int` `K = 6;` `    ``// Function call``    ``bool` `answer = isPossible(N, K);``    ``cout << boolalpha << answer;``    ``return` `0;``}`

## Java

 `// JAVA code for above approach``import` `java.util.*;``class` `GFG ``{` `  ``// Function toCheck if there can be``  ``// a subarray such that product of its``  ``// length with its minimum element is K``  ``public` `static` `boolean` `isPossible(``int` `N, ``int` `K)``  ``{` `    ``// Variable to store answer``    ``boolean` `ans = ``true``;` `    ``for` `(``int` `i = ``1``; i <= N; i++) {` `      ``// Variable to store length of``      ``// current candidate subarray``      ``int` `length = i;` `      ``// Since minimum element * length``      ``// of subarray should be K,``      ``// that means K should be divisible``      ``// by length of candidate subarray``      ``if` `(K % length == ``0``) {` `        ``// Candidate for minimum element``        ``int` `min_element = K / length;` `        ``// Checking if candidate for``        ``// minimum element can actually``        ``// be a minimum element in a``        ``// sequence on size "length"``        ``if` `(min_element < N - length + ``1``) {``          ``ans = ``true``;``          ``break``;``        ``}``      ``}``    ``}` `    ``// Returning answer``    ``return` `ans;``  ``}` `  ``// Driver code``  ``public` `static` `void` `main(String[] args)``  ``{``    ``int` `N = ``5``;``    ``int` `K = ``6``;` `    ``// Function call``    ``boolean` `answer = isPossible(N, K);``    ``System.out.print(answer);``  ``}``}` `// This code is contributed by Taranpreet`

## Python3

 `# Python3 code for above approach` `# Function toCheck if there can be``# a subarray such that product of its``# length with its minimum element is K``def` `isPossible(N, K):``  ` `    ``# Variable to store answer``    ``ans ``=` `1` `    ``for` `i ``in` `range``(``1``, N ``+` `1``):` `        ``# Variable to store length of``        ``# current candidate subarray``        ``length ``=` `i` `        ``# Since minimum element * length``        ``# of subarray should be K,``        ``# that means K should be divisible``        ``# by length of candidate subarray``        ``if` `(K ``%` `length ``=``=` `0``):` `            ``# Candidate for minimum element``            ``min_element ``=` `K ``/` `length` `            ``# Checking if candidate for``            ``# minimum element can actually``            ``# be a minimum element in a``            ``# sequence on size "length"``            ``if` `(min_element < N ``-` `length ``+` `1``):``                ``ans ``=` `1``                ``break` `    ``# Returning answer``    ``return` `ans` `# Driver code``if` `__name__ ``=``=` `"__main__"``:``      ` `    ``N ``=` `5``    ``K ``=` `6` `    ``# Function call``    ``answer ``=` `isPossible(N, K)``    ``print``(``bool``(answer))``    ` `    ``# This code is contributed by hrithikgarg03188.`

## C#

 `// C# code for above approach``using` `System;``class` `GFG {` `  ``// Function toCheck if there can be``  ``// a subarray such that product of its``  ``// length with its minimum element is K``  ``static` `bool` `isPossible(``int` `N, ``int` `K)``  ``{` `    ``// Variable to store answer``    ``bool` `ans = ``true``;` `    ``for` `(``int` `i = 1; i <= N; i++) {` `      ``// Variable to store length of``      ``// current candidate subarray``      ``int` `length = i;` `      ``// Since minimum element * length``      ``// of subarray should be K,``      ``// that means K should be divisible``      ``// by length of candidate subarray``      ``if` `(K % length == 0) {` `        ``// Candidate for minimum element``        ``int` `min_element = K / length;` `        ``// Checking if candidate for``        ``// minimum element can actually``        ``// be a minimum element in a``        ``// sequence on size "length"``        ``if` `(min_element < N - length + 1) {``          ``ans = ``true``;``          ``break``;``        ``}``      ``}``    ``}` `    ``// Returning answer``    ``return` `ans;``  ``}` `  ``// Driver code``  ``public` `static` `void` `Main()``  ``{``    ``int` `N = 5;``    ``int` `K = 6;` `    ``// Function call``    ``bool` `answer = isPossible(N, K);``    ``Console.Write(answer);``  ``}``}` `// This code is contributed by Samim Hossain Mondal.`

## Javascript

 ``

Output
`true`

Time Complexity: O(N)
Auxiliary Space: O(1)

Previous
Next