检查二进制数组是否可以在 K 位与 1 异或后变为回文
给定一个大小为N的二进制数组arr[]和一个整数K,任务是检查二进制数组是否可以转换为回文K次操作,其中在一次操作中,可以选择任何随机索引并将值存储在该索引将被其按位XOR(^)替换为 1。
例子:
Input: arr[] = { 1, 0, 0, 1, 1}, D = 0
Output: No
Explanation: As the array is not a palindrome, we must need some non-zero number of operations
to make it palindrome.
Input: arr[] = { 1, 0, 1, 1, 0, 0}, D = 1
Output: Yes
Explanation: Closest arrays which are palindrome : {0, 0, 1, 1, 0, 0} and {1, 0, 1, 1, 0, 1}, which is possible
using 1 such operations only.
方法:解决问题的方法基于以下思想:
The minimum number of operations required is the number of unequal elements equidistant from the mid of the array and to make them equal it takes 1 operation (from 1 to 0 or from 0 to 1).
There is need of 2 operations to change two equal elements and make them have the same value again using bitwise XOR.
要实现这个想法,请按照以下步骤操作:
- 初始化两个指向数组两端的指针。
- 从数组的两端遍历数组时,找出不相等的数组元素的数量。
- 如果该数字大于K ,则不可能以恰好 K 步到达目标。
- 如果这个数正好等于K ,那么就有可能使数组回文。
- 否则,如果数字小于K :
- 如果数组长度是奇数,那么任意数量的正 K 都可以,因为我们可以在中间索引处执行操作。
- 如果数组长度是偶数,为了让它保持回文,我们必须选择两个索引并对这些索引进行操作。所以剩余的K必须是偶数。
这是上述方法的代码:
C++
// C++ code for the approach
#include
using namespace std;
// Function to check wheater the array
// can be turned into palindrome after K
// number of operations
bool check_possibility(int* arr, int K)
{
// Store the length of the array in
// arr_length variable
int arr_length = sizeof(arr) / sizeof(
arr[0]);
// Initialize two pointers from left and
// right ends of the array
int i = 0;
int j = arr_length - 1;
// Keep iterating through the array from
// two ends until the two pointers cross
// each other to count the number
// of the different array items.
while (i < j) {
// If the two elements are unequal,
// deacrease the value of K by one
if (arr[i] != arr[j]) {
K--;
}
// Move the left pointer towards the
// right and right pointer towards the
// left
i++;
j--;
}
// The unequal items are more than K or K
// becomes less than zero, it is impossible
// to make it a palindrome with D operations.
if (K < 0) {
return false;
}
// If K has a non-negative value, we make the
// array a palindrome if it has an odd length
// the remaining value of K is odd as we have
// to choose two indices at a time to keep it
// as a palindrome.
else {
if ((arr_length % 2) != 0
|| (K % 2) == 0) {
return true;
}
else {
return false;
}
}
}
// Driver code
int main()
{
int arr[6] = { 1, 0, 1, 1, 0, 0 };
int K = 1;
// Function call
if (check_possibility(arr, K) == true) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
}
Java
// Java code for the approach
import java.io.*;
class GFG
{
// Function to check wheater the array
// can be turned into palindrome after K
// number of operations
public static boolean check_possibility(int arr[],
int K)
{
// Store the length of the array in
// arr_length variable
int arr_length = arr.length;
// Initialize two pointers from left and
// right ends of the array
int i = 0;
int j = arr_length - 1;
// Keep iterating through the array from
// two ends until the two pointers cross
// each other to count the number
// of the different array items.
while (i < j) {
// If the two elements are unequal,
// deacrease the value of K by one
if (arr[i] != arr[j]) {
K--;
}
// Move the left pointer towards the
// right and right pointer towards the
// left
i++;
j--;
}
// The unequal items are more than K or K
// becomes less than zero, it is impossible
// to make it a palindrome with D operations.
if (K < 0) {
return false;
}
// If K has a non-negative value, we make the
// array a palindrome if it has an odd length
// the remaining value of K is odd as we have
// to choose two indices at a time to keep it
// as a palindrome.
else {
if ((arr_length % 2) != 0 || (K % 2) == 0) {
return true;
}
else {
return false;
}
}
}
public static void main(String[] args)
{
int arr[] = { 1, 0, 1, 1, 0, 0 };
int K = 1;
// Function call
if (check_possibility(arr, K) == true) {
System.out.println("Yes");
}
else {
System.out.println("No");
}
}
}
// This code is contributed by Rohit Pradhan
Python3
# Python code for the approach
def check_possibility(arr, K):
# Store the length of the array in
# arr_length variable
arr_length = len(arr)
# Initialize two pointers from left and right
# ends of the array
i = 0
j = arr_length - 1
# Keep iterating through the array from two
# ends until the two pointers cross each
# other to count the number of the different
# array items.
while(i < j):
# If the two elements are unequal,
# deacrease the value of K by one
if(arr[i] != arr[j]):
K -= 1
# Move the left pointer towards the right
# and right pointer towards the left
i += 1
j -= 1
# The unequal items are more than K or
# K becomes less than zero, it is impossible
# to make it a palindrome with K operations.
if(K < 0):
return False
# If K has a non-negative value, we make the
# array a palindrome if it has an odd length
# the remaining value of K is odd as we have
# to choose two indices at a time
# to keep it as a palindrome
else:
if( (arr_length % 2)!= 0 or (K % 2)== 0):
return True
else:
return False
# Driver code
if __name__ == '__main__':
arr = [1, 0, 1, 1, 0, 0]
K = 1
if check_possibility(arr, K) == True :
print("Yes")
else:
print("No")
Javascript
// JavaScript code for the approach
// Function to check wheater the array
// can be turned into palindrome after K
// number of operations
function check_possibility(arr, K)
{
// Store the length of the array in
// arr_length variable
var arr_length = arr.length;
// Initialize two pointers from left and
// right ends of the array
var i = 0;
var j = arr_length - 1;
// Keep iterating through the array from
// two ends until the two pointers cross
// each other to count the number
// of the different array items.
while (i < j) {
// If the two elements are unequal,
// deacrease the value of K by one
if (arr[i] != arr[j]) {
K--;
}
// Move the left pointer towards the
// right and right pointer towards the
// left
i++;
j--;
}
// The unequal items are more than K or K
// becomes less than zero, it is impossible
// to make it a palindrome with D operations.
if (K < 0) {
return false;
}
// If K has a non-negative value, we make the
// array a palindrome if it has an odd length
// the remaining value of K is odd as we have
// to choose two indices at a time to keep it
// as a palindrome.
else {
if ((arr_length % 2) != 0 || (K % 2) == 0) {
return true;
}
else {
return false;
}
}
}
// Driver code
var arr = [ 1, 0, 1, 1, 0, 0 ];
var K = 1;
// Function call
if (check_possibility(arr, K) == true) {
console.log("Yes");
}
else {
console.log("No");
}
// This code is contributed by phasing17
Yes
时间复杂度: O(N)
辅助空间: O(1)