给定一个大小为N的数组arr[] ,任务是找到所有的三元组(i, j, k) ,以便用它们的按位异或值替换三元组的元素,即替换 arr[i], arr[j] , arr[k] 与(arr[i] ^ arr[j] ^ arr[k])使所有数组元素相等。如果存在多个解决方案,请打印其中任何一个。否则,打印-1 。
例子:
Input: arr[] = { 4, 2, 1, 7, 2 }
Output: { (0, 1, 2), (2, 3, 4), (0, 1, 4) }
Explanation:
Selecting a triplet (0, 1, 2) and replacing them with arr[0] ^ arr[1] ^ arr[2] modifies arr[] to { 7, 7, 7, 7, 2 }
Selecting a triplet (2, 3, 4) and replacing them with arr[2] ^ arr[3] ^ arr[4] modifies arr[] to { 7, 7, 2, 2, 2 }
Selecting a triplet (0, 1, 4) and replacing them with arr[0] ^ arr[1] ^ arr[2] modifies arr[] to { 2, 2, 2, 2, 2 }
Input: arr[] = { 1, 3, 2, 2 }
Output: -1
方法:该问题可以基于以下观察来解决:
x ^ X ^ Y = Y
X ^ Y ^ Y = X
If any two elements of a triplet are equal, then replacing all the elements of the triplet with their Bitwise XOR makes all elements of the triplet equal to the third element of the triplet.
请按照以下步骤解决问题:
- 选择形式为 { (0, 1, 2) , (2, 3, 4) …} 的三元组使 { (arr[0], arr[1]) , (arr[2], arr [3]) … } 相等。
- 根据以上观察,选择 { (0, 1, N – 1) , (2, 3, N -1) , … } 形式的三元组使所有数组元素等于数组的最后一个元素。
- 最后,打印三元组。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find triplets such that
// replacing them with their XOR make
// all array elements equal
void checkXOR(int arr[], int N)
{
// If N is even
if (N % 2 == 0) {
// Calculate xor of
// array elements
int xro = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
// Update xor
xro ^= arr[i];
}
// If xor is not equal to 0
if (xro != 0) {
cout << -1 << endl;
return;
}
// Selecting the triplets such that
// elements of the pairs (arr[0], arr[1]),
// (arr[2], arr[3])... can be made equal
for (int i = 0; i < N - 3; i += 2) {
cout << i << " " << i + 1
<< " " << i + 2 << endl;
}
// Selecting the triplets such that
// all array elements can be made
// equal to arr[N - 1]
for (int i = 0; i < N - 3; i += 2) {
cout << i << " " << i + 1
<< " " << N - 1 << endl;
}
}
else {
// Selecting the triplets such that
// elements of the pairs (arr[0], arr[1]),
// (arr[2], arr[3])... can be made equal
for (int i = 0; i < N - 2; i += 2) {
cout << i << " " << i + 1 << " "
<< i + 2 << endl;
}
// Selecting the triplets such that
// all array elements can be made
// equal to arr[N - 1]
for (int i = 0; i < N - 2; i += 2) {
cout << i << " " << i + 1
<< " " << N - 1 << endl;
}
}
}
// Driver Code
int main()
{
// Given array
int arr[] = { 4, 2, 1, 7, 2 };
// Size of array
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
checkXOR(arr, N);
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to find triplets such that
// replacing them with their XOR make
// all array elements equal
static void checkXOR(int arr[], int N)
{
// If N is even
if (N % 2 == 0)
{
// Calculate xor of
// array elements
int xro = 0;
// Traverse the array
for(int i = 0; i < N; i++)
{
// Update xor
xro ^= arr[i];
}
// If xor is not equal to 0
if (xro != 0)
{
System.out.println(-1);
return;
}
// Selecting the triplets such that
// elements of the pairs (arr[0], arr[1]),
// (arr[2], arr[3])... can be made equal
for(int i = 0; i < N - 3; i += 2)
{
System.out.println(i + " " + (i + 1) +
" " + (i + 2));
}
// Selecting the triplets such that
// all array elements can be made
// equal to arr[N - 1]
for(int i = 0; i < N - 3; i += 2)
{
System.out.println(i + " " + (i + 1) +
" " + (N - 1));
}
}
else
{
// Selecting the triplets such that
// elements of the pairs (arr[0], arr[1]),
// (arr[2], arr[3])... can be made equal
for(int i = 0; i < N - 2; i += 2)
{
System.out.println(i + " " + (i + 1) +
" " + (i + 2));
}
// Selecting the triplets such that
// all array elements can be made
// equal to arr[N - 1]
for(int i = 0; i < N - 2; i += 2)
{
System.out.println(i + " " + (i + 1) +
" " + (N - 1));
}
}
}
// Driver code
public static void main(String[] args)
{
// Given array
int arr[] = { 4, 2, 1, 7, 2 };
// Size of array
int N = arr.length;
// Function call
checkXOR(arr, N);
}
}
// This code is contributed by susmitakundugoaldanga
Python3
# Python program to implement
# the above approach
# Function to find triplets such that
# replacing them with their XOR make
# all array elements equal
def checkXOR(arr, N):
# If N is even
if (N % 2 == 0):
# Calculate xor of
# array elements
xro = 0;
# Traverse the array
for i in range(N):
# Update xor
xro ^= arr[i];
# If xor is not equal to 0
if (xro != 0):
print(-1);
return;
# Selecting the triplets such that
# elements of the pairs (arr[0], arr[1]),
# (arr[2], arr[3])... can be made equal
for i in range(0, N - 3, 2):
print(i, " ", (i + 1), " ", (i + 2), end=" ");
# Selecting the triplets such that
# all array elements can be made
# equal to arr[N - 1]
for i in range(0, N - 3, 2):
print(i, " ", (i + 1), " ", (N - 1), end=" ");
else:
# Selecting the triplets such that
# elements of the pairs (arr[0], arr[1]),
# (arr[2], arr[3])... can be made equal
for i in range(0, N - 2, 2):
print(i, " ", (i + 1), " ", (i + 2));
# Selecting the triplets such that
# all array elements can be made
# equal to arr[N - 1]
for i in range(0, N - 2, 2):
print(i, " ", (i + 1), " ", (N - 1));
# Driver code
if __name__ == '__main__':
# Given array
arr = [4, 2, 1, 7, 2];
# Size of array
N = len(arr);
# Function call
checkXOR(arr, N);
# This code is contributed by 29AjayKumar
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to find triplets such that
// replacing them with their XOR make
// all array elements equal
static void checkXOR(int[] arr, int N)
{
// If N is even
if (N % 2 == 0)
{
// Calculate xor of
// array elements
int xro = 0;
// Traverse the array
for(int i = 0; i < N; i++)
{
// Update xor
xro ^= arr[i];
}
// If xor is not equal to 0
if (xro != 0)
{
Console.WriteLine(-1);
return;
}
// Selecting the triplets such that
// elements of the pairs (arr[0], arr[1]),
// (arr[2], arr[3])... can be made equal
for(int i = 0; i < N - 3; i += 2)
{
Console.WriteLine(i + " " + (i + 1) +
" " + (i + 2));
}
// Selecting the triplets such that
// all array elements can be made
// equal to arr[N - 1]
for(int i = 0; i < N - 3; i += 2)
{
Console.WriteLine(i + " " + (i + 1) +
" " + (N - 1));
}
}
else
{
// Selecting the triplets such that
// elements of the pairs (arr[0], arr[1]),
// (arr[2], arr[3])... can be made equal
for(int i = 0; i < N - 2; i += 2)
{
Console.WriteLine(i + " " + (i + 1) +
" " + (i + 2));
}
// Selecting the triplets such that
// all array elements can be made
// equal to arr[N - 1]
for(int i = 0; i < N - 2; i += 2)
{
Console.WriteLine(i + " " + (i + 1) +
" " + (N - 1));
}
}
}
// Driver code
public static void Main()
{
// Given array
int[] arr = { 4, 2, 1, 7, 2 };
// Size of array
int N = arr.Length;
// Function call
checkXOR(arr, N);
}
}
// This code is contributed by sanjoy_62
Javascript
0 1 2
2 3 4
0 1 4
2 3 4
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live