Lexicographically largest N-length Bitonic sequence made up of elements from given range
Last Updated :
01 Jun, 2021
Given three integers N, low and high, the task is to find the lexicographically largest bitonic sequence consisting of N elements lying in the range [low, high]. If it is not possible to generate such a sequence, then print “Not Possible”.
Examples:
Input: N = 5, low = 2, high = 6
Output: 5 6 5 4 3
Explanation:
The sequence {arr[0], arr[1]} is strictly increasing followed by strictly decreasing sequence of the remaining elements. This sequence is the lexicographically largest possible having all elements in the range [2, 6] and length of this sequence is 5.
Input: N = 10, low = 4, high = 10
Output: 7 8 9 10 9 8 7 6 5 4
Approach: The idea is to find the suitable index of high in the resultant sequence and then maintain a difference of 1 between adjacent elements in the sequence such that the bitonic sequence formed is the lexicographically largest possible. Follow the steps below to solve the problem:
- Initialize an array A[] of size N to store the resultant sequence.
- Initialize a variable high_index = -1 to store the index of high in A[] and set high_index = N – (high – low + 1).
- If high_index > (N – 1) / 2, then the remaining N/2 elements cannot be placed in strictly increasing order. So, print “Not Possible”.
- Otherwise, perform the following steps:
- If high_index ? 0, then set high_index = 1 as there has to be a strictly increasing sequence at the beginning.
- Maintain a strictly decreasing sequence with a difference of 1 from the range [high_index, 0], starting with a value high.
- Maintain a strictly decreasing sequence with a difference of 1 from the range[high_index + 1, N – 1] starting with a value (high – 1).
- After completing the above steps, print all the elements in array A[].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void LargestArray( int N, int low, int high)
{
int high_index = N - (high - low + 1);
if (high_index > (N - 1) / 2) {
cout << "Not Possible" ;
return ;
}
if (high_index <= 0)
high_index = 1;
int A[N];
int temp = high;
for ( int i = high_index; i >= 0; i--) {
A[i] = temp--;
}
high -= 1;
for ( int i = high_index + 1; i < N; i++)
A[i] = high--;
for ( int i = 0; i < N; i++) {
cout << A[i] << ' ' ;
}
}
int main()
{
int N = 5, low = 2, high = 6;
LargestArray(N, low, high);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void LargestArray( int N, int low,
int high)
{
int high_index = N - (high - low + 1 );
if (high_index > (N - 1 ) / 2 )
{
System.out.print( "Not Possible" );
return ;
}
if (high_index <= 0 )
high_index = 1 ;
int [] A = new int [N];
int temp = high;
for ( int i = high_index; i >= 0 ; i--)
{
A[i] = temp--;
}
high -= 1 ;
for ( int i = high_index + 1 ; i < N; i++)
A[i] = high--;
for ( int i = 0 ; i < N; i++)
{
System.out.print(A[i] + " " );
}
}
public static void main(String[] args)
{
int N = 5 , low = 2 , high = 6 ;
LargestArray(N, low, high);
}
}
|
Python3
def LargestArray(N, low, high):
high_index = N - (high - low + 1 )
if (high_index > (N - 1 ) / / 2 ):
print ( "Not Possible" )
return
if (high_index < = 0 ):
high_index = 1
A = [ 0 ] * N
temp = high
for i in range (high_index, - 1 , - 1 ):
A[i] = temp
temp = temp - 1
high - = 1
for i in range (high_index + 1 , N):
A[i] = high
high = high - 1
for i in range (N):
print (A[i], end = " " )
N = 5
low = 2
high = 6
LargestArray(N, low, high)
|
C#
using System;
class GFG{
static void LargestArray( int N, int low,
int high)
{
int high_index = N - (high - low + 1);
if (high_index > (N - 1) / 2)
{
Console.Write( "Not Possible" );
return ;
}
if (high_index <= 0)
high_index = 1;
int [] A = new int [N];
int temp = high;
for ( int i = high_index; i >= 0; i--)
{
A[i] = temp--;
}
high -= 1;
for ( int i = high_index + 1; i < N; i++)
A[i] = high--;
for ( int i = 0; i < N; i++)
{
Console.Write(A[i] + " " );
}
}
public static void Main(String[] args)
{
int N = 5, low = 2, high = 6;
LargestArray(N, low, high);
}
}
|
Javascript
<script>
function LargestArray(N, low, high)
{
let high_index = N - (high - low + 1);
if (high_index > (N - 1) / 2)
{
document.write( "Not Possible" );
return ;
}
if (high_index <= 0)
high_index = 1;
let A = [];
let temp = high;
for (let i = high_index; i >= 0; i--)
{
A[i] = temp--;
}
high -= 1;
for (let i = high_index + 1; i < N; i++)
A[i] = high--;
for (let i = 0; i < N; i++)
{
document.write(A[i] + " " );
}
}
let N = 5, low = 2, high = 6;
LargestArray(N, low, high);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...