Difference between ceil of array sum divided by K and sum of ceil of array elements divided by K
Given an array arr[] and an integer K, the task is to find the absolute difference between the ceil of the total sum of the array divided by K and the sum of the ceil of every array element divided by K.
Examples:
Input: arr[] = {1, 2, 3, 4, 5, 6}, K = 4
Output: 2
Explanation: Sum of the array = 21. Ceil of ( Sum of the array ) / K = 6.
Sum of ceil of array elements divided by K = (1/4) + (2/4) + (3/4) + (4/4) + (5/4) + (6/4) = 1 + 1 + 1 + 1 + 2 + 2 = 8.
Therefore, absolute difference = 8 – 6 = 2.
Input: arr[] = {1, 2, 3}, K = 2
Output: 1
Approach: Follow the steps below to solve the given problem:
- Initialize two variables, say totalSum and perElementSum, to store the total sum of the array and the sum of the ceil of every array element divided by K.
- Traverse the array and perform the following:
- Add the current element arr[i] to the totalSum.
- Add the ceil of the current element divided by K i.e., arr[i]/K.
- After the above steps print the absolute value of totalSum and perElementSum as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int ceilDifference( int arr[], int n,
int x)
{
int totalSum = 0;
int perElementSum = 0;
for ( int i = 0; i < n; i++) {
totalSum += arr[i];
perElementSum
+= ceil (( double )(arr[i])
/ ( double )(x));
}
int totalCeilSum
= ceil (( double )(totalSum)
/ ( double )(x));
return abs (perElementSum
- totalCeilSum);
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int K = 4;
int N = sizeof (arr) / sizeof (arr[0]);
cout << ceilDifference(arr, N, K);
return 0;
}
|
Java
public class GFG{
static int ceilDifference( int arr[], int n,
int x)
{
int totalSum = 0 ;
int perElementSum = 0 ;
for ( int i = 0 ; i < n; i++) {
totalSum += arr[i];
perElementSum
+= Math.ceil(( double )(arr[i])
/ ( double )(x));
}
int totalCeilSum
= ( int ) Math.ceil(( double )(totalSum)
/ ( double )(x));
return Math.abs(perElementSum
- totalCeilSum);
}
public static void main(String[] args) {
int arr[] = { 1 , 2 , 3 , 4 , 5 , 6 };
int K = 4 ;
int N = arr.length;
System.out.println(ceilDifference(arr, N, K));
}
}
|
Python3
from math import ceil
def ceilDifference(arr, n, x):
totalSum = 0
perElementSum = 0
for i in range (n):
totalSum + = arr[i]
perElementSum + = ceil(arr[i] / x)
totalCeilSum = ceil(totalSum / x)
return abs (perElementSum - totalCeilSum)
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 4 , 5 , 6 ]
K = 4
N = len (arr)
print (ceilDifference(arr, N, K))
|
C#
using System;
class GFG{
static int ceilDifference( int [] arr, int n, int x)
{
int totalSum = 0;
int perElementSum = 0;
for ( int i = 0; i < n; i++)
{
totalSum += arr[i];
perElementSum += ( int )Math.Ceiling(
( double )(arr[i]) / ( double )(x));
}
int totalCeilSum = ( int )Math.Ceiling(
( double )(totalSum) / ( double )(x));
return Math.Abs(perElementSum - totalCeilSum);
}
public static void Main( string [] args)
{
int [] arr = { 1, 2, 3, 4, 5, 6 };
int K = 4;
int N = arr.Length;
Console.Write(ceilDifference(arr, N, K));
}
}
|
Javascript
<script>
function ceilDifference(arr , n, x)
{
var totalSum = 0;
var perElementSum = 0;
for ( var i = 0; i < n; i++) {
totalSum += arr[i];
perElementSum
+= parseInt(Math.ceil((arr[i])
/ (x)));
}
var totalCeilSum
= parseInt( Math.ceil((totalSum)
/ (x)));
return Math.abs(perElementSum
- totalCeilSum);
}
var arr = [ 1, 2, 3, 4, 5, 6 ];
var K = 4;
var N = arr.length;
document.write(ceilDifference(arr, N, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
17 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...