Saturday, September 28, 2024
Google search engine
HomeData Modelling & AIAbsolute difference between floor of Array sum divided by X and floor...

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

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)

 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Commit to GfG’s Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated :
30 Jun, 2021
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Share your thoughts in the comments

RELATED ARTICLES

Most Popular

Recent Comments