Find Permutation of numbers in range [L, R] having X peaks and Y valleys
Last Updated :
25 Apr, 2023
Given integers L, R, X and Y such that (R > L ≥ 1), (X ≥ 0) and (Y ≥ 0). Find the permutation of the numbers in range [L, R] such that there are exactly X peaks and Y valleys present in the permutation. Print Yes and the permutation if the permutation is found. Otherwise print No.
Note: In an array arr[] there is a peak at ith index if arr[i-1] < arr[i] > arr[i+1] and valley at ith index if arr[i-1] > arr[i] < arr[i+1], where 0< i < N.
Examples:
Input: L = 1, R = 3, X = 1, Y = 0
Output: Yes, arr[] = { 1, 3, 2 }
Explanation: 1 peak is required and no valley is required.
Clearly arr[0] < arr[1] > arr[2], arr[1] is the peak.
Input: L = 4, R = 8, X = 4, Y = 1
Output: No
Explanation: There is no such permutation of size 5 with 4 peaks and 1 valley.
Input: L = 3, R = 5, X = 0, Y = 0
Output: Yes, arr[] = { 3, 4, 5 }
Explanation: 0 peaks and 0 valleys are required.
The sorted array has no peak or valley.
Approach: There can be following five cases. Follow the approaches mentioned for each case.
Case-1(No possible permutation): The first and last element of the permutation does not contribute to the numbers of peaks and valleys.
- So if (X + Y) > (R – L – 1) there will be no permutation which satisfies the numbers of peaks and valleys.
- Also if absolute value of (X – Y) > 1, there will be no such permutation, because there is exactly 1 valley between two peaks and vice-versa.
Case-2(X = 0, Y = 0): Follow the steps mentioned below.
- Create an array arr[] of size (R – L + 1) and store the numbers in range [L, R] in sorted order in the array.
- Print the array.
Case-3(X > Y): Follow the steps mentioned below:
- Create an array arr[] of size (R – L + 1) consisting the numbers in range [L, R] in sorted order.
- Consider last (X + Y – 1) elements to assign X peaks and Y valleys.
- Iterate from i = (N – 2) to i = (N – (X + Y – 1)). where N = (R – L + 1)
- In each iteration swap arr[i] with arr[i+1] and decrement i by 2.
- Print the array
Case-4(X < Y): Follow the steps mentioned below:
- Create an array arr[] of size (R – L + 1) consisting the numbers in range [L, R] in sorted order.
- Consider first (X + Y) elements to assign X peaks and Y valleys.
- Iterate from i = 1 to i = (X+Y).
- For each iteration swap arr[i] with arr[i-1] and increment i by 2.
- Print the array.
Case-5(X = Y): Follow the steps mentioned below:
- Create an array arr[] of length (R – L + 1) consisting the numbers in range [L, R] in sorted order.
- Consider first (X+Y) elements to assign the (X-1) peaks and Y valleys.
- Iterate from i = 1 to i = (X+Y).
- For each iteration swap arr[i] with arr[i-1] and increment i by 2.
- After the iteration is complete swap the last two elements of the array to get one more peak.
- Print the array.
Given below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void BuildMountainArray( int L, int R, int X, int Y)
{
int N = (R - L + 1);
if (((X + Y) > (N - 2)) || abs (X - Y) > 1) {
cout << "No" << endl;
}
else {
vector< int > res(N, 0);
for ( int index = 0; index < N;
index++) {
res[index] = L + index;
}
if (X == 0 && Y == 0) {
cout << "Yes" << endl;
for ( int index = 0; index < N;
index++) {
cout << res[index] << " " ;
}
cout << endl;
return ;
}
else if (X > Y) {
for ( int index = N - 2;
index >= (N - (X + Y + 1));
index -= 2) {
swap(res[index],
res[index + 1]);
}
}
else if (Y > X) {
for ( int index = 1; index <=
(X + Y); index += 2) {
swap(res[index],
res[index - 1]);
}
}
else {
for ( int index = 1; index <=
(X + Y); index += 2) {
swap(res[index],
res[index - 1]);
}
swap(res[N - 2], res[N - 1]);
}
cout << "Yes" << endl;
for ( int index = 0; index < N;
index++) {
cout << res[index] << " " ;
}
cout << endl;
}
}
int main()
{
int L = 1, R = 3, X = 1, Y = 0;
BuildMountainArray(L, R, X, Y);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void BuildMountainArray( int L, int R, int X,
int Y)
{
int N = (R - L + 1 );
if (((X + Y) > (N - 2 )) || Math.abs(X - Y) > 1 ) {
System.out.println( "No" );
}
else
{
int res[] = new int [N];
for ( int index = 0 ; index < N; index++) {
res[index] = L + index;
}
if (X == 0 && Y == 0 ) {
System.out.println( "Yes" );
for ( int index = 0 ; index < N; index++) {
System.out.print(res[index] + " " );
}
System.out.println();
return ;
}
else if (X > Y) {
for ( int index = N - 2 ;
index >= (N - (X + Y + 1 ));
index -= 2 ) {
int temp = res[index];
res[index] = res[index + 1 ];
res[index + 1 ] = temp;
}
}
else if (Y > X) {
for ( int index = 1 ; index <= (X + Y);
index += 2 ) {
int temp = res[index];
res[index] = res[index - 1 ];
res[index - 1 ] = temp;
}
}
else {
for ( int index = 1 ; index <= (X + Y);
index += 2 ) {
int temp = res[index];
res[index] = res[index - 1 ];
res[index - 1 ] = temp;
}
int temp = res[N - 2 ];
res[N - 2 ] = res[N - 1 ];
res[N - 1 ] = temp;
}
System.out.println( "Yes" );
for ( int index = 0 ; index < N; index++) {
System.out.print(res[index] + " " );
}
System.out.println();
}
}
public static void main(String[] args)
{
int L = 1 , R = 3 , X = 1 , Y = 0 ;
BuildMountainArray(L, R, X, Y);
}
}
|
Python3
import math as Math
def BuildMountainArray(L, R, X, Y):
N = (R - L + 1 )
if (((X + Y) > (N - 2 )) or Math.fabs(X - Y) > 1 ):
print ( "No" )
else :
res = [ 0 ] * N
for index in range (N):
res[index] = L + index
if (X = = 0 and Y = = 0 ):
print ( "Yes" )
for index in range (N):
print (res[index], end = " " )
print ("")
return
elif (X > Y):
for index in range (N - 2 , N - (X + Y + 1 ) - 1 , - 2 ):
temp = res[index]
res[index] = res[index + 1 ]
res[index + 1 ] = temp
elif (Y > X):
for index in range ( 1 , X + Y + 1 , 2 ):
temp = res[index]
res[index] = res[index - 1 ]
res[index - 1 ] = temp
else :
for index in range ( 1 , X + Y + 1 , 2 ):
temp = res[index]
res[index] = res[index - 1 ]
res[index - 1 ] = temp
temp = res[N - 2 ]
res[N - 2 ] = res[N - 1 ]
res[N - 1 ] = temp
print ( "Yes" )
for index in range (N):
print (res[index], end = " " )
print ("")
L = 1
R = 3
X = 1
Y = 0
BuildMountainArray(L, R, X, Y)
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG {
static void BuildMountainArray( int L, int R, int X,
int Y)
{
int N = (R - L + 1);
if (((X + Y) > (N - 2)) || Math.Abs(X - Y) > 1) {
Console.WriteLine( "No" );
}
else
{
int [] res = new int [N];
for ( int index = 0; index < N; index++) {
res[index] = L + index;
}
if (X == 0 && Y == 0) {
Console.WriteLine( "Yes" );
for ( int index = 0; index < N; index++) {
Console.Write(res[index] + " " );
}
Console.WriteLine();
return ;
}
else if (X > Y) {
for ( int index = N - 2;
index >= (N - (X + Y + 1));
index -= 2) {
int temp1 = res[index];
res[index] = res[index + 1];
res[index + 1] = temp1;
}
}
else if (Y > X) {
for ( int index = 1; index <= (X + Y);
index += 2) {
int temp2 = res[index];
res[index] = res[index - 1];
res[index - 1] = temp2;
}
}
else
{
for ( int index = 1; index <= (X + Y);
index += 2) {
int temp3 = res[index];
res[index] = res[index - 1];
res[index - 1] = temp3;
}
int temp4 = res[N - 2];
res[N - 2] = res[N - 1];
res[N - 1] = temp4;
}
Console.WriteLine( "Yes" );
for ( int index = 0; index < N; index++) {
Console.Write(res[index] + " " );
}
Console.WriteLine();
}
}
public static void Main()
{
int L = 1, R = 3, X = 1, Y = 0;
BuildMountainArray(L, R, X, Y);
}
}
|
Javascript
<script>
const BuildMountainArray = (L, R, X, Y) => {
let N = (R - L + 1);
if (((X + Y) > (N - 2)) || Math.abs(X - Y) > 1) {
document.write( "No<br/>" );
}
else
{
let res = new Array(N).fill(0);
for (let index = 0; index < N;
index++) {
res[index] = L + index;
}
if (X == 0 && Y == 0) {
document.write( "Yes<br/>" );
for (let index = 0; index < N;
index++) {
document.write(`${res[index]} `);
}
document.write( "<br/>" );
return ;
}
else if (X > Y) {
for (let index = N - 2;
index >= (N - (X + Y + 1));
index -= 2) {
let temp = res[index];
res[index] = res[index + 1];
res[index + 1] = temp;
}
}
else if (Y > X) {
for (let index = 1; index <=
(X + Y); index += 2) {
let temp = res[index];
res[index] = res[index - 1];
res[index - 1] = temp;
}
}
else {
for (let index = 1; index <=
(X + Y); index += 2) {
let temp = res[index];
res[index] = res[index - 1];
res[index - 1] = temp;
}
let temp = res[N - 2];
res[N - 2] = res[N - 1];
res[N - 1] = temp;
}
document.write( "Yes<br/>" );
for (let index = 0; index < N;
index++) {
document.write(`${res[index]} `);
}
document.write( "<br/>" );
}
}
let L = 1, R = 3, X = 1, Y = 0;
BuildMountainArray(L, R, X, Y);
</script>
|
Time Complexity: O(N) where N = (R – L + 1)
Space Complexity: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...