生成一个长度为 N 的数组,其中包含 K 个子数组作为它们自己长度的排列
给定整数N和K ,任务是生成一个长度为N的数组,其中恰好包含K个子数组作为1 到 X的排列,其中X是子数组长度。可能存在多个答案,您可以打印其中任何一个。如果无法构造数组,则打印-1 。
注意:长度为 N 的排列是从 1 到 N(含)的整数列表,其中每个元素仅出现一次。
例子:
Input: N = 5, K = 4
Output: 1, 2, 3, 5, 4
Explanation: 4 subarrays which are a permutation of their own length are:
A[1] = {1}
A[1…2] = {1, 2}
A[1…3] = {1, 2, 3}
A[1…5] = {1, 2, 3, 5, 4}
Note that their exists no permutation of length 4 as a subarray.
Input: N = 7, K = 3
Output: {1, 2, 7, 4, 5, 6, 3}
Explanation: 3 subarrays which are a permutation of their own length are:
A[1] = {1}
A[1…2] = {1, 2}
A[1…7] = {1, 2, 7, 3, 4, 5, 6}
Their exists no permutations of lengths 3, 4, 5 and 6 as a subarray.
方法:该问题的解决方案基于以下观察。如果所有数字按从1 到 N的递增顺序排列,则总共有N个子数组作为它们自己长度的排列。如果任何值与最高值交换,则排列数会减少。因此,使数字与K相同,使第K 值最高,并保持其他人越来越多地排序将完成任务。如果N > 1 ,则至少有 2 个子数组是它们自己长度的排列。请按照以下步骤解决问题:
- 如果N > 1 且 K < 2或如果K = 0 ,则不可能有这样的数组。
- 生成一个长度为N的数组,该数组由从1 到 N的整数按排序顺序组成。
- 数组中的最大元素显然是最后一个元素N ,即arr[N-1] 。
- 将 arr[N-1]与arr[K-1]交换
- 数组是一种可能的答案。
插图:
For example take N = 5, K = 4
- Generate array containing 1 to N is sorted order.
arr[] = {1, 2, 3, 4, 5}
There are 5 subarrays that are permutations of their own length: {1}, {1, 2}, {1, 2, 3}, {1, 2, 3, 4}, {1, 2, 3, 4, 5} - Here required K is 4. So swap the 4th element with the highest value.
arr[] = {1, 2, 3, 5, 4}
Now there are only K subarrays which are permutation of their own length: {1}, {1, 2}, {1, 2, 3}, {1, 2, 3, 4, 5}.
Because maximum value arrives at Kth position and now
for subarrays of length K or greater (but less than N) the highest element gets included in the subarray
which is not part of a permutation containing elements from 1 to X (subarray length).
下面是上述方法的实现。
C++
// C++ program to implement the approach
#include
using namespace std;
// Function to generate the required array
vector generateArray(int N, int K)
{
vector arr;
// If making an array is not possible
if (K == 0 || (N > 1 && K < 2))
return arr;
arr.assign(N, 0);
// Array of size N in sorted order
for (int i = 1; i <= N; i++)
arr[i - 1] = i;
// Swap the maximum with the Kth element
swap(arr[K - 1], arr[N - 1]);
return arr;
}
// Driver code
int main()
{
int N = 5, K = 4;
// Function call
vector ans = generateArray(N, K);
if (ans.size() == 0)
cout << "-1";
else {
for (int i : ans)
cout << i << " ";
}
return 0;
}
Java
// Java code for the above approach
import java.util.*;
class GFG{
// Function to swap two numbers
static void swap(int m, int n)
{
// Swapping the values
int temp = m;
m = n;
n = temp;
}
// Function to generate the required array
static int[] generateArray(int N, int K)
{
int[] arr = new int[N];
// If making an array is not possible
if (K == 0 || (N > 1 && K < 2))
return arr;
for (int i = 0; i < N; i++) {
arr[i] = 0;
}
// Array of size N in sorted order
for (int i = 1; i <= N; i++)
arr[i - 1] = i;
// Swap the maximum with the Kth element
swap(arr[K - 1], arr[N - 1]);
return arr;
}
// Driver code
public static void main(String[] args)
{
int N = 5, K = 4;
// Function call
int[] ans = generateArray(N, K);
if (ans.length == 0)
System.out.print("-1");
else {
for (int i = 0; i < ans.length; i++) {
System.out.print(ans[i] + " ");
}
}
}
}
// This code is contributed by sanjoy_62.
Python3
# Python program to implement the approach
# Function to generate the required array
def generateArray(N, K):
arr = []
# If making an array is not possible
if (K == 0 or (N > 1 and K < 2)):
return arr
for i in range(0, N):
arr.append(0)
# Array of size N in sorted order
for i in range(1, N + 1):
arr[i-1] = i
# Swap the maximum with the Kth element
arr[K - 1], arr[N - 1] = arr[N - 1], arr[K - 1]
return arr
# Driver code
N = 5
K = 4
# Function call
ans = generateArray(N, K)
if (len(ans) == 0):
print("-1")
else:
for i in ans:
print(i, end=' ')
# This code is contributed by ninja_hattori.
C#
// C# program to implement the approach
using System;
class GFG {
// Function to swap two numbers
static void swap(int m, int n)
{
// Swapping the values
int temp = m;
m = n;
n = temp;
}
// Function to generate the required array
static int[] generateArray(int N, int K)
{
int[] arr = new int[N];
// If making an array is not possible
if (K == 0 || (N > 1 && K < 2))
return arr;
for (int i = 0; i < N; i++) {
arr[i] = 0;
}
// Array of size N in sorted order
for (int i = 1; i <= N; i++)
arr[i - 1] = i;
// Swap the maximum with the Kth element
swap(arr[K - 1], arr[N - 1]);
return arr;
}
// Driver code
public static void Main()
{
int N = 5, K = 4;
// Function call
int[] ans = generateArray(N, K);
if (ans.Length == 0)
Console.Write("-1");
else {
for (int i = 0; i < ans.Length; i++) {
Console.Write(ans[i] + " ");
}
}
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
1 2 3 5 4
时间复杂度: 在)
辅助空间: 在)