Generate Bitonic Sequence of length N from integers in a given range
Last Updated :
22 Aug, 2022
Given integers N, L and R, the task is to generate a Bitonic Sequence of length N from the integers in the range [L, R] such that the first element is the maximum. If it is not possible to create such a sequence, then print “-1”.
A Bitonic Sequence is a sequence that must be strictly increasing at first and then strictly decreasing.
Examples:
Input: N = 5, L = 3, R = 10
Output: 9, 10, 9, 8, 7
Explanation: The sequence {9, 10, 9, 8, 7} is first strictly increasing and then strictly decreasing.
Input: N = 5, L = 2, R = 5
Output: 4, 5, 4, 3, 2
Explanation:
[ The sequence {4, 5, 4, 3, 2} is first strictly increasing and then strictly decreasing.
Approach: The idea is to use a Deque so that elements can be added from the end and the beginning. Follow the steps below to solve the problem:
- Initialize a deque to store the element of the resultant bitonic sequence.
- Initialize a variable i as 0 and start adding elements in the resultant list starting from (R – i) until i less than the minimum of (R – L + 1) and (N – 1).
- After the above steps if the size of the resultant list is less than N then add elements from (R – 1) to L from the starting of the list until the size of the resultant list does not become N.
- After the above steps, if N is greater than (R – L)*2 + 1, then it is not possible to construct such a sequence then print “-1” else print the sequence stored in deque.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void bitonicSequence( int num, int lower,
int upper)
{
if (num > (upper - lower) * 2 + 1)
{
cout << -1;
return ;
}
deque< int > ans;
deque< int >::iterator j = ans.begin();
for ( int i = 0;
i < min(upper - lower + 1, num - 1);
i++)
ans.push_back(upper - i);
for ( int i = 0;
i < num - ans.size();
i++)
ans.push_front(upper - i - 1);
cout << '[' ;
for (j = ans.begin(); j != ans.end(); ++j)
cout << ' ' << *j;
cout << ' ' << ']' ;
}
int main()
{
int N = 5, L = 3, R = 10;
bitonicSequence(N, L, R);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static void bitonicSequence(
int num, int lower, int upper)
{
if (num > (upper - lower) * 2 + 1 ) {
System.out.println(- 1 );
return ;
}
Deque<Integer> ans
= new ArrayDeque<>();
for ( int i = 0 ;
i < Math.min(upper - lower + 1 ,
num - 1 );
i++)
ans.add(upper - i);
for ( int i = 0 ;
i < num - ans.size(); i++)
ans.addFirst(upper - i - 1 );
System.out.println(ans);
}
public static void main(String[] args)
{
int N = 5 , L = 3 , R = 10 ;
bitonicSequence(N, L, R);
}
}
|
Python3
from collections import deque
def bitonicSequence(num, lower, upper):
if (num > (upper - lower) * 2 + 1 ):
print ( - 1 )
return
ans = deque()
for i in range ( min (upper - lower + 1 ,
num - 1 )):
ans.append(upper - i)
for i in range (num - len (ans)):
ans.appendleft(upper - i - 1 )
print ( list (ans))
if __name__ = = '__main__' :
N = 5
L = 3
R = 10
bitonicSequence(N, L, R)
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static void bitonicSequence( int num,
int lower,
int upper)
{
if (num > (upper - lower) * 2 + 1)
{
Console.WriteLine(-1);
return ;
}
List< int > ans = new List< int >();
for ( int i = 0;
i < Math.Min(upper - lower + 1,
num - 1); i++)
ans.Add(upper - i);
for ( int i = 0;
i < num - ans.Count; i++)
ans.Insert(0,upper - i - 1);
Console.Write( "[" );
foreach ( int x in ans)
Console.Write(x + ", " );
Console.Write( "]" );
}
public static void Main(String[] args)
{
int N = 5, L = 3, R = 10;
bitonicSequence(N, L, R);
}
}
|
Javascript
<script>
function bitonicSequence(num, lower, upper)
{
if (num > (upper - lower) * 2 + 1)
{
document.write( -1);
return ;
}
var ans = [];
for ( var i = 0;
i < Math.min(upper - lower + 1, num - 1);
i++)
ans.push(upper - i);
for ( var i = 0;
i < num - ans.length;
i++)
{
ans.splice(0, 0, upper -i - 1)
}
document.write( '[' );
ans.forEach(element => {
document.write( " " +element);
});
document.write( ' ' + ']' );
}
var N = 5, L = 3, R = 10;
bitonicSequence(N, L, R);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...