重新排列给定的 Array,使得提升到其索引的每个元素都是奇数
给定一个长度为N的数组arr ,任务是重新排列给定数组的元素,使得对于每个元素,它的按位 XOR 与它的索引是一个奇数值。如果无法重新排列,则返回-1 。
例子:
Input: arr[] = {1 2 4 3 5}
Output: 1 2 3 4 5
Explanation: In the above array:
for 1st element: value is 1 and index is 0 -> so 1 ^ 0 = 1, which is odd
for 2nd element: value is 2 and index is 1 -> so 2 ^ 1 = 3, which is odd
for 3rd element: value is 4 and index is 2 -> so 4 ^ 2 = 6, which is even -> rearranging will happen
for 4th element: value is 3 and index is 3 -> so 3 ^ 3 = 0, which is even -> rearranging will happen
for 5th element: value is 5 and index is 4 -> so 5 ^ 4 = 3, which is odd
So if we swap the positions of 4 and 3 as {1, 2, 3, 4, 5},
the XOR of 3^2 will become 1, and XOR of 4^3 will become 7, which are both odd.
Hence {1, 2, 3, 4, 5} is one of the possible rearrangements.
Input: arr[] = {1 2 7 3 5}
Output: -1
方法:解决这个问题的想法是基于按位异或运算符的属性,即:
- 两个奇数元素的异或总是偶数,
- 两个偶数元素的异或总是偶数,并且
- 奇数和偶数元素的异或总是奇数。
因此,为了根据需要重新排列数组,我们将所有偶数元素存储在奇数索引处,奇数元素存储在偶数索引处。
请按照以下步骤了解如何:
- 首先计算奇数和偶数索引数组有多少个,分别为 n/2 和 nn/2
- 然后计算数组有多少奇偶元素
- 分别存储数组的偶数和奇数元素
- 检查是否可以重新排列,即偶数元素的计数等于奇数索引,反之亦然。
- 如果不可能,则返回 -1。
- 如果可以重新排列,则在偶数索引处插入所有奇数元素,在奇数索引处插入偶数元素。
- 最后返回重新排列的数组。
下面是该方法的实现:
C++
// C++ program to Rearrange the array
// Such that A[i]^ i is odd
#include
using namespace std;
// Function to rearrange given array
vector rearrange(int arr[], int n)
{
vector ans;
int i;
// Count how many odd
// and even index array have
int oddIndex = n / 2,
evenIndex = n - oddIndex;
// Count how many odd
// and even elements array have
int oddElement = 0, evenElement = 0;
// Store the even and odd elements
// of the array separately
vector odd, even;
for (i = 0; i < n; i++)
if (arr[i] % 2) {
oddElement++;
odd.push_back(arr[i]);
}
else {
evenElement++;
even.push_back(arr[i]);
}
// To make XOR of each element
// with its index as odd,
// we have to place each even element
// at an odd index and vice versa
// Therefore check if rearrangement
// is possible or not
if (oddElement != evenIndex
|| oddIndex != evenElement) {
ans.push_back(-1);
}
// If the rearrangement is possible
else {
// Insert odd elements at even indices
// and even elements at odd indices
int j = 0, k = 0;
for (int i = 0; i < n; i++)
if (i % 2)
ans.push_back(even[j++]);
else
ans.push_back(odd[k++]);
}
// return the rearranged array
return ans;
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 4, 3, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
vector res = rearrange(arr, n);
for (auto i : res)
cout << i << " ";
return 0;
}
Java
// Java program to Rearrange the array
// Such that A[i]^ i is odd
import java.io.*;
import java.util.ArrayList;
class GFG {
// Function to rearrange given array
static void rearrange(int arr[], int n)
{
ArrayList ans = new ArrayList<>();
// Count how many odd
// and even index array have
int oddIndex = n / 2, evenIndex = n - oddIndex;
// Count how many odd
// and even elements array have
int oddElement = 0, evenElement = 0;
// Store the even and odd elements
// of the array separately
ArrayList odd = new ArrayList<>();
ArrayList even = new ArrayList<>();
for (int i = 0; i < n; i++)
if (arr[i] % 2 == 1) {
oddElement++;
odd.add(arr[i]);
}
else {
evenElement++;
even.add(arr[i]);
}
// To make XOR of each element
// with its index as odd,
// we have to place each even element
// at an odd index and vice versa
// Therefore check if rearrangement
// is possible or not
if (oddElement != evenIndex
|| oddIndex != evenElement) {
ans.add(-1);
}
// If the rearrangement is possible
else {
// Insert odd elements at even indices
// and even elements at odd indices
int j = 0, k = 0;
for (int i = 0; i < n; i++)
if (i % 2 == 1)
ans.add(even.get(j++));
else
ans.add(odd.get(k++));
}
// print the rearranged array
for(int i = 0; i < ans.size(); i++){
System.out.print(ans.get(i) + " ");
}
}
// Driver Code
public static void main (String[] args) {
int[] arr = { 1, 2, 4, 3, 5 };
int n = arr.length;
rearrange(arr, n);
}
}
// This code is contributed by hrithikgarg03188.
Python3
# python3 program to Rearrange the array
# Such that A[i]^ i is odd
# Function to rearrange given array
def rearrange(arr, n):
ans = []
i = 0
# Count how many odd
# and even index array have
oddIndex = n // 2
evenIndex = n - oddIndex
# Count how many odd
# and even elements array have
oddElement, evenElement = 0, 0
# Store the even and odd elements
# of the array separately
odd, even = [], []
for i in range(0, n):
if (arr[i] % 2):
oddElement += 1
odd.append(arr[i])
else:
evenElement += 1
even.append(arr[i])
# To make XOR of each element
# with its index as odd,
# we have to place each even element
# at an odd index and vice versa
# Therefore check if rearrangement
# is possible or not
if (oddElement != evenIndex
or oddIndex != evenElement):
ans.append(-1)
# If the rearrangement is possible
else:
# Insert odd elements at even indices
# and even elements at odd indices
j, k = 0, 0
for i in range(0, n):
if (i % 2):
ans.append(even[j])
j += 1
else:
ans.append(odd[k])
k += 1
# return the rearranged array
return ans
# Driver Code
if __name__ == "__main__":
arr = [1, 2, 4, 3, 5]
n = len(arr)
res = rearrange(arr, n)
for i in res:
print(i, end=" ")
# This code is contributed by rakeshsahni
C#
// C# program to Rearrange the array
// Such that A[i]^ i is odd
using System;
using System.Collections;
class GFG {
// Function to rearrange given array
static ArrayList rearrange(int[] arr, int n)
{
ArrayList ans = new ArrayList();
// Count how many odd
// and even index array have
int oddIndex = n / 2, evenIndex = n - oddIndex;
// Count how many odd
// and even elements array have
int oddElement = 0, evenElement = 0;
// Store the even and odd elements
// of the array separately
ArrayList odd = new ArrayList();
ArrayList even = new ArrayList();
for (int i = 0; i < n; i++)
if (arr[i] % 2 == 1) {
oddElement++;
odd.Add(arr[i]);
}
else {
evenElement++;
even.Add(arr[i]);
}
// To make XOR of each element
// with its index as odd,
// we have to place each even element
// at an odd index and vice versa
// Therefore check if rearrangement
// is possible or not
if (oddElement != evenIndex
|| oddIndex != evenElement) {
ans.Add(-1);
}
// If the rearrangement is possible
else {
// Insert odd elements at even indices
// and even elements at odd indices
int j = 0, k = 0;
for (int i = 0; i < n; i++)
if (i % 2 == 1)
ans.Add(even[j++]);
else
ans.Add(odd[k++]);
}
// return the rearranged array
return ans;
}
// Driver Code
public static void Main()
{
int[] arr = { 1, 2, 4, 3, 5 };
int n = arr.Length;
ArrayList res = rearrange(arr, n);
for (int i = 0; i < res.Count; i++) {
Console.Write(res[i] + " ");
}
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
1 2 3 4 5
时间复杂度: O(N)。
辅助空间: O(N)。