📌  相关文章
📜  重新排列数组以使前缀和数组的乘积不为零

📅  最后修改于: 2021-05-17 17:21:38             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是重新排列给定数组,以使其前缀和数组的所有元素的乘积不等于0 。如果无法重新排列满足给定条件的数组,则打印-1

例子:

方法:想法是按照给定数组升序或降序排序,以使其前缀总和的任何元素都不等于0 。请按照以下步骤解决问题:

  • 计算给定数组的元素总和,例如totalSum
  • 如果totalSum = 0,则打印-1
  • 如果totalSum> 0,则以降序打印给定数组,以便其前缀的任何元素之和不等于0。
  • 如果totalSum <0,则以升序打印给定数组,以便其前缀的任何元素之和不等于0。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to print array elements
void printArr(int arr[], int N)
{
    for (int i = 0; i < N; i++) {
        cout << arr[i] << " ";
    }
}
 
// Function to rearrange array
// that satisfies the given condition
void rearrangeArr(int arr[], int N)
{
    // Stores sum of elements
    // of the given array
    int totalSum = 0;
 
    // Calculate totalSum
    for (int i = 0; i < N; i++) {
        totalSum += arr[i];
    }
 
    // If the totalSum is equal to 0
    if (totalSum == 0) {
 
        // No possible way to
        // rearrange array
        cout << "-1" << endl;
    }
 
    // If totalSum exceeds 0
    else if (totalSum > 0) {
 
        // Rearrange the array
        // in descending order
        sort(arr, arr + N,
             greater());
        printArr(arr, N);
    }
 
    // Otherwise
    else {
 
        // Rearrange the array
        // in ascending order
        sort(arr, arr + N);
        printArr(arr, N);
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 1, -1, -2, 3 };
    int N = sizeof(arr) / sizeof(arr[0]);
    rearrangeArr(arr, N);
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to print array elements
static void printArr(int arr[], int N)
{
    for(int i = 0; i < N; i++)
    {
        System.out.print(arr[i] + " ");
    }
}
 
// Function to rearrange array
// that satisfies the given condition
static void rearrangeArr(int arr[], int N)
{
     
    // Stores sum of elements
    // of the given array
    int totalSum = 0;
 
    // Calculate totalSum
    for(int i = 0; i < N; i++)
    {
        totalSum += arr[i];
    }
 
    // If the totalSum is equal to 0
    if (totalSum == 0)
    {
         
        // No possible way to
        // rearrange array
        System.out.print("-1" + "\n");
    }
 
    // If totalSum exceeds 0
    else if (totalSum > 0)
    {
         
        // Rearrange the array
        // in descending order
        Arrays.sort(arr);
        arr = reverse(arr);
        printArr(arr, N);
    }
     
    // Otherwise
    else
    {
         
        // Rearrange the array
        // in ascending order
        Arrays.sort(arr);
        printArr(arr, N);
    }
}
 
static int[] reverse(int a[])
{
    int i, n = a.length, t;
    for(i = 0; i < n / 2; i++)
    {
        t = a[i];
        a[i] = a[n - i - 1];
        a[n - i - 1] = t;
    }
    return a;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, -1, -2, 3 };
    int N = arr.length;
     
    rearrangeArr(arr, N);
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program to implement
# the above approach
 
# Function to rearrange array
# that satisfies the given condition
def rearrangeArr(arr, N):
     
    # Stores sum of elements
    # of the given array
    totalSum = 0
     
    # Calculate totalSum
    for i in range(N):
        totalSum += arr[i]
 
    # If the totalSum is equal to 0
    if (totalSum == 0):
         
        # No possible way to
        # rearrange array
        print(-1)
 
    # If totalSum exceeds 0
    elif (totalSum > 0):
         
        # Rearrange the array
        # in descending order
        arr.sort(reverse = True)
        print(*arr, sep = ' ')
         
    # Otherwise
    else:
 
        # Rearrange the array
        # in ascending order
        arr.sort()
        print(*arr, sep = ' ')
 
# Driver Code
arr = [ 1, -1, -2, 3 ]
N = len(arr)
 
rearrangeArr(arr, N);
 
# This code is contributed by avanitrachhadiya2155


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to print array elements
static void printArr(int []arr, int N)
{
    for(int i = 0; i < N; i++)
    {
        Console.Write(arr[i] + " ");
    }
}
 
// Function to rearrange array
// that satisfies the given condition
static void rearrangeArr(int []arr, int N)
{
     
    // Stores sum of elements
    // of the given array
    int totalSum = 0;
 
    // Calculate totalSum
    for(int i = 0; i < N; i++)
    {
        totalSum += arr[i];
    }
 
    // If the totalSum is equal to 0
    if (totalSum == 0)
    {
         
        // No possible way to
        // rearrange array
        Console.Write("-1" + "\n");
    }
 
    // If totalSum exceeds 0
    else if (totalSum > 0)
    {
         
        // Rearrange the array
        // in descending order
        Array.Sort(arr);
        arr = reverse(arr);
        printArr(arr, N);
    }
     
    // Otherwise
    else
    {
         
        // Rearrange the array
        // in ascending order
        Array.Sort(arr);
        printArr(arr, N);
    }
}
 
static int[] reverse(int []a)
{
    int i, n = a.Length, t;
    for(i = 0; i < n / 2; i++)
    {
        t = a[i];
        a[i] = a[n - i - 1];
        a[n - i - 1] = t;
    }
    return a;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 1, -1, -2, 3 };
    int N = arr.Length;
     
    rearrangeArr(arr, N);
}
}
 
// This code is contributed by Princi Singh


输出:
3 1 -1 -2










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