给定一个由N 个整数组成的数组arr[] ,任务是通过恰好一次交换找到给定数组的字典序最大排列,该排列小于给定数组。如果有可能获得这样的排列,则打印该排列。否则,打印“-1” 。
例子:
Input: arr[] = {5, 4, 3, 2, 1}
Output: 5 4 3 1 2
Explanation:
Lexicographically, the largest permutation which is smaller than the given array can be formed by swapping 2 and 1.
Hence, the resultant permutation is {5, 4, 3, 1, 2}
Input: arr[] = {1, 2, 3, 4, 5}
Output: -1
方法:给定的问题可以通过找到最后一个大于其下一个元素的元素,并将其与数组中下一个较小的元素交换来解决。请按照以下步骤解决问题:
- 如果给定数组按升序排序,则打印“-1”,因为不可能按字典顺序找到小于给定数组的给定数组的最大排列。
- 从末尾遍历给定的数组并找到该索引,例如idx ,它严格大于下一个元素。
- 现在,再次从末尾遍历给定的数组并找到第一个元素的索引(比如j ),该元素较小,元素arr[idx] 。
- 减少j的值直到arr[j – 1]与arr[j]相同。
- 交换数组arr[] 中索引idx和j处的元素以获得结果排列。
- 完成上述步骤后,打印数组作为结果排列。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to lexicographic largest
// permutation possible by a swap
// that is smaller than given array
void findPermutation(vector& arr)
{
int N = arr.size();
int i = N - 2;
// Find the index of first element
// such that arr[i] > arr[i + 1]
while (i >= 0 && arr[i] <= arr[i + 1])
i--;
// If the array is sorted
// in increasing order
if (i == -1) {
cout << "-1";
return;
}
int j = N - 1;
// Find the index of first element
// which is smaller than arr[i]
while (j > i && arr[j] >= arr[i])
j--;
// If arr[j] == arr[j-1]
while (j > i && arr[j] == arr[j - 1]) {
// Decrement j
j--;
}
// Swap the element
swap(arr[i], arr[j]);
// Print the array arr[]
for (auto& it : arr) {
cout << it << ' ';
}
}
// Driver Code
int main()
{
vector arr = { 1, 2, 5, 3, 4, 6 };
findPermutation(arr);
return 0;
}
Java
// java program for the above approach
import java.util.*;
class GFG{
// Function to lexicographic largest
// permutation possible by a swap
// that is smaller than given array
static void findPermutation(int[] arr)
{
int N = arr.length;
int i = N - 2;
// Find the index of first element
// such that arr[i] > arr[i + 1]
while (i >= 0 && arr[i] <= arr[i + 1])
i--;
// If the array is sorted
// in increasing order
if (i == -1)
{
System.out.print("-1");
return;
}
int j = N - 1;
// Find the index of first element
// which is smaller than arr[i]
while (j > i && arr[j] >= arr[i])
j--;
// If arr[j] == arr[j-1]
while (j > i && arr[j] == arr[j - 1])
{
// Decrement j
j--;
}
// Swap the element
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
// Print the array arr[]
for(int it : arr)
{
System.out.print(it + " ");
}
}
// Driver code
public static void main(String[] args)
{
int[] arr = { 1, 2, 5, 3, 4, 6 };
findPermutation(arr);
}
}
// This code is contributed by splevel62.
C#
// C# program for the above approach
using System;
class GFG{
// Function to lexicographic largest
// permutation possible by a swap
// that is smaller than given array
static void findPermutation(int[] arr)
{
int N = arr.Length;
int i = N - 2;
// Find the index of first element
// such that arr[i] > arr[i + 1]
while (i >= 0 && arr[i] <= arr[i + 1])
i--;
// If the array is sorted
// in increasing order
if (i == -1)
{
Console.Write("-1");
return;
}
int j = N - 1;
// Find the index of first element
// which is smaller than arr[i]
while (j > i && arr[j] >= arr[i])
j--;
// If arr[j] == arr[j-1]
while (j > i && arr[j] == arr[j - 1])
{
// Decrement j
j--;
}
// Swap the element
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
// Print the array arr[]
foreach(int it in arr)
{
Console.Write(it + " ");
}
}
// Driver Code
public static void Main()
{
int[] arr = { 1, 2, 5, 3, 4, 6 };
findPermutation(arr);
}
}
// This code is contributed by ukasp
Python3
# Python program for the above approach
# Function to lexicographic largest
# permutation possible by a swap
# that is smaller than given array
def findPermutation(arr):
N = len(arr)
i = N - 2
# Find the index of first element
# such that arr[i] > arr[i + 1]
while (i >= 0 and arr[i] <= arr[i + 1]):
i -= 1
# If the array is sorted
# in increasing order
if (i == -1) :
print("-1")
return
j = N - 1
# Find the index of first element
# which is smaller than arr[i]
while (j > i and arr[j] >= arr[i]):
j -= 1
# If arr[j] == arr[j-1]
while (j > i and arr[j] == arr[j - 1]) :
# Decrement j
j -= 1
# Swap the element
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
# Pr the array arr[]
for it in arr :
print(it, end = " ")
# Driver Code
arr = [ 1, 2, 5, 3, 4, 6 ]
findPermutation(arr)
# This code is contributed by code_hunt.
Javascript
输出:
1 2 4 3 5 6
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live