将数组的每个元素减少到一半,保留总和为零
给定一个由N个整数组成的数组arr[] ,总元素和为零。任务是将每个元素减少到一半,以使总和保持为零。对于数组中的每个奇数元素X ,它可以减少为(X + 1) / 2或(X – 1) / 2 。
例子:
Input: arr[] = {-7, 14, -7}
Output: -4 7 -3
-4 + 7 -3 = 0
Input: arr[] = {-14, 14}
Output: -7 7
方法:所有偶数元素都可以除以 2,但对于奇数元素,它们必须交替减少为(X + 1) / 2和(X – 1) / 2以保留原始和(即 0)最后的数组。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
// Function to reduce every
// element to it's half such that
// the total sum remain zero
void half(int arr[], int n)
{
int i;
// Flag to switch between alternating
// odd numbers in the array
int flag = 0;
// For every element of the array
for (i = 0; i < n; i++)
{
// If its even then reduce it to half
if (arr[i] % 2 == 0 )
cout << arr[i] / 2 << " ";
// If its odd
else
{
// Reduce the odd elements
// alternatively
if (flag == 0)
{
cout << arr[i] / 2 - 1 << " ";
// Switch flag
flag = 1;
}
else
{
int q = arr[i] / 2;
cout<
Java
// Java implementation of the above approach
class GFG
{
// Function to reduce every
// element to it's half such that
// the total sum remain zero
static void half(int arr[], int n)
{
int i;
// Flag to switch between alternating
// odd numbers in the array
int flag = 0;
// For every element of the array
for (i = 0; i < n; i++)
{
// If its even then reduce it to half
if (arr[i] % 2 == 0 )
System.out.print(arr[i] / 2 + " ");
// If its odd
else
{
// Reduce the odd elements
// alternatively
if (flag == 0)
{
System.out.print(arr[i] / 2 - 1 + " ");
// Switch flag
flag = 1;
}
else
{
int q = arr[i] / 2;
System.out.print(q + " ");
// Switch flag
flag = 0;
}
}
}
}
// Driver code
public static void main (String[] args)
{
int arr[] = {-7, 14, -7};
int len = arr.length;
half(arr, len) ;
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the approach
# Function to reduce every
# element to it's half such that
# the total sum remain zero
def half(arr, n) :
# Flag to switch between alternating
# odd numbers in the array
flag = 0
# For every element of the array
for i in range(n):
# If its even then reduce it to half
if arr[i] % 2 == 0 :
print(arr[i]//2, end =" ")
# If its odd
else :
# Reduce the odd elements
# alternatively
if flag == 0:
print(arr[i]//2, end =" ")
# Switch flag
flag = 1
else :
q = arr[i]//2
q+= 1
print(q, end =" ")
# Switch flag
flag = 0
# Driver code
arr = [-7, 14, -7]
half(arr, len(arr))
C#
// C# implementation of the above approach
using System;
class GFG
{
// Function to reduce every
// element to it's half such that
// the total sum remain zero
static void half(int []arr, int n)
{
int i;
// Flag to switch between alternating
// odd numbers in the array
int flag = 0;
// For every element of the array
for (i = 0; i < n; i++)
{
// If its even then reduce it to half
if (arr[i] % 2 == 0 )
Console.Write(arr[i] / 2 + " ");
// If its odd
else
{
// Reduce the odd elements
// alternatively
if (flag == 0)
{
Console.Write(arr[i] / 2 - 1 + " ");
// Switch flag
flag = 1;
}
else
{
int q = arr[i] / 2;
Console.Write(q + " ");
// Switch flag
flag = 0;
}
}
}
}
// Driver code
public static void Main ()
{
int [] arr = {-7, 14, -7};
int len = arr.Length;
half(arr, len) ;
}
}
// This code is contributed by mohit kumar 29
Javascript
输出:
-4 7 -3