Given an array arr[] containing N integers, the task is to print k different permutations of indices such that the values at those indices form a non-decreasing sequence. Print -1 if it is not possible.
Examples:
Input: arr[] = {1, 3, 3, 1}, k = 3
Output:
0 3 1 2
3 0 1 2
3 0 2 1
For every permutation, the values at the indices form the following sequence {1, 1, 3, 3}
Input: arr[] = {1, 2, 3, 4}, k = 3
Output: -1
There is only 1 non decreasing sequence possible {1, 2, 3, 4}.
Approach: Sort the given array and keep track of the original indices of each element. That gives one required permutation. Now if any 2 continuous elements are equal then they can be swapped to get another permutation. Similarly, the third permutation can be generated.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;int next_pos = 1;// Utility function to print the original indices// of the elements of the arrayvoid printIndices(int n, pair<int, int> a[]){ for (int i = 0; i < n; i++) cout << a[i].second << " "; cout << endl;}// Function to print the required permutationsvoid printPermutations(int n, int a[], int k){ // To keep track of original indices pair<int, int> arr[n]; for (int i = 0; i < n; i++) { arr[i].first = a[i]; arr[i].second = i; } // Sort the array sort(arr, arr + n); // Count the number of swaps that can // be made int count = 1; for (int i = 1; i < n; i++) if (arr[i].first == arr[i - 1].first) count++; // Cannot generate 3 permutations if (count < k) { cout << "-1"; return; } for (int i = 0; i < k - 1; i++) { // Print the first permutation printIndices(n, arr); // Find an index to swap and create // second permutation for (int j = next_pos; j < n; j++) { if (arr[j].first == arr[j - 1].first) { swap(arr[j], arr[j - 1]); next_pos = j + 1; break; } } } // Print the last permuation printIndices(n, arr);}// Driver codeint main(){ int a[] = { 1, 3, 3, 1 }; int n = sizeof(a) / sizeof(a[0]); int k = 3; // Function call printPermutations(n, a, k); return 0;} |
Java
// Java implementation of the approachimport java.util.*;class GFG { static int next_pos = 1; static class pair { int first, second; pair() { first = 0; second = 0; } } // Utility function to print the original indices // of the elements of the array static void printIndices(int n, pair a[]) { for (int i = 0; i < n; i++) System.out.print(a[i].second + " "); System.out.println(); } static class sort implements Comparator<pair> { // Used for sorting in ascending order public int compare(pair a, pair b) { return a.first < b.first ? -1 : 1; } } // Function to print the required permutations static void printPermutations(int n, int a[], int k) { // To keep track of original indices pair arr[] = new pair[n]; for (int i = 0; i < n; i++) { arr[i] = new pair(); arr[i].first = a[i]; arr[i].second = i; } // Sort the array Arrays.sort(arr, new sort()); // Count the number of swaps that can // be made int count = 1; for (int i = 1; i < n; i++) if (arr[i].first == arr[i - 1].first) count++; // Cannot generate 3 permutations if (count < k) { System.out.print("-1"); return; } for (int i = 0; i < k - 1; i++) { // Print the first permutation printIndices(n, arr); // Find an index to swap and create // second permutation for (int j = next_pos; j < n; j++) { if (arr[j].first == arr[j - 1].first) { pair t = arr[j]; arr[j] = arr[j - 1]; arr[j - 1] = t; next_pos = j + 1; break; } } } // Print the last permuation printIndices(n, arr); } // Driver code public static void main(String arsg[]) { int a[] = { 1, 3, 3, 1 }; int n = a.length; int k = 3; // Function call printPermutations(n, a, k); }}// This code is contributed by Arnab Kundu |
Python3
# Python 3 implementation of the approach# Utility function to print the original# indices of the elements of the arraydef printIndices(n, a): for i in range(n): print(a[i][1], end=" ") print("\n", end="")# Function to print the required# permutationsdef printPermutations(n, a, k): # To keep track of original indices arr = [[0, 0] for i in range(n)] for i in range(n): arr[i][0] = a[i] arr[i][1] = i # Sort the array arr.sort(reverse=False) # Count the number of swaps that # can be made count = 1 for i in range(1, n): if (arr[i][0] == arr[i - 1][0]): count += 1 # Cannot generate 3 permutations if (count < k): print("-1", end="") return next_pos = 1 for i in range(k - 1): # Print the first permutation printIndices(n, arr) # Find an index to swap and create # second permutation for j in range(next_pos, n): if (arr[j][0] == arr[j - 1][0]): temp = arr[j] arr[j] = arr[j - 1] arr[j - 1] = temp next_pos = j + 1 break # Print the last permuation printIndices(n, arr)# Driver codeif __name__ == '__main__': a = [1, 3, 3, 1] n = len(a) k = 3 # Function call printPermutations(n, a, k)# This code is contributed by# Surendra_Gangwar |
0 3 1 2 3 0 1 2 3 0 2 1
Time Complexity: O(N log N + K N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Generate all binary permutations such that there are more or equal 1's than 0's before every point in all permutations
- Print all permutations in sorted (lexicographic) order
- Print distinct sorted permutations with duplicates allowed in input
- Count of common subarrays in two different permutations of 1 to N
- Write a program to print all permutations of a given string
- Print all distinct permutations of a given string with duplicates
- Print all the palindromic permutations of given string in alphabetic order
- Print the two possible permutations from a given sequence
- Print all lexicographical greater permutations of a given string
- Iterative approach to print all permutations of an Array
- Check if two sorted arrays can be merged to form a sorted array with no adjacent pair from the same array
- Count possible permutations of given array satisfying the given conditions
- Print all permutations with repetition of characters
- Print first n distinct permutations of string using itertools in Python
- Java Program to print distinct permutations of a string
- Print all permutations of a number N greater than itself
- Sort a nearly sorted (or K sorted) array
- Find the Number of Permutations that satisfy the given condition in an array
- Count permutations of given array that generates the same Binary Search Tree (BST)
- Merge K sorted arrays of different sizes | ( Divide and Conquer Approach )
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

