📌  相关文章
📜  检查 N 的排列是否存在与至少 1 个子数组的大小和最小值为 K 的乘积

📅  最后修改于: 2022-05-13 01:56:04.895000             🧑  作者: Mango

检查 N 的排列是否存在与至少 1 个子数组的大小和最小值为 K 的乘积

给定两个整数NK ,任务是检查是否有可能形成N个整数的排列,使得它包含至少1个子数组,使得该子数组的长度与其中存在的最小元素的乘积为K。

例子:

方法:可以根据以下观察解决问题:

按照下面提到的步骤来实现上述想法:

  • 将数组从i = 1迭代到N
  • i为满足所需条件的子数组的长度。
    • 计算子数组中的最小元素。
    • 因为, L * M = K ,所以, M=K / L ,(其中 M 是当前子数组中的最小元素)
  • 检查观察中陈述的条件是否满足,即M < N – L + 1
  • 如果是,则返回 true。

下面是上述方法的实现。

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



输出
true

时间复杂度: O(N)
辅助空间: O(1)