📌  相关文章
📜  重新排列数组以最小化奇数和偶数索引元素的平方和的差异

📅  最后修改于: 2022-05-13 01:56:08.684000             🧑  作者: Mango

重新排列数组以最小化奇数和偶数索引元素的平方和的差异

给定一个大小为N (8 的倍数)的数组arr[] ,其中数组中的值将在 [a, (a+8*N) -1] 范围内( a可以是任何正整数),任务是以某种方式重新排列数组,使得奇数索引处的平方和与偶数索引处的元素的平方和之间的差异是所有可能的重新排列中的最小值。

注意:如果有多个重新排列,则返回其中任何一个。

例子

方法:这个问题可以根据以下数学观察来解决:

请按照以下步骤解决此问题:

  • 将数组分成大小为 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)