Given a sequence **arr** of **N** positive integers, the task is to find the length of the longest subsequence such that xor of adjacent integers in the subsequence must be **non-decreasing**.

**Examples:**

Input:N = 8, arr = {1, 100, 3, 64, 0, 5, 2, 15}

Output:6

The subsequence of maximum length is {1, 3, 0, 5, 2, 15}

with XOR of adjacent elements as {2, 3, 5, 7, 13}

Input:N = 3, arr = {1, 7, 10}

Output:3

The subsequence of maximum length is {1, 3, 7}

with XOR of adjacent elements as {2, 4}.

**Approach:**

- This problem can be solved using dynamic programming where
**dp[i]**will store the length of the longest valid subsequence that ends at index**i**. - First, store the xor of all the pairs of elements i.e.
**arr[i] ^ arr[j]**and the pair**(i, j)**also and then sort them according to the value of xor as they need to be non-decreasing. - Now if the pair
**(i, j)**is considered then the length of the longest subsequence that ends at**j**will be**max(dp[j], 1 + dp[i])**. In this way, calculate the maximum possible value of**dp[]**array for each position and then take the maximum of them.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the length of the longest ` `// subsequence such that the XOR of adjacent ` `// elements in the subsequence must ` `// be non-decreasing ` `int` `LongestXorSubsequence(` `int` `arr[], ` `int` `n) ` `{ ` ` ` ` ` `vector<pair<` `int` `, pair<` `int` `, ` `int` `> > > v; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `for` `(` `int` `j = i + 1; j < n; j++) { ` ` ` ` ` `// Computing xor of all the pairs ` ` ` `// of elements and store them ` ` ` `// along with the pair (i, j) ` ` ` `v.push_back(make_pair(arr[i] ^ arr[j], ` ` ` `make_pair(i, j))); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Sort all possible xor values ` ` ` `sort(v.begin(), v.end()); ` ` ` ` ` `int` `dp[n]; ` ` ` ` ` `// Initialize the dp array ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `dp[i] = 1; ` ` ` `} ` ` ` ` ` `// Calculating the dp array ` ` ` `// for each possible position ` ` ` `// and calculating the max length ` ` ` `// that ends at a particular index ` ` ` `for` `(` `auto` `i : v) { ` ` ` `dp[i.second.second] ` ` ` `= max(dp[i.second.second], ` ` ` `1 + dp[i.second.first]); ` ` ` `} ` ` ` ` ` `int` `ans = 1; ` ` ` ` ` `// Taking maximum of all position ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `ans = max(ans, dp[i]); ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `int` `arr[] = { 2, 12, 6, 7, 13, 14, 8, 6 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `cout << LongestXorSubsequence(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to find the length of the longest ` `# subsequence such that the XOR of adjacent ` `# elements in the subsequence must ` `# be non-decreasing ` `def` `LongestXorSubsequence(arr, n): ` ` ` ` ` `v ` `=` `[] ` ` ` ` ` `for` `i ` `in` `range` `(` `0` `, n): ` ` ` `for` `j ` `in` `range` `(i ` `+` `1` `, n): ` ` ` ` ` `# Computing xor of all the pairs ` ` ` `# of elements and store them ` ` ` `# along with the pair (i, j) ` ` ` `v.append([(arr[i] ^ arr[j]), (i, j)]) ` ` ` ` ` `# v.push_back(make_pair(arr[i] ^ arr[j], make_pair(i, j))) ` ` ` ` ` `# Sort all possible xor values ` ` ` `v.sort() ` ` ` ` ` `# Initialize the dp array ` ` ` `dp ` `=` `[` `1` `for` `x ` `in` `range` `(` `88` `)] ` ` ` ` ` `# Calculating the dp array ` ` ` `# for each possible position ` ` ` `# and calculating the max length ` ` ` `# that ends at a particular index ` ` ` `for` `a, b ` `in` `v: ` ` ` `dp[b[` `1` `]] ` `=` `max` `(dp[b[` `1` `]], ` `1` `+` `dp[b[` `0` `]]) ` ` ` ` ` `ans ` `=` `1` ` ` ` ` `# Taking maximum of all position ` ` ` `for` `i ` `in` `range` `(` `0` `, n): ` ` ` `ans ` `=` `max` `(ans, dp[i]) ` ` ` ` ` `return` `ans ` ` ` `# Driver code ` `arr ` `=` `[ ` `2` `, ` `12` `, ` `6` `, ` `7` `, ` `13` `, ` `14` `, ` `8` `, ` `6` `] ` `n ` `=` `len` `(arr) ` `print` `(LongestXorSubsequence(arr, n)) ` ` ` `# This code is contributed by Sanjit Prasad ` |

*chevron_right*

*filter_none*

**Output:**

5

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.

## Recommended Posts:

- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Longest subsequence such that adjacent elements have at least one common digit
- Longest Increasing Subsequence using Longest Common Subsequence Algorithm
- Length of longest subsequence whose XOR value is odd
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Maximum length subsequence with difference between adjacent elements as either 0 or 1
- Longest subsequence with different adjacent characters
- Longest subarray such that adjacent elements have at least one common digit | Set 1
- Longest subarray such that adjacent elements have at least one common digit | Set - 2
- Length of the longest subsequence consisting of distinct elements
- Length of longest subsequence in an Array having all elements as Nude Numbers
- Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements
- Length of Smallest Subsequence such that sum of elements is greater than equal to K
- Choose X such that (A xor X) + (B xor X) is minimized
- Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P.
- Find the original Array using XOR values of all adjacent elements
- Kth array element after M replacements of array elements by XOR of adjacent pairs
- Find length of longest subsequence of one string which is substring of another string

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.