Difference between ceil of array sum divided by K and sum of ceil of array elements divided by K | Python3 program for the above approach ; Function to find absolute difference between array sum divided by x and sum of ceil of array elements divided by x ; Stores the total sum ; Stores the sum of ceil of array elements divided by x ; Traverse the array ; Adding each array element ; Add the value ceil of arr [ i ] / x ; Find the ceil of the total sum divided by x ; Return absolute difference ; Driver Code
from math import ceil NEW_LINE def ceilDifference ( arr , n , x ) : NEW_LINE INDENT totalSum = 0 NEW_LINE perElementSum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT totalSum += arr [ i ] NEW_LINE perElementSum += ceil ( arr [ i ] / x ) NEW_LINE DEDENT totalCeilSum = ceil ( totalSum / x ) NEW_LINE return abs ( perElementSum - totalCeilSum ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] NEW_LINE K = 4 NEW_LINE N = len ( arr ) NEW_LINE print ( ceilDifference ( arr , N , K ) ) NEW_LINE DEDENT