# Absolute difference between floor of Array sum divided by X and floor sum of every Array element when divided by X

• Last Updated : 30 Jun, 2021

Given an array A[] and a positive integer X. The task is to find the absolute difference between the floor of the total sum divided by X and the sum of the floor of each element of A[] divided by X.

Examples:

Input: A[] = {1, 2, 3, 4, 5, 6}, X = 4
Output: 2
Explanation

• Sum of A[] = 1 + 2 + 3 + 4 + 5 + 6 = 21
• Sum of A[] divided by X = 21 / 4 = 5
• Sum of floor of every element divided by X = 1 / 4 + 2 / 4 + 3 / 4 + 4 / 4 + 5 / 4 + 6 / 4 = 0 + 0 + 0 + 1 + 1 + 1 = 3
• Absolute Difference = 5 – 3 = 2

Input: A[] = {1, 2}, X = 2
Output: 0

Approach : Follow the given steps to solve the problem

• Initialize two variables, totalFloorSum = 0 and FloorSumPerElement = 0
• Traverse the array, for i in range [0, N – 1]
• Update totalFloorSum = totalFloorSum + A[i] and FloorSumPerElement = FloorSumPerElement + floor(A[i] / X)
• Update totalFloorSum = totalFloorSum / N
• After completing the above steps, print the absolute difference of totalFloorSum and FloorSumPerElement

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 the two sum values` `int` `floorDifference(``int` `A[], ``int` `N, ``int` `X)` `{` `    ``// Variable to store total sum` `    ``int` `totalSum = 0;`   `    ``// Variable to store sum of A[i] / X` `    ``int` `perElementSum = 0;` `    `  `      ``// Traverse the array` `    ``for` `(``int` `i = 0; i < N; i++) {` `      `  `          ``// Update totalSum` `        ``totalSum += A[i];` `      `  `          ``// Update perElementSum` `        ``perElementSum += A[i] / X;` `    ``}`   `    ``// Floor of total sum divided by X` `    ``int` `totalFloorSum = totalSum / X;`   `    ``// Return the absolute difference` `    ``return` `abs``(totalFloorSum - perElementSum);` `}`   `// Driver Code` `int` `main()` `{` `    ``// Input` `    ``int` `A[] = { 1, 2, 3, 4, 5, 6 };` `    ``int` `X = 4;`   `    ``// Size of Array` `    ``int` `N = ``sizeof``(A) / ``sizeof``(A);`   `    ``// Function call to find absolute difference` `    ``// between the two sum values` `    ``cout << floorDifference(A, N, X);`   `    ``return` `0;` `}`

## Java

 `// Java program for the above approach` `import` `java.util.*;` `class` `GFG` `{    `   `// Function to find absolute difference` `// between the two sum values` `static` `int` `floorDifference(``int` `A[], ``int` `N, ``int` `X)` `{` `  `  `    ``// Variable to store total sum` `    ``int` `totalSum = ``0``;`   `    ``// Variable to store sum of A[i] / X` `    ``int` `perElementSum = ``0``;` `    `  `      ``// Traverse the array` `    ``for` `(``int` `i = ``0``; i < N; i++) {` `      `  `          ``// Update totalSum` `        ``totalSum += A[i];` `      `  `          ``// Update perElementSum` `        ``perElementSum += A[i] / X;` `    ``}`   `    ``// Floor of total sum divided by X` `    ``int` `totalFloorSum = totalSum / X;`   `    ``// Return the absolute difference` `    ``return` `Math.abs(totalFloorSum - perElementSum);` `}`   `// Driver Code` `public` `static` `void` `main(String[] args)` `{` `  `  `    ``// Input` `    ``int` `A[] = { ``1``, ``2``, ``3``, ``4``, ``5``, ``6` `};` `    ``int` `X = ``4``;`   `    ``// Size of Array` `    ``int` `N = A.length;`   `    ``// Function call to find absolute difference` `    ``// between the two sum values` `    ``System.out.print( floorDifference(A, N, X));` `}` `}`   `// This code is contributed by code_hunt.`

## Python3

 `# Python3 program for the above approach`   `# Function to find absolute difference` `# between the two sum values` `def` `floorDifference(A, N, X):` `    `  `    ``# Variable to store total sum` `    ``totalSum ``=` `0`   `    ``# Variable to store sum of A[i] / X` `    ``perElementSum ``=` `0`   `    ``# Traverse the array` `    ``for` `i ``in` `range``(N):` `        `  `        ``# Update totalSum` `        ``totalSum ``+``=` `A[i]`   `        ``# Update perElementSum` `        ``perElementSum ``+``=` `A[i] ``/``/` `X`   `    ``# Floor of total sum divided by X` `    ``totalFloorSum ``=` `totalSum ``/``/` `X`   `    ``# Return the absolute difference` `    ``return` `abs``(totalFloorSum ``-` `perElementSum)`   `# Driver Code` `if` `__name__ ``=``=` `'__main__'``:` `    `  `    ``# Input` `    ``A ``=` `[ ``1``, ``2``, ``3``, ``4``, ``5``, ``6` `]` `    ``X ``=` `4`   `    ``# Size of Array` `    ``N ``=` `len``(A)`   `    ``# Function call to find absolute difference` `    ``# between the two sum values` `    ``print` `(floorDifference(A, N, X))`   `# This code is contributed by mohit kumar 29`

## C#

 `// C# program for the above approach` `using` `System;` `class` `GFG` `{`   `// Function to find absolute difference` `// between the two sum values` `static` `int` `floorDifference(``int``[] A, ``int` `N, ``int` `X)` `{` `   `  `    ``// Variable to store total sum` `    ``int` `totalSum = 0;` ` `  `    ``// Variable to store sum of A[i] / X` `    ``int` `perElementSum = 0;` `     `  `      ``// Traverse the array` `    ``for` `(``int` `i = 0; i < N; i++) {` `       `  `          ``// Update totalSum` `        ``totalSum += A[i];` `       `  `          ``// Update perElementSum` `        ``perElementSum += A[i] / X;` `    ``}` ` `  `    ``// Floor of total sum divided by X` `    ``int` `totalFloorSum = totalSum / X;` ` `  `    ``// Return the absolute difference` `    ``return` `Math.Abs(totalFloorSum - perElementSum);` `}`   `// Driver code` `static` `void` `Main()` `{` `  `  `    ``// Input` `    ``int``[] A = { 1, 2, 3, 4, 5, 6 };` `    ``int` `X = 4;` ` `  `    ``// Size of Array` `    ``int` `N = A.Length;` ` `  `    ``// Function call to find absolute difference` `    ``// between the two sum values` `    ``Console.Write( floorDifference(A, N, X));`   `}` `}`   `// This code is contributed by sanjoy_62.`

## Javascript

 ``

Output

`2`

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :