A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
Given a list S that initially contains a single value 0. Below are the Q queries of the following types:
The task is to print all the element in the list in increasing order after performing the given Q queries.
Examples:
Input:_ Q[][] = { {0, 6}, {0, 3}, {0, 2}, {1, 4}, {1, 5} }_
Output:_ 1 2 3 7_
Explanation:
[0] (initial value)
[0 6] (add 6 to list)
[0 6 3] (add 3 to list)
[0 6 3 2] (add 2 to list)
[4 2 7 6] (XOR each element by 4)
[1 7 2 3] (XOR each element by 5)
Thus sorted order after performing queries is [1 2 3 7]
Input:_ Q[][]= {{0, 2}, {1, 3}, {0, 5} }_
Output:_ 1 3 5_
Explanation:
[0] (initial value)
[0 2] (add 2 to list)
[3 1] (XOR each element by 3)
[3 1 5] (add 5 to list)
Thus sorted order after performing queries is [1 3 5]
Naive Approach: The simplest approach to solve the problem is:
Time Complexity:_ O(N*Q), where N is the length of the list and Q is the number of queries_
Auxiliary Space:_ O(1)_
Efficient Approach: It is much easier to process the numbers in reverse order by keeping track of the cumulative Bitwise XOR working from right to left. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
// Java program for the above approach
**import**
java.util.*;
**class**
GFG {
// Function to return required list
// after performing all the queries
**static**
List<Integer> ConstructList(``**int**``[][] Q)
{
// Store cumulative Bitwise XOR
**int**
xor =
0``;
// Initialize final list to return
List<Integer> ans =
**new**
ArrayList<>();
// Perform each query
**for**
(``**int**
i = Q.length -
1``; i >=
0``; i--) {
**if**
(Q[i][``0``] ==
0``)
ans.add(Q[i][``1``] ^ xor);
**else**
xor ^= Q[i][``1``];
}
// The initial value of 0
ans.add(xor);
// Sort the list
Collections.sort(ans);
// Return final list
**return**
ans;
}
// Driver Code
**public**
**static**
**void**
main(String[] args)
{
// Given Queries
**int**``[][] Q = {
{
0``,
6
}, {
0``,
3
}, {
0``,
2
},
{
1``,
4
}, {
1``,
5
}
};
// Function Call
System.out.println(ConstructList(Q));
}
}
Output:
[1, 2, 3, 7]
Time Complexity:_ O(Q * log(Q))_, where Q is the number of queries.
Auxiliary Space:_ O(N)_, where N is the number of elements in the resultant list.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.