XOR subsequences
Practice
3.2 (4 votes)
Set
Algorithms
Basics of greedy algorithms
Greedy algorithms
Problem
87% Success 1718 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code
You are given an array \(A\) consisting of \(N\) integers. Your task is to find the longest subsequence S, such that XOR of any two elements in \(S\) is non-zero.
You are required to print the size of \(S (L)\) and the array of indices that are chosen for \(S\). In case of multiple such arrays, choose the lexicographically smallest array.
Example: Let array A be [7, 9, 7], the maximum length of subsequence can be 2. There are two ways to choose a subsequence, [7, 9] and [9, 7]. The indices selected in the first case are [1, 2] while in the second they are [2, 3] so we need to choose [1, 2] as it is minimal.
Input format
- The first line contains an integer \(T\) denoting the number of test cases.
- The first line of each test case contains an integer \(N\) denoting the number of elements in array \(A\).
- The second line of each test case contains \(N\) space-separated integers of array \(A\).
Output format
For each test case:
- The first line must contain the maximum size (\(L\)) of \(S\) that can be chosen as a subsequence.
- The second line must contain space-separated integers denoting the lexicographically minimal array of indices that can be chosen.
Constraints
- \(1 \leq T \leq 20000\)
- \(1 \leq N \leq 200000\)
- \(1 \leq A_i \leq 10^9\)
- Sum of \(N\) over all test cases will not exceed 200000.
Submissions
Please login to view your submissions
Similar Problems
Points:20
16 votes
Tags:
AlgorithmsBasics of Greedy AlgorithmsGreedy Algorithms
Points:20
8 votes
Tags:
AlgorithmsBasics of Greedy AlgorithmsC++Greedy Algorithms
Points:20
5 votes
Tags:
Greedy AlgorithmsAlgorithmsBasics of Greedy Algorithms
Editorial