Array 的字典最小排列使得前缀总和直到任何索引都不等于 K
给定一个数组arr[],由N个不同的正整数和一个整数K 组成,任务是找到数组的字典序最小排列,使得输出数组的任何前缀的元素之和不等于K .如果不存在这样的排列,则打印“ -1 ”。否则,打印输出数组。
例子:
Input: arr[] = {2, 6, 4, 5, 3, 1}, K = 15
Output: 1 2 3 4 6 5
Explanation:
The lexicographically smallest permutation of the given array is, {1, 2, 3, 4, 6, 5} having no prefix of sum equal to 15.
Input: arr[]={3, 1, 4, 6}, K = 12
Output: 1 3 4 6
Explanation:
The lexicographically smallest permutation of the given array is, {1, 3, 4, 6} having no prefix of sum equal to 12.
方法:可以通过首先对数组进行升序排序,然后将前缀的最后一个元素与下一个元素交换其总和等于K来解决该问题。请按照以下步骤解决此问题:
- 如果数组的和等于K ,则打印“ -1 ”,因为不可能找到满足条件的数组的任何排列。
- 按升序对数组进行排序。
- 初始化一个变量,比如preSum为0以存储前缀的总和。
- 使用变量i遍历范围[0, N-2]并执行以下步骤:
- 通过arr[i]增加preSum 。
- 如果preSum等于K,则交换arr[i]和arr[i+1]然后 break。
- 最后,完成上述步骤后,打印数组arr[]的元素。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the output array
// satisfying given conditions
void findpermutation(int arr[], int N, int K)
{
int sum = 0;
// Traverse the array arr[]
for (int i = 0; i < N; i++) {
sum = sum + arr[i];
}
// If sum is equal to K
if (sum == K) {
cout << -1 << endl;
}
else {
// Sort array in ascending order
sort(arr, arr + N);
// Stores the sum of a prefix
int preSum = 0;
// Traverse the array arr[]
for (int i = 0; i < N; i++) {
// Update the preSum
preSum = preSum + arr[i];
// If preSum is equal to K
if (preSum == K) {
// Swap
swap(arr[i], arr[i + 1]);
break;
}
}
// Print the array arr[]
for (int i = 0; i < N; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
}
// Driver code
int main()
{
int arr[] = { 2, 6, 4, 5, 3, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = 15;
findpermutation(arr, N, K);
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the output array
// satisfying given conditions
static void findpermutation(int arr[], int N, int K)
{
int sum = 0;
// Traverse the array arr[]
for(int i = 0; i < N; i++)
{
sum = sum + arr[i];
}
// If sum is equal to K
if (sum == K)
{
System.out.println(-1);
}
else
{
// Sort array in ascending order
Arrays.sort(arr);
// Stores the sum of a prefix
int preSum = 0;
// Traverse the array arr[]
for(int i = 0; i < N; i++)
{
// Update the preSum
preSum = preSum + arr[i];
// If preSum is equal to K
if (preSum == K)
{
// Swap
swap(arr, i, i + 1);
break;
}
}
// Print the array arr[]
for(int i = 0; i < N; i++)
{
System.out.print(arr[i] + " ");
}
System.out.println( );
}
}
static int[] swap(int []arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 2, 6, 4, 5, 3, 1 };
int N = arr.length;
int K = 15;
findpermutation(arr, N, K);
}
}
// This code is contributed by shivanisinghss2110
Python3
# Python3 program for the above approach
# Function to find the output array
# satisfying given conditions
def findpermutation(arr, N, K):
sum = 0
# Traverse the array arr[]
for i in range(0, N):
sum = sum + arr[i]
# If sum is equal to K
if (sum == K):
print("-1")
else:
# Sort array in ascending order
arr.sort()
# Stores the sum of a prefix
preSum = 0
# Traverse the array arr[]
for i in range(0, N):
# Update the preSum
preSum = preSum + arr[i]
# If preSum is equal to K
if (preSum == K):
# Swap
temp = arr[i]
arr[i] = arr[i + 1]
arr[i + 1] = temp
# Print the array arr[]
for i in range(0, N):
print(arr[i], end = " ")
# Driver code
arr = [ 2, 6, 4, 5, 3, 1 ]
N = len(arr)
K = 15
findpermutation(arr, N, K)
# This code is contributed by amreshkumar3
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the output array
// satisfying given conditions
static void findpermutation(int []arr, int N, int K)
{
int sum = 0;
// Traverse the array arr[]
for(int i = 0; i < N; i++)
{
sum = sum + arr[i];
}
// If sum is equal to K
if (sum == K)
{
Console.WriteLine(-1);
}
else
{
// Sort array in ascending order
Array.Sort(arr);
// Stores the sum of a prefix
int preSum = 0;
// Traverse the array arr[]
for(int i = 0; i < N; i++)
{
// Update the preSum
preSum = preSum + arr[i];
// If preSum is equal to K
if (preSum == K)
{
// Swap
swap(arr, i, i + 1);
break;
}
}
// Print the array arr[]
for(int i = 0; i < N; i++)
{
Console.WriteLine(arr[i] + " ");
}
Console.WriteLine( );
}
}
static int[] swap(int []arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
// Driver code
static void Main()
{
int []arr= { 2, 6, 4, 5, 3, 1 };
int N = arr.Length;
int K = 15;
findpermutation(arr, N, K);
}
}
// This code is contributed by SoumikMondal
Javascript
输出
1 2 3 4 6 5
时间复杂度: O(N)
辅助空间: O(1)