Diferencia absoluta entre el piso de la suma del Array dividido por X y el piso de la suma de cada elemento del Array cuando se divide por X

Dada una array A[] y un entero positivo X . La tarea es encontrar la diferencia absoluta entre el piso de la suma total dividida por X y la suma del piso de cada elemento de A[] dividido por X.

Ejemplos:

Entrada: A[] = {1, 2, 3, 4, 5, 6}, X = 4
Salida: 2
Explicación

  • Suma de A[] = 1 + 2 + 3 + 4 + 5 + 6 = 21
  • Suma de A[] dividida por X = 21 / 4 = 5
  • Suma de piso de cada elemento dividido por X = 1/4 + 2/4 + 3/4 + 4/4 + 5/4 + 6/4 = 0 + 0 + 0 + 1 + 1 + 1 = 3
  • Diferencia absoluta = 5 – 3 = 2

Entrada: A[] = {1, 2}, X = 2
Salida: 0

Enfoque : siga los pasos dados para resolver el problema

  • Inicialice dos variables, totalFloorSum = 0 y FloorSumPerElement = 0
  • Atraviesa la array , para i en el rango [0, N – 1]
    • Actualice totalFloorSum = totalFloorSum + A[i] y FloorSumPerElement = FloorSumPerElement + floor(A[i] / X)
  • Actualizar totalFloorSum = totalFloorSum / N
  • Después de completar los pasos anteriores, imprima la diferencia absoluta de totalFloorSum y FloorSumPerElement

A continuación se muestra la implementación del enfoque anterior:

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>
Producción

2

Complejidad de Tiempo : O(N)
Espacio Auxiliar : O(1)

Publicación traducida automáticamente

Artículo escrito por garry133 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *