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 1Explanation:
- 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.
- Now, for each query, first check if the xi is greater than the maximum element in the bitonic sequence or not.
- If yes, update the maximum sequence and include that element in the set for increasing sequence.
- 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.
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:
// 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; } |
4 5 6 6 1 2 3 5 2 1
Recommended Posts:
- Find bitonic point in given bitonic sequence
- Find an element in Bitonic array
- k-th missing element in increasing sequence which is not present in a given sequence
- Kth smallest element after every insertion
- Array range queries for searching an element
- Queries to insert, delete one occurrence of a number and print the least and most frequent element
- Number of sub-sequence such that it has one consecutive element with difference less than or equal to 1
- Find an element in an array such that elements form a strictly decreasing and increasing sequence
- Minimum operations required to transform a sequence of numbers to a sequence where a[i]=a[i+2]
- Maximum sum bitonic subarray
- Longest Bitonic Subsequence in O(n log n)
- Program to check if an array is bitonic or not
- Printing Longest Bitonic Subsequence
- Length of longest strict bitonic subsequence
- Maximum Length Bitonic Subarray | Set 2 (O(n) time and O(1) Space)
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.



