重新排列数组以最小化奇数和偶数索引元素的平方和的差异
给定一个大小为N (8 的倍数)的数组arr[] ,其中数组中的值将在 [a, (a+8*N) -1] 范围内( a可以是任何正整数),任务是以某种方式重新排列数组,使得奇数索引处的平方和与偶数索引处的元素的平方和之间的差异是所有可能的重新排列中的最小值。
注意:如果有多个重新排列,则返回其中任何一个。
例子:
Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8}
Output: 1 2 4 3 7 8 6 5
Explanation: The difference is 0 as 1 + 42 + 72 + 62 = 102 = 22 + 32 + 82 + 52
Input: arr[] = { 9, 11, 12, 15, 16, 13, 10, 14}
Output: 9 10 12 11 15 16 14 13
Explanation: The difference is 0 as 92 + 122 + 152 + 142 = 102 + 112 + 162 + 132 = 646
方法:这个问题可以根据以下数学观察来解决:
For any positive integer S, ( S )2 + ( S+3 )2 – 4 = ( S+1 )2 + ( S+2 )2. As N is a multiple of 8 so it can be divided into N/8 groups where the difference of sum of squares of elements at odd and even indices for each group is 0.
For the first four elements keep the sum of squares at odd indices greater and four the next four just the opposite to keep the sum of squares of even indices more. So this group of 8 elements will have difference 0. As the similar is done for all N/8 groups the overall difference will be 0.
The sequence of each group can be like: S, S+1, S+3, S+2, S+6, S+7, S+5, S+4
请按照以下步骤解决此问题:
- 将数组分成大小为 8 的组。
- 根据观察结果排列每组中的元素。
- 返回重新排列的数组。
下面是上述方法的实现:
C++
// C++ code to implement the approach
#include
using namespace std;
// Function to find
// maximum element of the array
int maximum(int arr[], int size)
{
int ma = INT_MIN;
for (int i = 0; i < size; i++) {
ma = max(ma, arr[i]);
}
return ma;
}
// Function to find
// minimum element of the array
int minimum(int arr[], int size)
{
int mi = INT_MAX;
for (int i = 0; i < size; i++) {
mi = min(mi, arr[i]);
}
return mi;
}
// Function to print the array
void print_min(int arr[], int size)
{
int low = minimum(arr, size);
int high = maximum(arr, size);
// using the fact that
// s^2 + (s+3)^2 = (s+1)^2 + (s+2)^2 + 4.
for (int i = 0; i < size; i += 4) {
// Making the difference +4
// for the odd indices
if (i % 8 == 0) {
arr[i] = low;
arr[i + 2] = low + 3;
arr[i + 1] = low + 1;
arr[i + 3] = low + 2;
}
// Making the difference -4 for
// odd indices +4 - 4 = 0 (balanced)
else {
arr[i] = low + 2;
arr[i + 2] = low + 1;
arr[i + 1] = low + 3;
arr[i + 3] = low;
}
low += 4;
}
// Printing the array
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
}
// Driver code
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
int N = sizeof(arr) / (sizeof(int));
// Function call
print_min(arr, N);
return 0;
}
Java
// JAVA code to implement the approach
import java.util.*;
class GFG
{
// Function to find
// maximum element of the array
public static int maximum(int arr[], int size)
{
int ma = Integer.MIN_VALUE;
for (int i = 0; i < size; i++) {
ma = Math.max(ma, arr[i]);
}
return ma;
}
// Function to find
// minimum element of the array
public static int minimum(int arr[], int size)
{
int mi = Integer.MAX_VALUE;
for (int i = 0; i < size; i++) {
mi = Math.min(mi, arr[i]);
}
return mi;
}
// Function to print the array
public static void print_min(int arr[], int size)
{
int low = minimum(arr, size);
int high = maximum(arr, size);
// using the fact that
// s^2 + (s+3)^2 = (s+1)^2 + (s+2)^2 + 4.
for (int i = 0; i < size; i += 4) {
// Making the difference +4
// for the odd indices
if (i % 8 == 0) {
arr[i] = low;
arr[i + 2] = low + 3;
arr[i + 1] = low + 1;
arr[i + 3] = low + 2;
}
// Making the difference -4 for
// odd indices +4 - 4 = 0 (balanced)
else {
arr[i] = low + 2;
arr[i + 2] = low + 1;
arr[i + 1] = low + 3;
arr[i + 3] = low;
}
low += 4;
}
// Printing the array
for (int i = 0; i < size; i++) {
System.out.print(arr[i] + " ");
}
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
int N = arr.length;
// Function call
print_min(arr, N);
}
}
// This code is contributed by Taranpreet
Python3
# Python code to implement the approach
INT_MIN = -2147483647 - 1
INT_MAX = 2147483647
# Function to find
# maximum element of the array
def maximum(arr, size):
ma = INT_MIN
for i in range(size):
ma = max(ma, arr[i])
return ma
# Function to find
# minimum element of the array
def minimum(arr, size):
mi = INT_MAX
for i in range(size):
mi = min(mi, arr[i])
return mi
# Function to print the array
def print_min(arr, size):
low = minimum(arr, size)
high = maximum(arr, size)
# using the fact that
# s^2 + (s+3)^2 = (s+1)^2 + (s+2)^2 + 4.
for i in range(0,size,4):
# Making the difference +4
# for the odd indices
if (i % 8 == 0):
arr[i] = low
arr[i + 2] = low + 3
arr[i + 1] = low + 1
arr[i + 3] = low + 2
# Making the difference -4 for
# odd indices +4 - 4 = 0 (balanced)
else:
arr[i] = low + 2
arr[i + 2] = low + 1
arr[i + 1] = low + 3
arr[i + 3] = low
low += 4
# Printing the array
for i in range(size):
print(arr[i],end=" ")
# Driver code
arr = [1, 2, 3, 4, 5, 6, 7, 8]
N = len(arr)
# Function call
print_min(arr, N)
# This code is contributed by shinjanpatra
C#
// C# code to implement the approach
using System;
class GFG {
// Function to find
// maximum element of the array
static int maximum(int[] arr, int size)
{
int ma = Int32.MinValue;
for (int i = 0; i < size; i++) {
ma = Math.Max(ma, arr[i]);
}
return ma;
}
// Function to find
// minimum element of the array
static int minimum(int[] arr, int size)
{
int mi = Int32.MaxValue;
for (int i = 0; i < size; i++) {
mi = Math.Min(mi, arr[i]);
}
return mi;
}
// Function to print the array
static void print_min(int[] arr, int size)
{
int low = minimum(arr, size);
int high = maximum(arr, size);
// using the fact that
// s^2 + (s+3)^2 = (s+1)^2 + (s+2)^2 + 4.
for (int i = 0; i < size; i += 4) {
// Making the difference +4
// for the odd indices
if (i % 8 == 0) {
arr[i] = low;
arr[i + 2] = low + 3;
arr[i + 1] = low + 1;
arr[i + 3] = low + 2;
}
// Making the difference -4 for
// odd indices +4 - 4 = 0 (balanced)
else {
arr[i] = low + 2;
arr[i + 2] = low + 1;
arr[i + 1] = low + 3;
arr[i + 3] = low;
}
low += 4;
}
// Printing the array
for (int i = 0; i < size; i++) {
Console.Write(arr[i] + " ");
}
}
// Driver code
public static void Main()
{
int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8 };
int N = arr.Length;
// Function call
print_min(arr, N);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
1 2 4 3 7 8 6 5
时间复杂度:O(N)
辅助空间:O(1)