Queries on insertion of an element in a Bitonic Sequence

Given a Bitonic sequence ‘S’ and ‘Q’ no. of queries. Each query contain an integer xi, 1 <= i <= Q. The task is to print the length of bitonic sequence after inserting the integer for each query. Also, print the bitonic sequence after all the queries.

Examples:

Input: S = { 1, 2, 5, 2 }, Q = 4, x = { 5, 1, 3, 2 }
Output:
4
5
6
6
Bitonic Sequence: 1 2 3 5 2 1

Explanation:

  • For the 1st query, we need to insert x1 = 5 but since 5 is the maximum element and we can have only one occurrence of the maximum element in S, so we won’t insert 5 in S. Hence, size = 4.
  • For the 2nd query, we need to insert x2 = 1, so we insert it in decreasing part as increasing part already has 1 in it. Hence, size = 5.
  • For 3rd query, we insert x3 = 3 in increasing side so size becomes 6.
  • For 4th query, we cannot insert x2 = 2 since 2 is in both increasing and decreasing side.
  • Final sequence: 1, 2, 3, 5, 2, 1

Input: S = { 1, 2, 5, 2 }, Q = 4, x = { 5, 6, 4, 4 }
Output:
4
5
6
7
Bitonic Sequence: 1 2 4 5 6 4 2

Approach: The idea is to make two sets, one for increasing sequence and other for decreasing sequence.

  1. Now, for each query, first check if the xi is greater than the maximum element in the bitonic sequence or not.
  2. If yes, update the maximum sequence and include that element in the set for increasing sequence.
  3. If no, then check if that element is present in the increasing sequence set, if not include it in increasing sequence set, else include it into decreasing sequence set.
  4. The benefit of using the set is it won’t allow multiple same values. So, if the element is new in the set, it will be included and increase the size of sequence else it will remain the size of sequence same.

Below is the implementation of this approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the bitonic sequence
void solveQuery(int a[], int n, int x[], int q)
{
    int maxx = INT_MIN;
  
    // Finding the maximum element
    for (int i = 0; i < n; i++)
        maxx = max(maxx, a[i]);
  
    set<int> s1, s2;
  
    s1.insert(a[0]);
    s2.insert(a[n - 1]);
  
    // set to include increasing sequence element
    for (int i = 1; i < n; i++)
        if (a[i] > a[i - 1])
            s1.insert(a[i]);
  
    // set to include decreasing sequence element
    for (int i = n - 2; i >= 0; i--)
        if (a[i] > a[i + 1])
            s2.insert(a[i]);
  
    // removing maximum element from
    // decreasing sequence set
    s2.erase(s2.find(maxx));
  
    // for each query
    for (int i = 0; i < q; i++) {
  
        // checking if x is greater than
        // maximum element or not.
        if (maxx <= x[i]) {
            maxx = x[i];
            s1.insert(x[i]);
        }
  
        else {
  
            // checking if x lie in increasing sequence
            if (s1.find(x[i]) == s1.end())
                s1.insert(x[i]);
  
            // else insert into decreasing sequence set
            else
                s2.insert(x[i]);
        }
  
        // finding the length
        int ans = s1.size() + s2.size();
        cout << ans << "\n";
    }
  
    // printing the sequence
    set<int>::iterator it;
    for (it = s1.begin(); it != s1.end(); it++)
        cout << (*it) << " ";
  
    set<int>::reverse_iterator rit;
  
    for (rit = s2.rbegin(); rit != s2.rend(); rit++)
        cout << (*rit) << " ";
}
  
// Driver code
int main()
{
    int a[] = { 1, 2, 5, 2 };
    int n = sizeof(a) / sizeof(a[0]);
    int x[] = { 5, 1, 3, 2 };
    int q = sizeof(x) / sizeof(x[0]);
  
    solveQuery(a, n, x, q);
    return 0;
}

chevron_right


Output:

4
5
6
6
1 2 3 5 2 1


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.