Saturday, November 16, 2024
HomeSoftware DevelopmentExamine if permutation of N exists with product of atleast 1 subarray's...

Examine if permutation of N exists with product of atleast 1 subarray’s dimension and min as Ok

[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++

  

#embrace <bits/stdc++.h>

utilizing namespace std;

  

bool isPossible(int N, int Ok)

{

    

    bool ans = true;

  

    for (int i = 1; i <= N; i++) {

  

        

        

        int size = i;

  

        

        

        

        

        if (Ok % size == 0) {

  

            

            int min_element = Ok / size;

  

            

            

            

            

            if (min_element < N - size + 1) {

                ans = true;

                break;

            }

        }

    }

  

    

    return ans;

}

  

int essential()

{

    int N = 5;

    int Ok = 6;

  

    

    bool reply = isPossible(N, Ok);

    cout << boolalpha << reply;

    return 0;

}

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

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments