# Queries on insertion of an element in a Bitonic Sequence in C++

In this problem, we are given a bitonic Sequence and Q queries. Each query has an integer x. Our task is to print the length of the bitonic sequence after inserting integers after each query. And at the end print the bitonic sequence.

Problem description − Here, we are given a bitonic sequence. And there are Q queries, each containing one integer that is to be added to the sequence. We will add elements from each query to the sequence and then return the length of the bitonic sequence. After all the queries are completed, we will print the bitonic sequence.

Bitonic sequence is a special type of sequence, which increases upto a point (called a bitonic point), and then decreases.

Example − 1, 5, 6, 8, 9, 7, 5, 2

Let’s take an example to understand the problem better,

## Input

bseq = {1, 4, 6, 5, 2, 1}
Q = 2
Query = {5, 6 }

## Output

7 7
{1, 4, 5, 6, 5, 2, 1 }

## Explanation

For the first query, value to be inserted is 5 which can be inserted in the increasing part of the sequence made sequence {1, 4, 5, 6, 5, 2, 1 }, length 7.

For the first query, value to be inserted is 6 which cannot be inserted as 6 is the max value. So, 6 will not be inserted.

Final sequence {1, 4, 5, 6, 5, 2, 1 } length 7.

To solve this problem, we will have to split the bitonic sequence into two sets, one for increasing part of the sequence upto maximum value. The other one for the decreasing part of the sequence.

Now, for every element to be inserted the can be the following cases −

Case 1(if the element is greater than max) − add the element at the end of the increasing series, and update max.

Case 2 − if the element is lesser than the max, check-in the increasing set first for the availability of the element, insert if no element equal to it is available. Otherwise, search in the decreasing set and add if possible.

Case 3 (if the element is equal to max or the value is available in both increasing and decreasing set) − leave the element cannot be added.

After each query operation, we will find the set of the bitonic sequence by adding the lengths of both the sets,

length(bseq) = length(incSet) + length(decSet)

After all the queries are done, print the bitonic sequence by printing the incSet and then printing the decSet.

Program to illustrate the working of our solution −

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;

void calcBitonicSeqLenth(int bSeq[], int n, int query[], int Q){
int maxVal = INT_MIN;
for (int i = 0; i < n; i++)
maxVal = max(maxVal, bSeq[i]);
set <int> incSet, decSet;
incSet.insert(bSeq);
decSet.insert(bSeq[n - 1]);
for (int i = 1; i < n; i++)
if (bSeq[i] > bSeq[i - 1])
incSet.insert(bSeq[i]);
for (int i = n - 2; i >= 0; i--)
if (bSeq[i] > bSeq[i + 1])
decSet.insert(bSeq[i]);
decSet.erase(decSet.find(maxVal));
for (int i = 0; i < Q; i++) {
if (maxVal <= query[i]) {
maxVal = query[i];
incSet.insert(query[i]);
}
else {
if (incSet.find(query[i]) == incSet.end())
incSet.insert(query[i]);
else
decSet.insert(query[i]);
}
int length = incSet.size() + decSet.size();
cout<<"For query "<<(i+1)<<": The length of Bitonic Sequence is "<<length<<endl;
}
cout<<"The Bitonic Sequence at the end of all queries is : ";
set<int>::iterator it;
for (it = incSet.begin(); it != incSet.end(); it++)
cout<<(*it)<<" ";

set<int>::reverse_iterator revIt;

for (revIt = decSet.rbegin(); revIt != decSet.rend(); revIt++)
cout<<(*revIt)<<" ";
}
int main(){
int bSeq[] = { 1, 4, 6, 5, 2, 1 };
int n = sizeof(bSeq) / sizeof(bSeq);
int Q = 2;
int query[] = { 6, 5 };
calcBitonicSeqLenth(bSeq, n, query, Q);
return 0;
}

## Output

For query 1: The length of Bitonic Sequence is 6
For query 2: The length of Bitonic Sequence is 7
The Bitonic Sequence at the end of all queries is : 1 4 5 6 5 2 1