Check if Array can be split into subarrays such that XOR of length of Longest Decreasing Subsequences of those subarrays is 0
Given an array of integers arr[] of size N, the task is to check whether arr[] can be split into different subarrays such that on taking the XOR of lengths of LDS (Longest decreasing subsequences) of all the subarrays is equal to 0. Print ‘YES‘ if it is possible to split else print ‘NO‘.
Examples:
Input: arr[] = {1, 0, 3, 4, 5}
Output: YES
Explanation: {1}, {0}, {3}, {4, 5} length of LDS of subarrays: 1, 1, 1, 1, XOR of lengths = 0. So answer is Yes.
Input: arr[] = {5, 4, 3}
Output: NO
Approach: The XOR operation of even number of 1’s is 0. So if the array length is even then each element can be considered as LDS of size 1 which makes XOR of even 1’s equal to 0 and for odd length arrays to have even LDS’s with 1’s any subarray of size 2 can be considered with LDS i.e. the subarray should be strictly increasing so the LDS will be 1. Follow the steps below to solve the problem:
- Initialize a variable found as false.
- If N is even print ‘YES’ and return.
- Else, Iterate over the range (0, N – 1] using the variable i and perform the following tasks:
- Check if there is consecutive increasing elements by arr[i]>arr[i-1]
- If arr[i]>arr[i-1] make found as true and come out of the loop
- If found is true print “YES”, else print “NO”
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void xor_of_lds( int arr[], int n)
{
if (n % 2 == 0) {
cout << "YES" << endl;
return ;
}
else {
bool found = 0;
for ( int i = 1; i < n; i++) {
if (arr[i] > arr[i - 1]) {
found = 1;
break ;
}
}
if (found == 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
int main()
{
int arr[] = { 1, 0, 3, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
xor_of_lds(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void xor_of_lds( int arr[], int n)
{
if (n % 2 == 0 ) {
System.out.print( "YES" + "\n" );
return ;
}
else {
boolean found = false ;
for ( int i = 1 ; i < n; i++) {
if (arr[i] > arr[i - 1 ]) {
found = true ;
break ;
}
}
if (found == true )
System.out.print( "YES" + "\n" );
else
System.out.print( "NO" + "\n" );
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 0 , 3 , 4 , 5 };
int N = arr.length;
xor_of_lds(arr, N);
}
}
|
Python3
def xor_of_lds (arr, n) :
if (n % 2 = = 0 ):
print ( "YES" )
return
else :
found = 0
for i in range ( 1 , n):
if (arr[i] > arr[i - 1 ]):
found = 1
break
if (found = = 1 ):
print ( "YES" )
else :
print ( "NO" )
arr = [ 1 , 0 , 3 , 4 , 5 ]
N = len (arr)
xor_of_lds(arr, N)
|
C#
using System;
class GFG
{
static void xor_of_lds( int []arr, int n)
{
if (n % 2 == 0) {
Console.Write( "YES" + "\n" );
return ;
}
else {
bool found = false ;
for ( int i = 1; i < n; i++) {
if (arr[i] > arr[i - 1]) {
found = true ;
break ;
}
}
if (found == true )
Console.Write( "YES" + "\n" );
else
Console.Write( "NO" + "\n" );
}
}
public static void Main()
{
int []arr = { 1, 0, 3, 4, 5 };
int N = arr.Length;
xor_of_lds(arr, N);
}
}
|
Javascript
<script>
const xor_of_lds = (arr, n) => {
if (n % 2 == 0) {
document.write( "YES<br/>" );
return ;
}
else {
let found = 0;
for (let i = 1; i < n; i++) {
if (arr[i] > arr[i - 1]) {
found = 1;
break ;
}
}
if (found == 1)
document.write( "YES<br/>" );
else
document.write( "NO<br/>" );
}
}
let arr = [1, 0, 3, 4, 5];
let N = arr.length;
xor_of_lds(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
17 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...