# 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++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to find absolute difference``// between array sum divided by x and``// sum of ceil of array elements divided by x``int` `ceilDifference(``int` `arr[], ``int` `n,``                   ``int` `x)``{``    ``// Stores the total sum``    ``int` `totalSum = 0;` `    ``// Stores the sum of ceil of``    ``// array elements divided by x``    ``int` `perElementSum = 0;` `    ``// Traverse the array``    ``for` `(``int` `i = 0; i < n; i++) {` `        ``// Adding each array element``        ``totalSum += arr[i];` `        ``// Add the value ceil of arr[i]/x``        ``perElementSum``            ``+= ``ceil``((``double``)(arr[i])``                    ``/ (``double``)(x));``    ``}` `    ``// Find the ceil of the``    ``// total sum divided by x``    ``int` `totalCeilSum``        ``= ``ceil``((``double``)(totalSum)``               ``/ (``double``)(x));` `    ``// Return absolute difference``    ``return` `abs``(perElementSum``               ``- totalCeilSum);``}` `// Driver Code``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

 `// Java approach for the above approach``public` `class` `GFG{` ` ``// Function to find absolute difference`` ``// between array sum divided by x and`` ``// sum of ceil of array elements divided by x`` ``static` `int` `ceilDifference(``int` `arr[], ``int` `n,``                    ``int` `x)`` ``{``     ``// Stores the total sum``     ``int` `totalSum = ``0``;` `     ``// Stores the sum of ceil of``     ``// array elements divided by x``     ``int` `perElementSum = ``0``;` `     ``// Traverse the array``     ``for` `(``int` `i = ``0``; i < n; i++) {` `         ``// Adding each array element``         ``totalSum += arr[i];` `         ``// Add the value ceil of arr[i]/x``         ``perElementSum``             ``+= Math.ceil((``double``)(arr[i])``                     ``/ (``double``)(x));``     ``}` `     ``// Find the ceil of the``     ``// total sum divided by x``     ``int` `totalCeilSum``         ``= (``int``) Math.ceil((``double``)(totalSum)``            ``/ (``double``)(x));` `     ``// Return absolute difference``     ``return` `Math.abs(perElementSum``                ``- totalCeilSum);`` ``}` `    ``// Driver Code``    ``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));``    ``}` `}``// This code is contributed by abhinavjain194`

## Python3

 `# Python3 program for the above approach``from` `math ``import` `ceil` `# Function to find absolute difference``# between array sum divided by x and``# sum of ceil of array elements divided by x``def` `ceilDifference(arr, n, x):``    ``# Stores the total sum``    ``totalSum ``=` `0` `    ``# Stores the sum of ceil of``    ``# array elements divided by x``    ``perElementSum ``=` `0` `    ``# Traverse the array``    ``for` `i ``in` `range``(n):``        ``# Adding each array element``        ``totalSum ``+``=` `arr[i]` `        ``# Add the value ceil of arr[i]/x``        ``perElementSum ``+``=` `ceil(arr[i]``/``x)` `    ``# Find the ceil of the``    ``# total sum divided by x``    ``totalCeilSum ``=` `ceil(totalSum ``/` `x)` `    ``# Return absolute difference``    ``return` `abs``(perElementSum``-` `totalCeilSum)` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ``arr ``=``[``1``, ``2``, ``3``, ``4``, ``5``, ``6``]``    ``K ``=` `4``    ``N ``=` `len``(arr)` `    ``print` `(ceilDifference(arr, N, K))` `# This code is contributed by mohit kumar 29.`

## C#

 `// C# approach for the above approach``using` `System;` `class` `GFG{` `// Function to find absolute difference``// between array sum divided by x and``// sum of ceil of array elements divided by x``static` `int` `ceilDifference(``int``[] arr, ``int` `n, ``int` `x)``{``    ` `    ``// Stores the total sum``    ``int` `totalSum = 0;` `    ``// Stores the sum of ceil of``    ``// array elements divided by x``    ``int` `perElementSum = 0;` `    ``// Traverse the array``    ``for``(``int` `i = 0; i < n; i++)``    ``{``        ` `        ``// Adding each array element``        ``totalSum += arr[i];` `        ``// Add the value ceil of arr[i]/x``        ``perElementSum += (``int``)Math.Ceiling(``            ``(``double``)(arr[i]) / (``double``)(x));``    ``}` `    ``// Find the ceil of the``    ``// total sum divided by x``    ``int` `totalCeilSum = (``int``)Math.Ceiling(``        ``(``double``)(totalSum) / (``double``)(x));` `    ``// Return absolute difference``    ``return` `Math.Abs(perElementSum - totalCeilSum);``}` `// Driver Code``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));``}``}` `// This code is contributed by ukasp`

## Javascript

 ``

Output:
`2`

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

Previous
Next