📌  相关文章
📜  重新排列给定的数组,使每个元素不等于相邻元素的平均值

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

重新排列给定的数组,使每个元素不等于相邻元素的平均值

给定一个由N个唯一整数组成的数组arr ,任务是重新排列数组,使得数组索引i处的元素不应该是相邻元素(即索引 i-1 和 i+1)的平均值。任何可能的重新排列都可以返回。

例子:

方法:解决这个问题的主要观察是对于 3 个数字 a、b 和 c 满足 b 不应该是 a 和 c 的均值的条件,[a, b, c] 一定不能排序。因此,可以通过以下步骤解决此问题:

  • 从 1 到 (N-1) 遍历数组
  • 检查是否 (arr[i – 1] + arr[i + 1]) / 2 == arr[i])
  • 如果满足条件,则交换元素 arr[i] 和 arr[i+1]

下面是上述方法的实现:

C++
// C++ code for above implementation
#include 
using namespace std;
 
// Function to rearrange the array
void Rearrange(int arr[], int N)
{
 
    // Iterating for array
    for (int i = 1; i < (N - 1); i++) {
 
        // Checking whether the element i
        // is mean of i-1 and i+1
        if ((arr[i - 1] + arr[i + 1]) / 2 == arr[i]) {
 
            // Rearrange by swapping arr[i] and arr[i+1]
            swap(arr[i], arr[i + 1]);
        }
    }
 
    // Printing the output array
    for (int i = 0; i < N; i++) {
        cout << arr[i] << " ";
    }
}
 
 
// Driver code
int main()
{
    int arr[] = { 6, 9, 12, 25, 50, 75 };
    int N = sizeof(arr) / sizeof(int);
 
    // calling the function
    Rearrange(arr, N);
    return 0;
}


Java
// Java program for the above approach
 
import java.io.*;
 
class GFG {
  // Function to rearrange the array
   
static void Rearrange(int arr[], int N)
{
   
    // Iterating for array
    for (int i = 1; i < (N - 1); i++) {
   
        // Checking whether the element i
        // is mean of i-1 and i+1
        if ((arr[i - 1] + arr[i + 1]) / 2 == arr[i]) {
   
            // Rearrange by swapping arr[i] and arr[i+1]
            
          int temp = arr[i];
        arr[i] = arr[i + 1];
        arr[i + 1] = temp;
        }
    }
   
    // Printing the output array
    for (int i = 0; i < N; i++) {
         System.out.print(arr[i] +" ");
    }
}
   
// Driver code
    public static void main (String[] args) {
      int arr[] = { 6, 9, 12, 25, 50, 75 };
    int N = arr.length;
   
    // calling the function
    Rearrange(arr, N);
    }
}
// This code is contributed by Potta Lokesh


C#
// C# program for the above approachusing System;class GFG {// Function to rearrange the arraystatic void Rearrange(int []arr, int N){// Iterating for arrayfor (int i = 1; i < (N – 1); i++) {// Checking whether the element i// is mean of i-1 and i+1if ((arr[i – 1] + arr[i + 1]) / 2 == arr[i]) {// Rearrange by swapping arr[i] and arr[i+1]int temp = arr[i];arr[i] = arr[i + 1];arr[i + 1] = temp;}}// Printing the output arrayfor (int i = 0; i < N; i++) {Console.Write(arr[i] +" ");}}// Driver codepublic static void Main (String[] args){int []arr = { 6, 9, 12, 25, 50, 75 };int N = arr.Length;// calling the functionRearrange(arr, N);}}// This code is contributed by shivanisinghss2110


Python3
# Python3 program for the above approach
 
# Function to rearrange the array
def Rearrange(arr, N) :
 
    # Iterating for array
    for i in range(1, N - 1) :
 
        # Checking whether the element i
        # is mean of i-1 and i+1
        if ((arr[i - 1] + arr[i + 1]) // 2 == arr[i]) :
 
            # Rearrange by swapping arr[i] and arr[i+1]
            arr[i], arr[i + 1] = arr[i + 1], arr[i];
 
    # Printing the output array
    for i in range(N) :
        print(arr[i],end= " " )
   
 
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 6, 9, 12, 25, 50, 75 ];
    N = len(arr);
 
    # calling the function
    Rearrange(arr, N);
     
    # This code is contributed by AnkThon


Javascript


输出
6 12 9 25 75 50 

时间复杂度: O(N)
辅助空间: O(1)