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++
#include <bits/stdc++.h>
using namespace std;
int floorDifference( int A[], int N, int X)
{
int totalSum = 0;
int perElementSum = 0;
for ( int i = 0; i < N; i++) {
totalSum += A[i];
perElementSum += A[i] / X;
}
int totalFloorSum = totalSum / X;
return abs (totalFloorSum - perElementSum);
}
int main()
{
int A[] = { 1, 2, 3, 4, 5, 6 };
int X = 4;
int N = sizeof (A) / sizeof (A[0]);
cout << floorDifference(A, N, X);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int floorDifference( int A[], int N, int X)
{
int totalSum = 0 ;
int perElementSum = 0 ;
for ( int i = 0 ; i < N; i++) {
totalSum += A[i];
perElementSum += A[i] / X;
}
int totalFloorSum = totalSum / X;
return Math.abs(totalFloorSum - perElementSum);
}
public static void main(String[] args)
{
int A[] = { 1 , 2 , 3 , 4 , 5 , 6 };
int X = 4 ;
int N = A.length;
System.out.print( floorDifference(A, N, X));
}
}
|
Python3
def floorDifference(A, N, X):
totalSum = 0
perElementSum = 0
for i in range (N):
totalSum + = A[i]
perElementSum + = A[i] / / X
totalFloorSum = totalSum / / X
return abs (totalFloorSum - perElementSum)
if __name__ = = '__main__' :
A = [ 1 , 2 , 3 , 4 , 5 , 6 ]
X = 4
N = len (A)
print (floorDifference(A, N, X))
|
C#
using System;
class GFG
{
static int floorDifference( int [] A, int N, int X)
{
int totalSum = 0;
int perElementSum = 0;
for ( int i = 0; i < N; i++) {
totalSum += A[i];
perElementSum += A[i] / X;
}
int totalFloorSum = totalSum / X;
return Math.Abs(totalFloorSum - perElementSum);
}
static void Main()
{
int [] A = { 1, 2, 3, 4, 5, 6 };
int X = 4;
int N = A.Length;
Console.Write( floorDifference(A, N, X));
}
}
|
Javascript
<script>
function floorDifference(A,N,X)
{
let totalSum = 0;
let perElementSum = 0;
for (let i = 0; i < N; i++) {
totalSum += A[i];
perElementSum += Math.floor(A[i] / X);
}
let totalFloorSum = Math.floor(totalSum / X);
return Math.abs(totalFloorSum - perElementSum);
}
let A=[1, 2, 3, 4, 5, 6];
let X = 4;
let N = A.length;
document.write( floorDifference(A, N, X));
</script>
|
Time Complexity : O(N)
Auxiliary Space : O(1)
Share your thoughts in the comments
Please Login to comment...