Print k different sorted permutations of a given array in C Program.

Given an array containing N integers, the challenge 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.

Syntax

void findSortedPermutations(int arr[], int n, int k);

Algorithm

The approach works by sorting the array while keeping track of original indices. If any two consecutive elements are equal, they can be swapped to generate different permutations −

  1. Create pairs of (value, original_index) for each array element
  2. Sort the pairs by value to get the first permutation
  3. Count available swaps (consecutive equal elements)
  4. Generate k-1 additional permutations by swapping equal adjacent elements

Example

This example demonstrates finding 4 different sorted permutations from an array with duplicate values −

#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int value;
    int index;
} Pair;

int compare(const void *a, const void *b) {
    Pair *pairA = (Pair *)a;
    Pair *pairB = (Pair *)b;
    return pairA->value - pairB->value;
}

void printIndices(Pair arr[], int n) {
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i].index);
    }
    printf("
"); } void swap(Pair *a, Pair *b) { Pair temp = *a; *a = *b; *b = temp; } void findSortedPermutations(int a[], int n, int k) { Pair arr[n]; // Create pairs of (value, original_index) for (int i = 0; i < n; i++) { arr[i].value = a[i]; arr[i].index = i; } // Sort by values qsort(arr, n, sizeof(Pair), compare); // Count possible swaps (consecutive equal elements) int swapCount = 0; for (int i = 1; i < n; i++) { if (arr[i].value == arr[i - 1].value) { swapCount++; } } // Check if k permutations are possible if (swapCount + 1 < k) { printf("-1
"); return; } // Generate k-1 permutations by swapping for (int i = 0; i < k - 1; i++) { printIndices(arr, n); // Find first pair of equal adjacent elements and swap for (int j = 1; j < n; j++) { if (arr[j].value == arr[j - 1].value) { swap(&arr[j], &arr[j - 1]); break; } } } // Print the last permutation printIndices(arr, n); } int main() { int a[] = {2, 5, 6, 2, 2, 2, 2}; int n = sizeof(a) / sizeof(a[0]); int k = 4; printf("Array: "); for (int i = 0; i < n; i++) { printf("%d ", a[i]); } printf("
\nK = %d different sorted permutations:
", k); findSortedPermutations(a, n, k); return 0; }
Array: 2 5 6 2 2 2 2 

K = 4 different sorted permutations:
0 3 4 5 6 1 2 
3 0 4 5 6 1 2 
0 3 4 5 6 1 2 
3 0 4 5 6 1 2 

How It Works

  • The sorted array becomes [2,2,2,2,2,5,6] with indices [0,3,4,5,6,1,2]
  • Since multiple elements are equal, we can swap their positions to create different permutations
  • Each swap of adjacent equal elements generates a new valid permutation
  • The algorithm ensures all permutations maintain the non-decreasing order of values

Time Complexity

The time complexity is O(n log n) for sorting plus O(k × n) for generating k permutations, giving overall O(n log n + k × n). Space complexity is O(n) for storing the pairs.

Conclusion

This approach efficiently generates k different sorted permutations by leveraging duplicate values in the array. It uses sorting with index tracking and strategic swapping of equal adjacent elements to create variations.

Updated on: 2026-03-15T11:51:31+05:30

254 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements