Generate a random permutation of elements from range [L, R] (Divide and Conquer)

Given a range [L, R] where L ≤ R, the task is to generate a random permutation of the sequence [L, L + 1, L + 2, …, R].

Examples:

Input: L = 5, R = 15
Output: 11 9 6 5 8 7 10 12 13 15 14



Input: L = 10, R = 20
Output: 16 14 11 10 13 12 15 17 18 20 19

Approach: We will use Divide and Conquer algorithm for our solution. We will create a global vector that will store random numbers generated by our recursive function generate_random_permutation().
We will pass two arguments L (start) and R (end) to our recursive function. Inside the function it will call another function give_random_number that will return a number between X and Y. Lets call it N. We will store this random number in our vector and then we will call generate_random_permutation() function recursively for range [L, N – 1] and then for range [N + 1, R].
If L becomes greater than R then we will return from the function without performing any task, as this is our base condition.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// To store the random permutation
vector<int> permutation;
  
// Utility function to print
// the generated permutation
void printPermutation()
{
    for (auto i : permutation)
        cout << i << " ";
}
  
// Function to return a random
// number between x and y
int give_random_number(int l, int r)
{
    int x = rand() % (r - l + 1) + l;
    return x;
}
  
// Recursive function to generate
// the random permutation
void generate_random_permutation(int l, int r)
{
  
    // Base condition
    if (l > r)
        return;
  
    // Random number returned from the function
    int n = give_random_number(l, r);
  
    // Inserting random number in vector
    permutation.push_back(n);
  
    // Recursion call for [l, n - 1]
    generate_random_permutation(l, n - 1);
  
    // Recursion call for [n + 1, r]
    generate_random_permutation(n + 1, r);
}
  
// Driver code
int main()
{
    int l = 5;
    int r = 15;
  
    // Generate permutation
    generate_random_permutation(l, r);
  
    // Print the generated permutation
    printPermutation();
  
    return 0;
}

chevron_right


PHP

$r)
return;



// Random number returned from the function
$n = give_random_number($l, $r);

// Inserting random number in vector
array_push($permutation, $n);

// Recursion call for [l, n – 1]
generate_random_permutation($l, $n – 1);

// Recursion call for [n + 1, r]
generate_random_permutation($n + 1, $r);
}

// Driver code
$l = 5;
$r = 15;

// Generate permutation
generate_random_permutation($l, $r);

// Print the generated permutation
printPermutation();

// This code is contributed by mits
?>

Output:

11 9 6 5 8 7 10 12 13 15 14

Time Complexity: O(n)



My Personal Notes arrow_drop_up

DTU2K17 BatchSoftware Engineering

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.



Improved By : Mithun Kumar