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++
#include <bits/stdc++.h>
using namespace std;
void printArr( int arr[], int N)
{
for ( int i = 0; i < N; i++) {
cout << arr[i] << " " ;
}
}
void rearrangeArr( int arr[], int N)
{
int totalSum = 0;
for ( int i = 0; i < N; i++) {
totalSum += arr[i];
}
if (totalSum == 0) {
cout << "-1" << endl;
}
else if (totalSum > 0) {
sort(arr, arr + N,
greater< int >());
printArr(arr, N);
}
else {
sort(arr, arr + N);
printArr(arr, N);
}
}
int main()
{
int arr[] = { 1, -1, -2, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
rearrangeArr(arr, N);
}
|
Java
import java.util.*;
class GFG{
static void printArr( int arr[], int N)
{
for ( int i = 0 ; i < N; i++)
{
System.out.print(arr[i] + " " );
}
}
static void rearrangeArr( int arr[], int N)
{
int totalSum = 0 ;
for ( int i = 0 ; i < N; i++)
{
totalSum += arr[i];
}
if (totalSum == 0 )
{
System.out.print( "-1" + "\n" );
}
else if (totalSum > 0 )
{
Arrays.sort(arr);
arr = reverse(arr);
printArr(arr, N);
}
else
{
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;
}
public static void main(String[] args)
{
int arr[] = { 1 , - 1 , - 2 , 3 };
int N = arr.length;
rearrangeArr(arr, N);
}
}
|
Python3
def rearrangeArr(arr, N):
totalSum = 0
for i in range (N):
totalSum + = arr[i]
if (totalSum = = 0 ):
print ( - 1 )
elif (totalSum > 0 ):
arr.sort(reverse = True )
print ( * arr, sep = ' ' )
else :
arr.sort()
print ( * arr, sep = ' ' )
arr = [ 1 , - 1 , - 2 , 3 ]
N = len (arr)
rearrangeArr(arr, N);
|
C#
using System;
class GFG{
static void printArr( int []arr, int N)
{
for ( int i = 0; i < N; i++)
{
Console.Write(arr[i] + " " );
}
}
static void rearrangeArr( int []arr, int N)
{
int totalSum = 0;
for ( int i = 0; i < N; i++)
{
totalSum += arr[i];
}
if (totalSum == 0)
{
Console.Write( "-1" + "\n" );
}
else if (totalSum > 0)
{
Array.Sort(arr);
arr = reverse(arr);
printArr(arr, N);
}
else
{
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;
}
public static void Main(String[] args)
{
int []arr = { 1, -1, -2, 3 };
int N = arr.Length;
rearrangeArr(arr, N);
}
}
|
Javascript
<script>
function printArr(arr, N)
{
for ( var i = 0; i < N; i++)
{
document.write(arr[i] + " " );
}
}
function rearrangeArr(arr, N)
{
var totalSum = 0;
for ( var i = 0; i < N; i++)
{
totalSum += arr[i];
}
if (totalSum == 0)
{
document.write( "-1" + "<br>" );
}
else if (totalSum > 0)
{
arr.sort(compare);
arr = reverse(arr);
printArr(arr, N);
}
else
{
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;
}
var arr = [ 1, -1, -2, 3 ] ;
var N = arr.length;
rearrangeArr(arr, N);
</script>
|
Time Complexity: O(N logN)
Space Complexity: O(1)
Share your thoughts in the comments
Please Login to comment...