检查 Array 是否可以拆分为子数组,使得这些子数组的最长递减子序列的长度的 XOR 为 0
给定一个大小为N的整数数组arr[] ,任务是检查是否可以将arr[]拆分为不同的子数组,使得所有子数组的 LDS(最长递减子序列)长度的 XOR 等于0 .如果可以拆分则打印' YES ',否则打印' NO '。
例子:
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
方法:偶数个1 的XOR 操作为0。因此,如果数组长度为偶数,则每个元素都可以视为大小为1的 LDS,这使得偶数1 的XOR 等于0 ,并且对于奇数长度的数组具有偶数 LDS 1 的任何大小为2的子数组都可以与 LDS 一起考虑,即子数组应该严格增加,因此 LDS 将为1 。请按照以下步骤解决问题:
- 初始化一个发现为假的变量。
- 如果N甚至打印'YES'并返回。
- 否则,使用变量i迭代范围(0, N – 1]并执行以下任务:
- 通过arr[i]>arr[i-1]检查是否有连续增加的元素
- 如果arr[i]>arr[i-1] make发现为真并退出循环
- 如果找到为真打印“YES”,否则打印“NO”
下面是上述方法的实现。
C++
// C++ code for the above approach
#include
using namespace std;
// Function to find XOR of LDS's
// of subarrays
void xor_of_lds(int arr[], int n)
{
// If length is even each element
// can be considered as lds of length
// 1 which makes even 1's and xor is 0
if (n % 2 == 0) {
cout << "YES" << endl;
return;
}
else {
// For odd length we need to find
// even subarray of length 2 which
// is strictly increasing so that it
// makes a subarray with lds=1
bool found = 0;
for (int i = 1; i < n; i++) {
// Check if there are 2
// consecutive increasing elements
if (arr[i] > arr[i - 1]) {
found = 1;
break;
}
}
if (found == 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
// Driver Code
int main()
{
// Initializing array of arr
int arr[] = { 1, 0, 3, 4, 5 };
int N = sizeof(arr) / sizeof(arr[0]);
// Call the function
xor_of_lds(arr, N);
return 0;
}
Java
// Java code for the above approach
import java.util.*;
class GFG{
// Function to find XOR of LDS's
// of subarrays
static void xor_of_lds(int arr[], int n)
{
// If length is even each element
// can be considered as lds of length
// 1 which makes even 1's and xor is 0
if (n % 2 == 0) {
System.out.print("YES" +"\n");
return;
}
else {
// For odd length we need to find
// even subarray of length 2 which
// is strictly increasing so that it
// makes a subarray with lds=1
boolean found = false;
for (int i = 1; i < n; i++) {
// Check if there are 2
// consecutive increasing elements
if (arr[i] > arr[i - 1]) {
found = true;
break;
}
}
if (found == true)
System.out.print("YES" +"\n");
else
System.out.print("NO" +"\n");
}
}
// Driver Code
public static void main(String[] args)
{
// Initializing array of arr
int arr[] = { 1, 0, 3, 4, 5 };
int N = arr.length;
// Call the function
xor_of_lds(arr, N);
}
}
// This code is contributed by shikhasingrajput
Python3
# Python code for the above approach
# Function to find XOR of LDS's
# of subarrays
def xor_of_lds (arr, n) :
# If length is even each element
# can be considered as lds of length
# 1 which makes even 1's and xor is 0
if (n % 2 == 0):
print("YES")
return
else:
# For odd length we need to find
# even subarray of length 2 which
# is strictly increasing so that it
# makes a subarray with lds=1
found = 0
for i in range(1, n):
# Check if there are 2
# consecutive increasing elements
if (arr[i] > arr[i - 1]):
found = 1
break
if (found == 1):
print("YES")
else:
print("NO")
# Driver Code
# Initializing array of arr
arr = [1, 0, 3, 4, 5]
N = len(arr)
# Call the function
xor_of_lds(arr, N)
# This code is contributed by Saurabh Jaiswal
C#
// C# code for the above approach
using System;
class GFG
{
// Function to find XOR of LDS's
// of subarrays
static void xor_of_lds(int []arr, int n)
{
// If length is even each element
// can be considered as lds of length
// 1 which makes even 1's and xor is 0
if (n % 2 == 0) {
Console.Write("YES" + "\n");
return;
}
else {
// For odd length we need to find
// even subarray of length 2 which
// is strictly increasing so that it
// makes a subarray with lds=1
bool found = false;
for (int i = 1; i < n; i++) {
// Check if there are 2
// consecutive increasing elements
if (arr[i] > arr[i - 1]) {
found = true;
break;
}
}
if (found == true)
Console.Write("YES" +"\n");
else
Console.Write("NO" +"\n");
}
}
// Driver Code
public static void Main()
{
// Initializing array of arr
int []arr = { 1, 0, 3, 4, 5 };
int N = arr.Length;
// Call the function
xor_of_lds(arr, N);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
YES
时间复杂度: O(N)
辅助空间: O(1)