Generate a random permutation of elements from range [L, R] (Divide and Conquer)
Last Updated :
22 Jun, 2022
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++
#include <bits/stdc++.h>
using namespace std;
vector< int > permutation;
void printPermutation()
{
for ( auto i : permutation)
cout << i << " " ;
}
int give_random_number( int l, int r)
{
int x = rand () % (r - l + 1) + l;
return x;
}
void generate_random_permutation( int l, int r)
{
if (l > r)
return ;
int n = give_random_number(l, r);
permutation.push_back(n);
generate_random_permutation(l, n - 1);
generate_random_permutation(n + 1, r);
}
int main()
{
int l = 5;
int r = 15;
generate_random_permutation(l, r);
printPermutation();
return 0;
}
|
Java
import java.util.Vector;
class GFG
{
static Vector<Integer> permutation = new Vector<>();
static void printPermutation()
{
permutation.stream().forEach((i) ->
{
System.out.print(i+ " " );
});
}
static int give_random_number( int l, int r)
{
int x = ( int ) (Math.random()% (r - l + 1 ) + l);
return x;
}
static void generate_random_permutation( int l, int r)
{
if (l > r)
return ;
int n = give_random_number(l, r);
permutation.add(n);
generate_random_permutation(l, n - 1 );
generate_random_permutation(n + 1 , r);
}
public static void main(String[] args)
{
int l = 5 ;
int r = 15 ;
generate_random_permutation(l, r);
printPermutation();
}
}
|
Python3
import random
permutation = []
def printPermutation() :
for i in permutation:
print (i, end = " " )
def give_random_number(l, r) :
x = random.randint(l, r)
return x
def generate_random_permutation(l, r) :
if (l > r) :
return
n = give_random_number(l, r)
permutation.append(n)
generate_random_permutation(l, n - 1 )
generate_random_permutation(n + 1 , r)
l = 5
r = 15
generate_random_permutation(l, r)
printPermutation()
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static List< int > permutation = new List< int >();
static void printPermutation()
{
foreach ( int i in permutation)
Console.Write(i+ " " );
}
static int give_random_number( int l, int r)
{
Random rnd = new Random();
int num = rnd.Next(l, r);
int x = ( int ) (num % (r - l + 1) + l);
return x;
}
static void generate_random_permutation( int l, int r)
{
if (l > r)
return ;
int n = give_random_number(l, r);
permutation.Add(n);
generate_random_permutation(l, n - 1);
generate_random_permutation(n + 1, r);
}
public static void Main(String[] args)
{
int l = 5;
int r = 15;
generate_random_permutation(l, r);
printPermutation();
}
}
|
PHP
<?php
$permutation = array ();
function printPermutation()
{
global $permutation ;
foreach ( $permutation as $i )
echo $i . " " ;
}
function give_random_number( $l , $r )
{
$x = rand() % ( $r - $l + 1) + $l ;
return $x ;
}
function generate_random_permutation( $l , $r )
{
global $permutation ;
if ( $l > $r )
return ;
$n = give_random_number( $l , $r );
array_push ( $permutation , $n );
generate_random_permutation( $l , $n - 1);
generate_random_permutation( $n + 1, $r );
}
$l = 5;
$r = 15;
generate_random_permutation( $l , $r );
printPermutation();
?>
|
Javascript
<script>
var permutation = [];
function printPermutation()
{
for ( var i of permutation)
document.write(i + " " );
}
function give_random_number(l, r)
{
var x = Math.floor(Math.random() * l + r) % (r - l + 1) + l;
return x;
}
function generate_random_permutation(l, r)
{
if (l > r)
return ;
var n = give_random_number(l, r);
permutation.push(n);
generate_random_permutation(l, n - 1);
generate_random_permutation(n + 1, r);
}
var l = 5;
var r = 15;
generate_random_permutation(l, r);
printPermutation();
</script>
|
Output:
11 9 6 5 8 7 10 12 13 15 14
Time Complexity: O(n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...