检查 N 的排列是否存在与至少 1 个子数组的大小和最小值为 K 的乘积
给定两个整数N和K ,任务是检查是否有可能形成N个整数的排列,使得它包含至少1个子数组,使得该子数组的长度与其中存在的最小元素的乘积为K。
A permutation of size N have all the integers from 1 to N present in it only once.
例子:
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
方法:可以根据以下观察解决问题:
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
按照下面提到的步骤来实现上述想法:
- 将数组从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)