Make the Array sum 0 by using either ceil or floor on each element
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++
#include <bits/stdc++.h>
using namespace std;
void setSumtoZero( double arr[], int N)
{
int A[N];
int sum = 0;
int m = INT_MIN;
for ( int i = 0; i < N; i++) {
sum += ceil (arr[i]);
A[i] = ceil (arr[i]);
}
if (sum > 0) {
m = min(sum, N);
for ( int i = 0; i < N && m > 0; i++) {
A[i] = floor (arr[i]);
if (A[i] != floor (arr[i]))
m--;
}
}
for ( int i = 0; i < N; i++) {
cout << A[i] << " " ;
}
}
int main()
{
double arr[] = { -2, -2, 4.5 };
int N = sizeof (arr) / sizeof (arr[0]);
setSumtoZero(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void setSumtoZero( double arr[], int N)
{
int []A = new int [N];
int sum = 0 ;
int m = Integer.MIN_VALUE;
for ( int i = 0 ; i < N; i++) {
sum += Math.ceil(arr[i]);
A[i] = ( int ) Math.ceil(arr[i]);
}
if (sum > 0 ) {
m = Math.min(sum, N);
for ( int i = 0 ; i < N && m > 0 ; i++) {
A[i] = ( int ) Math.floor(arr[i]);
if (A[i] != Math.floor(arr[i]))
m--;
}
}
for ( int i = 0 ; i < N; i++) {
System.out.print(A[i]+ " " );
}
}
public static void main(String[] args)
{
double arr[] = { - 2 , - 2 , 4.5 };
int N = arr.length;
setSumtoZero(arr, N);
}
}
|
Python3
import sys
from math import ceil,floor
def setSumtoZero(arr, N):
A = [ 0 for i in range (N)]
sum = 0
m = - sys.maxsize - 1
for i in range (N):
sum + = ceil(arr[i])
A[i] = ceil(arr[i])
if ( sum > 0 ):
m = min ( sum , N)
i = 0
while (i < N and m > 0 ):
A[i] = floor(arr[i])
if (A[i] ! = floor(arr[i])):
m - = 1
i + = 1
for i in range (N):
print (A[i],end = " " )
if __name__ = = '__main__' :
arr = [ - 2 , - 2 , 4.5 ]
N = len (arr)
setSumtoZero(arr, N)
|
C#
using System;
public class GFG{
static void setSumtoZero( double []arr, int N)
{
int []A = new int [N];
int sum = 0;
int m = int .MinValue;
for ( int i = 0; i < N; i++) {
sum += ( int )Math.Ceiling(arr[i]);
A[i] = ( int ) Math.Ceiling(arr[i]);
}
if (sum > 0) {
m = Math.Min(sum, N);
for ( int i = 0; i < N && m > 0; i++) {
A[i] = ( int ) Math.Floor(arr[i]);
if (A[i] != Math.Floor(arr[i]))
m--;
}
}
for ( int i = 0; i < N; i++) {
Console.Write(A[i]+ " " );
}
}
public static void Main(String[] args)
{
double []arr = { -2, -2, 4.5 };
int N = arr.Length;
setSumtoZero(arr, N);
}
}
|
Javascript
<script>
function setSumtoZero(arr, N)
{
let A = new Array(N);
let sum = 0;
let m = Number.MIN_VALUE;
for (let i = 0; i < N; i++) {
sum += Math.ceil(arr[i]);
A[i] = Math.ceil(arr[i]);
}
if (sum > 0) {
m = Math.min(sum, N);
for (let i = 0; i < N && m > 0; i++) {
A[i] = Math.floor(arr[i]);
if (A[i] != Math.floor(arr[i]))
m--;
}
}
for (let i = 0; i < N; i++) {
document.write(A[i] + " " );
}
}
let arr = [-2, -2, 4.5];
let N = arr.length;
setSumtoZero(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
18 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...