Reduce every element of the array to it's half retaining the sum zero
Last Updated :
23 Jul, 2022
Given an array arr[] of N integers with total element sum equal to zero. The task is to reduce every element to it's half such that the total sum remain zero. For every odd element X in the array, it could be reduced to either(X + 1) / 2 or (X - 1) / 2.
Examples:
Input: arr[] = {-7, 14, -7}
Output: -4 7 -3
-4 + 7 -3 = 0
Input: arr[] = {-14, 14}
Output: -7 7
Approach: All the even elements could be divided by 2 but for odd elements, they have to be alternatively reduced to (X + 1) / 2 and (X - 1) / 2 in order to retain the original sum (i.e. 0) in the final array.
Below is the implementation of the above approach:
C++
// C++ implementation of the above approach
#include<bits/stdc++.h>
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<<q <<" ";
// Switch flag
flag = 0;
}
}
}
}
// Driver code
int main ()
{
int arr[] = {-7, 14, -7};
int len = sizeof(arr)/sizeof(arr[0]);
half(arr, len) ;
return 0;
}
// This code is contributed by Rajput-Ji
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
<script>
// Javascript implementation of the above approach
// Function to reduce every
// element to it's half such that
// the total sum remain zero
function half(arr, n)
{
let i;
// Flag to switch between alternating
// odd numbers in the array
let 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 )
document.write(arr[i] / 2 + " ");
// If its odd
else
{
// Reduce the odd elements
// alternatively
if (flag == 0)
{
document.write(Math.ceil(arr[i] / 2) - 1 + " ");
// Switch flag
flag = 1;
}
else
{
let q = Math.ceil(arr[i] / 2);
document.write(q + " ");
// Switch flag
flag = 0;
}
}
}
}
// Driver code
let arr = [-7, 14, -7];
let len = arr.length;
half(arr, len) ;
// This code is contributed by _saurabh_jaiswal
</script>
Time Complexity : O(n) ,as we are traversing once on the array.
Space Complexity : O(1) ,as we are not using any extra space.
Similar Reads
Minimize the sum calculated by repeatedly removing any two elements and inserting their sum to the Array Given n elements, you can remove any two elements from the list, note their sum, and add the sum to the list. Repeat these steps while there is more than a single element in the list. The task is to minimize the sum of these chosen sums in the end.Examples: Input: arr[] = {1, 4, 7, 10} Output: 39 Ex
7 min read
Element equal to the sum of all the remaining elements Given an array of N positive elements. The task is to find an element which is equal to the sum of all elements of array except itself. Examples: Input: arr[] = {1, 2, 3, 6} Output: 6 6 is the element which is equal to the sum of all remaining elements i.e. 1 + 2+ 3 = 6 Input: arr[] = {2, 2, 2, 2} O
4 min read
Check if the array has an element which is equal to sum of all the remaining elements Given an array of N elements, the task is to check if the array has an element that is equal to the sum of all the remaining elements. Examples: Input: a[] = {5, 1, 2, 2} Output: Yes we can write 5=(1+2+2) Input: a[] = {2, 1, 2, 4, 3} Output: No Approach: Suppose that the total elements in the array
10 min read
Minimize shifting half of each element to either sides to reduce Array Given an array arr[] of size N, the task is to find minimum number of steps required to reduce all Array elements to 0 except the first and last, where in each step: Choose any index i (0<i<N-1) and decrement the element at that index (arr[i]) by 2Then increment any two elements, arr[j], where
8 min read
Check if sum of the given array can be reduced to 0 by reducing array elements by K Given an array arr[] consisting of N integers and an integer K, the task is to check if the sum of the array can be reduced to 0 by subtracting array elements by K any number of times. Examples: Input: arr[ ]= {-3, 2, -1, 5, 1}, K=2Output: "Yes"Explanation: Sum of the array is 4. Therefore, decreasi
4 min read