You are given an array consisting of integers. Your task is to find the longest subsequence S, such that XOR of any two elements in is non-zero.
You are required to print the size of and the array of indices that are chosen for . In the 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.
Example:
Input: n = 5, arr = {3, 2, 3, 1, 2}
Output:
3 1 2 4
Approach:
C++
#include <bits/stdc++.h>using namespace std;vector<int> xorSubsequence(int n, vector<int> arr){set<int> st;vector<int> res;for (int i = 0; i < n; i++){if (st.find(arr[i]) == st.end()){st.insert(arr[i]);res.push_back(i + 1);}}return res;}int main(){int n = 5;vector<int> arr = {3, 2, 3, 1, 2};vector<int> res = xorSubsequence(n, arr);cout << res.size() << "\n";for (int i = 0; i < res.size(); i++)cout << res[i] << " ";cout << "\n";return 0;}
No comments:
Post a Comment