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.
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.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
void solveQuery( int a[], int n, int x[], int q)
{
int maxx = INT_MIN;
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]);
for ( int i = 1; i < n; i++)
if (a[i] > a[i - 1])
s1.insert(a[i]);
for ( int i = n - 2; i >= 0; i--)
if (a[i] > a[i + 1])
s2.insert(a[i]);
s2.erase(s2.find(maxx));
for ( int i = 0; i < q; i++) {
if (maxx <= x[i]) {
maxx = x[i];
s1.insert(x[i]);
}
else {
if (s1.find(x[i]) == s1.end())
s1.insert(x[i]);
else
s2.insert(x[i]);
}
int ans = s1.size() + s2.size();
cout << ans << "\n" ;
}
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) << " " ;
}
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;
}
|
Java
import java.util.*;
class GFG
{
static void solveQuery( int a[], int n, int x[], int q)
{
int maxx = Integer.MIN_VALUE;
for ( int i = 0 ; i < n; i++)
maxx = Math.max(maxx, a[i]);
Set<Integer> s1 = new HashSet<>(), s2 = new HashSet<>();
s1.add(a[ 0 ]);
s2.add(a[n - 1 ]);
for ( int i = 1 ; i < n; i++)
if (a[i] > a[i - 1 ])
s1.add(a[i]);
for ( int i = n - 2 ; i >= 0 ; i--)
if (a[i] > a[i + 1 ])
s2.add(a[i]);
s2.remove(maxx);
for ( int i = 0 ; i < q; i++)
{
if (maxx <= x[i])
{
maxx = x[i];
s1.add(x[i]);
}
else
{
if (!s1.contains(x[i]))
s1.add(x[i]);
else
s2.add(x[i]);
}
int ans = s1.size() + s2.size();
System.out.print(ans + "\n" );
}
for (Integer it : s1)
System.out.print((it) + " " );
for (Integer it : s2)
System.out.print((it) + " " );
}
public static void main(String[] args)
{
int a[] = { 1 , 2 , 5 , 2 };
int n = a.length;
int x[] = { 5 , 1 , 3 , 2 };
int q = x.length;
solveQuery(a, n, x, q);
}
}
|
Python3
import sys
def solveQuery(a, n, x, q):
maxx = - sys.maxsize
for i in range (n):
maxx = max (maxx, a[i])
s1, s2 = set (), set ()
s1.add(a[ 0 ])
s2.add(a[n - 1 ])
for i in range ( 1 , n):
if a[i] > a[i - 1 ]:
s1.add(a[i])
for i in range (n - 2 , - 1 , - 1 ):
if a[i] > a[i + 1 ]:
s2.add(a[i])
s2.remove(maxx)
for i in range (q):
if maxx < = x[i]:
maxx = x[i]
s1.add(x[i])
else :
if x[i] not in s1:
s1.add(x[i])
else :
s2.add(x[i])
ans = len (s1) + len (s2)
print (ans)
for i in s1:
print (i, end = " " )
for i in reversed ( list (s2)):
print (i, end = " " )
if __name__ = = "__main__" :
a = [ 1 , 2 , 5 , 2 ]
n = len (a)
x = [ 5 , 1 , 3 , 2 ]
q = len (x)
solveQuery(a, n, x, q)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void solveQuery( int []a, int n, int []x, int q)
{
int maxx = int .MinValue;
for ( int i = 0; i < n; i++)
maxx = Math.Max(maxx, a[i]);
HashSet< int > s1 = new HashSet< int >(), s2 = new HashSet< int >();
s1.Add(a[0]);
s2.Add(a[n - 1]);
for ( int i = 1; i < n; i++)
if (a[i] > a[i - 1])
s1.Add(a[i]);
for ( int i = n - 2; i >= 0; i--)
if (a[i] > a[i + 1])
s2.Add(a[i]);
s2.Remove(maxx);
for ( int i = 0; i < q; i++)
{
if (maxx <= x[i])
{
maxx = x[i];
s1.Add(x[i]);
}
else
{
if (!s1.Contains(x[i]))
s1.Add(x[i]);
else
s2.Add(x[i]);
}
int ans = s1.Count + s2.Count;
Console.Write(ans + "\n" );
}
foreach ( int it in s1)
Console.Write((it) + " " );
foreach ( int it in s2)
Console.Write((it) + " " );
}
public static void Main(String[] args)
{
int []a = { 1, 2, 5, 2 };
int n = a.Length;
int []x = { 5, 1, 3, 2 };
int q = x.Length;
solveQuery(a, n, x, q);
}
}
|
Javascript
<script>
function solveQuery(a, n, x, q)
{
let maxx = Number.MIN_VALUE;
for (let i = 0; i < n; i++)
maxx = Math.max(maxx, a[i]);
let s1 = new Set();
let s2 = new Set();
s1.add(a[0]);
s2.add(a[n - 1]);
for (let i = 1; i < n; i++)
if (a[i] > a[i - 1])
s1.add(a[i]);
for (let i = n - 2; i >= 0; i--)
if (a[i] > a[i + 1])
s2.add(a[i]);
s2. delete (maxx);
for (let i = 0; i < q; i++)
{
if (maxx <= x[i])
{
maxx = x[i];
s1.add(x[i]);
}
else
{
if (!s1.has(x[i]))
s1.add(x[i]);
else
s2.add(x[i]);
}
let ans = s1.size + s2.size;
document.write(ans + "</br>" );
}
let S1 = Array.from(s1);
S1.sort( function (a, b){ return a - b});
S1.forEach ( function (it) {
document.write(it + " " );
})
s2.forEach ( function (it) {
document.write(it + " " );
})
}
let a = [ 1, 2, 5, 2 ];
let n = a.length;
let x = [ 5, 1, 3, 2 ];
let q = x.length;
solveQuery(a, n, x, q);
</script>
|
Output
4
5
6
6
1 2 3 5 2 1
Last Updated :
01 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...