Rearrange array to make product of prefix sum array non zero
Last Updated :
11 May, 2021
Given an array, arr[ ] of size N, the task is to rearrange the given array such that the product of all the elements of its prefix sum array is not equal to 0. If it is not possible to rearrange the array that satisfies the given condition, then print -1.
Examples:
Input: arr[] = {1, -1, -2, 3}
Output: 3 1 -1 -2
Explanation:
Prefix sum after rearranging the given array to {3, 1, -1, -2} are {3, 4, 3, 1} and product all the elements of its prefix sum array = (3 * 4 * 3 * 1) = 36.
Therefore, the required array is {3, 1, -1, -2}
Input: arr = {1, 1, -1, -1}
Output: -1
Approach: The idea is to sort the given array either in ascending order or descending order so that any element of its prefix sum not equal to 0. Follow the steps below to solve the problem:
- Calculate the sum of elements of the given array, say totalSum.
- If totalSum = 0 then print -1.
- If totalSum > 0 then print the given array in decreasing order so that any elements of its prefix sum not equal to 0.
- If totalSum < 0 then print the given array in ascending order so that any elements of its prefix sum not equal to 0.
Below is the implementation of the above approach:
C++
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
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<int>());
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
JavaScript
<script>
// javascript program to implement
// the above approach
// Function to print array elements
function printArr(arr, N)
{
for(var i = 0; i < N; i++)
{
document.write(arr[i] + " ");
}
}
// Function to rearrange array
// that satisfies the given condition
function rearrangeArr(arr, N)
{
// Stores sum of elements
// of the given array
var totalSum = 0;
// Calculate totalSum
for(var i = 0; i < N; i++)
{
totalSum += arr[i];
}
// If the totalSum is equal to 0
if (totalSum == 0)
{
// No possible way to
// rearrange array
document.write("-1" + "<br>");
}
// If totalSum exceeds 0
else if (totalSum > 0)
{
// Rearrange the array
// in descending order
arr.sort(compare);
arr = reverse(arr);
printArr(arr, N);
}
// Otherwise
else
{
// Rearrange the array
// in ascending order
arr.sort(compare);
printArr(arr, N);
}
}
function compare(a, b) {
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
}
function reverse(a)
{
var 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
var arr = [ 1, -1, -2, 3 ] ;
var N = arr.length;
rearrangeArr(arr, N);
</script>
Time Complexity: O(N logN)
Space Complexity: O(1)
Similar Reads
Rearrange array elements to maximize the sum of MEX of all prefix arrays Given an array arr[] of size N, the task is to rearrange the array elements such that the sum of MEX of all prefix arrays is the maximum possible. Note: MEX of a sequence is the minimum non-negative number not present in the sequence. Examples: Input: arr[] = {2, 0, 1}Output: 0, 1, 2Explanation:Sum
7 min read
Minimum steps to make sum and the product of all elements of array non-zero Given an array arr of N integers, the task is to find the minimum steps in which the sum and product of all elements of the array can be made non-zero. In one step any element of the array can be incremented by 1.Examples: Input: N = 4, arr[] = {0, 1, 2, 3} Output: 1 Explanation: As product of all e
7 min read
Rearrange given array to obtain positive prefix sums at exactly X indices Given an array arr[] consisting of N integers whose absolute values are distinct and an integer K, the task is to find such an arrangement of the given array by the following operations, such that it has positive prefix sum at exactly K places: Choose two integers i, j and swap the values of the ele
8 min read
Rearrange array to make sum of all subarrays starting from first index non-zero Given an array arr[] consisting of N integers, the task is to rearrange the array such that sum of all subarrays starting from the first index of the array is non-zero. If it is not possible to generate such arrangement, then print "-1". Examples: Input: arr[] = {-1, 1, -2, 3}Output: {-1, -2, 1, 3}E
12 min read
Minimize increments or decrements required to make sum and product of array elements non-zero Given an array arr[] of N integers, the task is to count the minimum number of increment or decrement operations required on the array such that the sum and product of all the elements of the array arr[] are non-zero. Examples: Input: arr[] = {-1, -1, 0, 0}Output: 2Explanation: Perform the following
6 min read