Skip to content
Related Articles

Related Articles

Improve Article

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 <bits/stdc++.h>
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[0]);
 
    // 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




<script>
// Javascript program for the above approach
 
// Function to find absolute difference
// between the two sum values
function floorDifference(A,N,X)
{
    // Variable to store total sum
    let totalSum = 0;
  
    // Variable to store sum of A[i] / X
    let perElementSum = 0;
      
    // Traverse the array
    for (let i = 0; i < N; i++) {
        
        // Update totalSum
        totalSum += A[i];
        
        // Update perElementSum
        perElementSum += Math.floor(A[i] / X);
    }
  
    // Floor of total sum divided by X
    let totalFloorSum = Math.floor(totalSum / X);
  
    // Return the absolute difference
    return Math.abs(totalFloorSum - perElementSum);
}
 
// Driver Code
// Input
let A=[1, 2, 3, 4, 5, 6];
let X = 4;
// Size of Array
let N = A.length;
 
// Function call to find absolute difference
// between the two sum values
document.write( floorDifference(A, N, X));
 
 
// This code is contributed by unknown2108
</script>
Output
2

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

 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :