# Make the Array sum 0 by using either ceil or floor on each element

• Difficulty Level : Medium
• Last Updated : 18 Oct, 2021

Given an array arr[] consisting of N floating point integers, the task is to modify the array by performing ceil() or floor() on every array element such that the sum of array elements is close to 0.

Examples:

Input: arr[] = {6.455, -1.24, -3.87, 2.434, -4.647}
Output: {6, -2, -3, 3, -4}
Explanation:
Perform the floor operation on array elements at indices {0, 1} and ceil operation at indices {2, 3, 4} modifies the array to {6, -2, -3, 3, -4} whose sum of elements is 0, which is closest to 0.

Input: arr[] = {-12.42, 9.264, 24.24, -13.04, 4.0, -9.66, -2.99}
Output: {-13, 9, 24, -13, 4, -9, -2}

Approach: The given problem can be solved by finding the sum of ceil() value of all array elements and if the sum of the array is positive then find the ceil of that number of elements such that the becomes closest to the value 0. Follow the steps below to solve the given problem:

• Initialize a variable, say sum to 0 that stores the sum of array elements.
• Initialize an array, say A[] that stores the updated array elements.
• Traverse the array arr[] and find the sum of ceil() of the array elements and update the value of A[i] to the value ceil(arr[i]).
• If the value of sum is positive then find the floor of some array element to reduced the sum to closest to 0 and the count of such array element is given by the minimum of the sum and N.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach`   `#include ` `using` `namespace` `std;`   `// Function to modify the array element` `// such that the sum is close to 0` `void` `setSumtoZero(``double` `arr[], ``int` `N)` `{` `    ``// Stores the modified elements` `    ``int` `A[N];`   `    ``// Stores the sum of array element` `    ``int` `sum = 0;`   `    ``// Stores minimum size of the array` `    ``int` `m = INT_MIN;`   `    ``// Traverse the array and find the` `    ``// sum` `    ``for` `(``int` `i = 0; i < N; i++) {` `        ``sum += ``ceil``(arr[i]);` `        ``A[i] = ``ceil``(arr[i]);` `    ``}`   `    ``// If sum is positive` `    ``if` `(sum > 0) {`   `        ``// Find the minimum number of` `        ``// elements that must be changed` `        ``m = min(sum, N);`   `        ``// Iterate until M elements are` `        ``// modified or the array end` `        ``for` `(``int` `i = 0; i < N && m > 0; i++) {`   `            ``// Update the current array` `            ``// elements to its floor` `            ``A[i] = ``floor``(arr[i]);` `            ``if` `(A[i] != ``floor``(arr[i]))` `                ``m--;` `        ``}` `    ``}`   `    ``// Print the resultant array` `    ``for` `(``int` `i = 0; i < N; i++) {` `        ``cout << A[i] << ``" "``;` `    ``}` `}`   `// Driver Code` `int` `main()` `{` `    ``double` `arr[] = { -2, -2, 4.5 };` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);` `    ``setSumtoZero(arr, N);`   `    ``return` `0;` `}`

## Java

 `// Java program for the above approach` `import` `java.util.*;`   `class` `GFG{`   `// Function to modify the array element` `// such that the sum is close to 0` `static` `void` `setSumtoZero(``double` `arr[], ``int` `N)` `{` `  `  `    ``// Stores the modified elements` `    ``int` `[]A = ``new` `int``[N];`   `    ``// Stores the sum of array element` `    ``int` `sum = ``0``;`   `    ``// Stores minimum size of the array` `    ``int` `m = Integer.MIN_VALUE;`   `    ``// Traverse the array and find the` `    ``// sum` `    ``for` `(``int` `i = ``0``; i < N; i++) {` `        ``sum += Math.ceil(arr[i]);` `        ``A[i] = (``int``) Math.ceil(arr[i]);` `    ``}`   `    ``// If sum is positive` `    ``if` `(sum > ``0``) {`   `        ``// Find the minimum number of` `        ``// elements that must be changed` `        ``m = Math.min(sum, N);`   `        ``// Iterate until M elements are` `        ``// modified or the array end` `        ``for` `(``int` `i = ``0``; i < N && m > ``0``; i++) {`   `            ``// Update the current array` `            ``// elements to its floor` `            ``A[i] = (``int``) Math.floor(arr[i]);` `            ``if` `(A[i] != Math.floor(arr[i]))` `                ``m--;` `        ``}` `    ``}`   `    ``// Print the resultant array` `    ``for` `(``int` `i = ``0``; i < N; i++) {` `        ``System.out.print(A[i]+ ``" "``);` `    ``}` `}`   `// Driver Code` `public` `static` `void` `main(String[] args)` `{` `    ``double` `arr[] = { -``2``, -``2``, ``4.5` `};` `    ``int` `N = arr.length;` `    ``setSumtoZero(arr, N);`   `}` `}`   `// This code is contributed by shikhasingrajput`

## Python3

 `# Python 3 program for the above approach` `import` `sys` `from` `math ``import` `ceil,floor`   `# Function to modify the array element` `# such that the sum is close to 0` `def` `setSumtoZero(arr, N):` `  `  `    ``# Stores the modified elements` `    ``A ``=` `[``0` `for` `i ``in` `range``(N)]`   `    ``# Stores the sum of array element` `    ``sum` `=` `0`   `    ``# Stores minimum size of the array` `    ``m ``=` `-``sys.maxsize``-``1`   `    ``# Traverse the array and find the` `    ``# sum` `    ``for` `i ``in` `range``(N):` `        ``sum` `+``=` `ceil(arr[i])` `        ``A[i] ``=` `ceil(arr[i])`   `    ``# If sum is positive` `    ``if` `(``sum` `> ``0``):`   `        ``# Find the minimum number of` `        ``# elements that must be changed` `        ``m ``=` `min``(``sum``, N)`   `        ``# Iterate until M elements are` `        ``# modified or the array end` `        ``i ``=` `0` `        ``while``(i < N ``and` `m > ``0``):` `          `  `            ``# Update the current array` `            ``# elements to its floor` `            ``A[i] ``=` `floor(arr[i])` `            ``if` `(A[i] !``=` `floor(arr[i])):` `                ``m ``-``=` `1` `            ``i ``+``=` `1`   `    ``# Print the resultant array` `    ``for` `i ``in` `range``(N):` `        ``print``(A[i],end ``=` `" "``)`   `# Driver Code` `if` `__name__ ``=``=` `'__main__'``:` `    ``arr ``=` `[``-``2``, ``-``2``, ``4.5``]` `    ``N ``=` `len``(arr)` `    ``setSumtoZero(arr, N)` `    `  `    ``# This code is contributed by SURENDRA_GANGWAR.`

## C#

 `// C# program for the above approach` `using` `System;`   `public` `class` `GFG{`   `// Function to modify the array element` `// such that the sum is close to 0` `static` `void` `setSumtoZero(``double` `[]arr, ``int` `N)` `{` `  `  `    ``// Stores the modified elements` `    ``int` `[]A = ``new` `int``[N];`   `    ``// Stores the sum of array element` `    ``int` `sum = 0;`   `    ``// Stores minimum size of the array` `    ``int` `m = ``int``.MinValue;`   `    ``// Traverse the array and find the` `    ``// sum` `    ``for` `(``int` `i = 0; i < N; i++) {` `        ``sum += (``int``)Math.Ceiling(arr[i]);` `        ``A[i] = (``int``) Math.Ceiling(arr[i]);` `    ``}`   `    ``// If sum is positive` `    ``if` `(sum > 0) {`   `        ``// Find the minimum number of` `        ``// elements that must be changed` `        ``m = Math.Min(sum, N);`   `        ``// Iterate until M elements are` `        ``// modified or the array end` `        ``for` `(``int` `i = 0; i < N && m > 0; i++) {`   `            ``// Update the current array` `            ``// elements to its floor` `            ``A[i] = (``int``) Math.Floor(arr[i]);` `            ``if` `(A[i] != Math.Floor(arr[i]))` `                ``m--;` `        ``}` `    ``}`   `    ``// Print the resultant array` `    ``for` `(``int` `i = 0; i < N; i++) {` `        ``Console.Write(A[i]+ ``" "``);` `    ``}` `}`   `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` `    ``double` `[]arr = { -2, -2, 4.5 };` `    ``int` `N = arr.Length;` `    ``setSumtoZero(arr, N);`   `}` `}`   ` `    `// This code is contributed by 29AjayKumar`

## Javascript

 ``

Output:

`-2 -2 4`

Time Complexity: O(N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up
Recommended Articles
Page :