Given Q queries of three types where every query consists of a number.

- Add element
on the left*num* - Add element
on the right*num* - Print the number of elements to the right an left of the given element
.*num*

The task is to write a program that performs the above queries.

**Note:** Elements in queries 1 and 2 are distinct and 0 <= num <= 10^{5}.

**Examples:**

Input:

Q = 5

Query of type 1: num = 3

Query of type 2: num = 5

Query of type 1: num = 2

Query of type 1: num = 4

Query of type 3: num = 3

Output:element on the right: 1 element on the left: 2

After query 1, the element positioning is 3

After query 2, the element positioning is 35

After query 3, the element positioning is 235

After query 4, the element positioning is 4235

So there is 1 element to the right and 2 elements on the left of 3 when

query 3 is called.

The following steps can be followed to solve the above problem.

- Initialize a variable
**left**and**right**as 0, and two hash arrays**position[]**and**mark[]**. position[] is used to store the index of num on left and right and mark[] is used to mark if the num is on left or right. - For
**query-1,**increase the count of left, and mark position[num] as left and mark[num] as 1. - For
**query-2,**increase the count of right, and mark position[num] as right and mark[num] as 2 - For
**query-3,**check if the given number is present on right or left using mark[]. - If the number is present on right, then the number of elements to the left of num will be
**(left-position[num])**, and the number of elements to the right of num will be**(position[num] – 1 + right)**. - If the number is present on left, then the number of elements to the right of num will be
**(right-position[num])**, and the number of elements to the left of num will be**(position[num] – 1 + *left)**.

.

Below is the implementation of the above approach:

`// C++ program to print the number of elements ` `// on right and left of a given element ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define MAXN 100005 ` ` ` `// function to perform query 1 ` `void` `performQueryOne(` `int` `num, ` `int` `* left, ` `int` `* right, ` ` ` `int` `* position, ` `int` `* mark) ` `{ ` ` ` `// count number of elements on left ` ` ` `*left = *left + 1; ` ` ` ` ` `// store the index number ` ` ` `position[num] = *(left); ` ` ` ` ` `// mark the element is on left ` ` ` `mark[num] = 1; ` `} ` ` ` `// function to perform query 2 ` `void` `performQueryTwo(` `int` `num, ` `int` `* left, ` `int` `* right, ` ` ` `int` `* position, ` `int` `* mark) ` `{ ` ` ` `// count number of elements on right ` ` ` `*right = *right + 1; ` ` ` ` ` `// store the index number ` ` ` `position[num] = *(right); ` ` ` ` ` `// mark the element is on right ` ` ` `mark[num] = 2; ` `} ` ` ` `// function to perform query 3 ` `void` `performQueryThree(` `int` `num, ` `int` `* left, ` `int` `* right, ` ` ` `int` `* position, ` `int` `* mark) ` `{ ` ` ` `int` `toright, toleft; ` ` ` ` ` `// if the element is on right ` ` ` `if` `(mark[num] == 2) { ` ` ` `toright = *right - position[num]; ` ` ` `toleft = position[num] - 1 + *left; ` ` ` `} ` ` ` ` ` `// if the element is on left ` ` ` `else` `if` `(mark[num] == 1) { ` ` ` `toleft = *left - position[num]; ` ` ` `toright = position[num] - 1 + *right; ` ` ` `} ` ` ` ` ` `// not present ` ` ` `else` `{ ` ` ` `cout << ` `"The number is not there\n"` `; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `cout << ` `"The number of elements to right of "` ` ` `<< num << ` `": "` `<< toright; ` ` ` ` ` `cout << ` `"\nThe number of elements to left of "` ` ` `<< num << ` `": "` `<< toleft; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `int` `left = 0, right = 0; ` ` ` ` ` `// hashing arrays ` ` ` `int` `position[MAXN] = { 0 }; ` ` ` `int` `mark[MAXN] = { 0 }; ` ` ` ` ` `int` `num = 3; ` ` ` ` ` `// query type-1 ` ` ` `performQueryOne(num, &left, &right, position, mark); ` ` ` ` ` `// query type-2 ` ` ` `num = 5; ` ` ` `performQueryTwo(num, &left, &right, position, mark); ` ` ` ` ` `// query type-2 ` ` ` `num = 2; ` ` ` `performQueryOne(num, &left, &right, position, mark); ` ` ` ` ` `// query type-2 ` ` ` `num = 4; ` ` ` `performQueryOne(num, &left, &right, position, mark); ` ` ` ` ` `// query type-3 ` ` ` `num = 3; ` ` ` `performQueryThree(num, &left, &right, position, mark); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Time Complexity:** O(1) for every query.

**Auxiliary Space:** O(MAXN), where MAXN is 10^{5}.

## Recommended Posts:

- Intersecting rectangle when bottom-left and top-right corners of two rectangles are given
- Count the number of elements which are greater than any of element on right side of an array
- Queries for number of distinct elements from a given index till last index in an array
- Sum of elements till the smallest index such that there are no even numbers to its right
- Queries to check whether all the elements can be made positive by flipping signs exactly K times
- Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements
- Number of strictly increasing Buildings from right with distinct Colors
- Minimum number of elements to be removed so that pairwise consecutive elements are same
- Choose X elements from A[] and Y elements from B[] which satisfy the given condition
- Smallest index such that there are no 0 or 1 to its right
- Generating numbers that are divisor of their right-rotations
- Index of the elements which are equal to the sum of all succeeding elements
- Number of permutations such that sum of elements at odd index and even index are equal
- Sum of elements in range L-R where first half and second half is filled with odd and even numbers
- Find the number of operations required to make all array elements Equal
- Minimum number of adjacent swaps for arranging similar elements together
- Minimum number of moves to make all elements equal
- Number of array elements derivable from D after performing certain operations
- Count Number of animals in a zoo from given number of head and legs
- Minimize the number of replacements to get a string with same number of 'a', 'b' and 'c' in it

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.