📌  相关文章
📜  重新排列数组,使左半部分的总和不等于右半部分的总和

📅  最后修改于: 2021-04-22 08:00:34             🧑  作者: Mango

给定长度为偶数的数组arr [] ,任务是检查是否可以对数组元素重新排序,以使左半部分的总和不等于数组右半部分的总和。如果可能,则以重新排序的顺序打印“是” ,否则打印“否”

例子:

方法:如果数组中的所有元素都相等,那么我们就不能对数组元素进行重新排序以满足给定的条件。否则,在给定数组的排序序列中,数组左半部分和右半部分的总和将始终不相等。

下面是上述方法的实现:

C
// C program for the above approach
#include 
#include 
  
// A comparator function used by qsort 
int compare(const void * a, const void * b) 
{ 
    return (*(int*)a - *(int*)b); 
}
  
// Function to print the required
// reordering of array if possible
void printArr(int arr[], int n)
{
      
    // Sort the array in increasing order
    qsort(arr, n, sizeof(int), compare);
  
    // If all elements are equal, then
    // it is not possible
    if (arr[0] == arr[n - 1])
    {
        printf("No\n");
    }
  
    // Else print the sorted array arr[]
    else 
    {
        printf("Yes\n");
        for(int i = 0; i < n; i++)
        {
            printf("%d ", arr[i]);
        }
    }
}
  
// Driver Code
int main()
{
      
    // Given array
    int arr[] = { 1, 2, 2, 1, 3, 1 };
  
    int N = sizeof(arr) / sizeof(arr[0]);
  
    // Function call
    printArr(arr, N);
    return 0;
}
  
// This code is contributed by equbalzeeshan


C++
// C++ program for the above approach
#include 
using namespace std;
  
// Function to print the required
// reordering of array if possible
void printArr(int arr[], int n)
{
    // Sort the array in increasing order
    sort(arr, arr + n);
  
    // If all elements are equal, then
    // it is not possible
    if (arr[0] == arr[n - 1]) {
        cout << "No" << endl;
    }
  
    // Else print the sorted array arr[]
    else {
  
        cout << "Yes" << endl;
        for (int i = 0; i < n; i++) {
            cout << arr[i] << " ";
        }
    }
}
  
// Driver Code
int main()
{
    // Given array
    int arr[] = { 1, 2, 2, 1, 3, 1 };
  
    int N = sizeof(arr) / sizeof(arr[0]);
  
    // Function Call
    printArr(arr, N);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
  
class GFG{
      
// Function to print the required
// reordering of array if possible
public static void printArr(int[] arr, int n) 
{
      
    // Sort the array in increasing order
    Arrays.sort(arr);
  
    // If all elements are equal, then
    // it is not possible
    if (arr[0] == arr[n - 1])
    {
        System.out.println("No");
    }
  
    // Else print the sorted array arr[]
    else
    {
        System.out.println("Yes");
        for(int i = 0; i < n; i++)
        {
            System.out.print(arr[i] + " ");
        }
    }
}
  
// Driver code
public static void main(String[] args)
{
      
    // Given array 
    int arr[] = { 1, 2, 2, 1, 3, 1 }; 
  
    int N = arr.length; 
  
    // Function call 
    printArr(arr, N); 
}
} 
  
// This code is contributed by equbalzeeshan


Python3
# Python3 program for the above approach
  
# Function to print the required
# reordering of the array if possible
def printArr(arr, n):
  
    # Sort the array in increasing
    # order
    arr.sort()
  
    # If all elements are equal, 
    # then it is not possible
    if(arr[0] == arr[n - 1]):
        print("No")
  
    # Else print the sorted 
    # array arr[]
    else:
        print("Yes")
        for i in range(n):
            print(arr[i], end = " ")
        print()
  
# Driver Code
if __name__ == '__main__':
  
    # Given array
    arr = [ 1, 2, 2, 1, 3, 1 ]
    N = len(arr)
  
    # Function Call
    printArr(arr, N)
  
# This code is contributed by Shivam Singh


C#
// C# code for the above approach
using System;
  
class GFG{
  
// Function to print the required
// reordering of array if possible
public static void printArr(int[] arr, int n) 
{
      
    // Sort the array in increasing order
    Array.Sort(arr);
  
    // If all elements are equal, then
    // it is not possible
    if (arr[0] == arr[n - 1])
    {
        Console.Write("No\n");
    }
  
    // Else print the sorted array arr[]
    else
    {
        Console.Write("Yes\n");
        for(int i = 0; i < n; i++)
        {
            Console.Write(arr[i] + " ");
        }
    }
}
  
// Driver code
public static void Main()
{ 
      
    // Given array 
    int[] arr = new int [6]{ 1, 2, 2, 1, 3, 1 }; 
  
    int N = arr.Length; 
  
    // Function call 
    printArr(arr, N); 
}
}
  
// This code is contributed by equbalzeeshan


输出:
Yes
1 1 1 2 2 3

时间复杂度: O(N * log(N))