给定一个大小为N的数组arr[ ] ,任务是重新排列给定数组,使其前缀和数组的所有元素的乘积不等于0 。如果无法重新排列满足给定条件的数组,则打印-1 。
例子:
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
方法:想法是按升序或降序对给定数组进行排序,使其前缀总和的任何元素不等于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
Javascript
输出:
3 1 -1 -2
时间复杂度: O(N logN)
空间复杂度: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。