Dada una array arr[] y un entero K , la tarea es encontrar la diferencia absoluta entre el techo de la suma total de la array dividida por K y la suma del techo de cada elemento de la array dividida por K.
Ejemplos:
Entrada: arr[] = {1, 2, 3, 4, 5, 6}, K = 4
Salida: 2
Explicación: Suma de la array = 21. Ceil of ( Suma de la array ) / K = 6.
Suma de techo de elementos de array dividido por K = (1/4) + (2/4) + (3/4) + (4/4) + (5/4) + (6/4) = 1 + 1 + 1 + 1 + 2 + 2 = 8.
Por lo tanto, diferencia absoluta = 8 – 6 = 2.Entrada: arr[] = {1, 2, 3}, K = 2
Salida: 1
Enfoque: siga los pasos a continuación para resolver el problema dado:
- Inicialice dos variables, digamos totalSum y perElementSum , para almacenar la suma total de la array y la suma del techo de cada elemento de la array dividido por K .
- Atraviese la array y realice lo siguiente:
- Agregue el elemento actual arr[i] a totalSum .
- Agregue el techo del elemento actual dividido por K , es decir, arr[i]/K .
- Después de los pasos anteriores, imprima el valor absoluto de totalSum y perElementSum como resultado.
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 array sum divided by x and // sum of ceil of array elements divided by x int ceilDifference(int arr[], int n, int x) { // Stores the total sum int totalSum = 0; // Stores the sum of ceil of // array elements divided by x int perElementSum = 0; // Traverse the array for (int i = 0; i < n; i++) { // Adding each array element totalSum += arr[i]; // Add the value ceil of arr[i]/x perElementSum += ceil((double)(arr[i]) / (double)(x)); } // Find the ceil of the // total sum divided by x int totalCeilSum = ceil((double)(totalSum) / (double)(x)); // Return absolute difference return abs(perElementSum - totalCeilSum); } // Driver Code int main() { int arr[] = { 1, 2, 3, 4, 5, 6 }; int K = 4; int N = sizeof(arr) / sizeof(arr[0]); cout << ceilDifference(arr, N, K); return 0; }
Java
// Java approach for the above approach public class GFG{ // Function to find absolute difference // between array sum divided by x and // sum of ceil of array elements divided by x static int ceilDifference(int arr[], int n, int x) { // Stores the total sum int totalSum = 0; // Stores the sum of ceil of // array elements divided by x int perElementSum = 0; // Traverse the array for (int i = 0; i < n; i++) { // Adding each array element totalSum += arr[i]; // Add the value ceil of arr[i]/x perElementSum += Math.ceil((double)(arr[i]) / (double)(x)); } // Find the ceil of the // total sum divided by x int totalCeilSum = (int) Math.ceil((double)(totalSum) / (double)(x)); // Return absolute difference return Math.abs(perElementSum - totalCeilSum); } // Driver Code public static void main(String[] args) { int arr[] = { 1, 2, 3, 4, 5, 6 }; int K = 4; int N = arr.length; System.out.println(ceilDifference(arr, N, K)); } } // This code is contributed by abhinavjain194
Python3
# Python3 program for the above approach from math import ceil # Function to find absolute difference # between array sum divided by x and # sum of ceil of array elements divided by x def ceilDifference(arr, n, x): # Stores the total sum totalSum = 0 # Stores the sum of ceil of # array elements divided by x perElementSum = 0 # Traverse the array for i in range(n): # Adding each array element totalSum += arr[i] # Add the value ceil of arr[i]/x perElementSum += ceil(arr[i]/x) # Find the ceil of the # total sum divided by x totalCeilSum = ceil(totalSum / x) # Return absolute difference return abs(perElementSum- totalCeilSum) # Driver Code if __name__ == '__main__': arr =[1, 2, 3, 4, 5, 6] K = 4 N = len(arr) print (ceilDifference(arr, N, K)) # This code is contributed by mohit kumar 29.
C#
// C# approach for the above approach using System; class GFG{ // Function to find absolute difference // between array sum divided by x and // sum of ceil of array elements divided by x static int ceilDifference(int[] arr, int n, int x) { // Stores the total sum int totalSum = 0; // Stores the sum of ceil of // array elements divided by x int perElementSum = 0; // Traverse the array for(int i = 0; i < n; i++) { // Adding each array element totalSum += arr[i]; // Add the value ceil of arr[i]/x perElementSum += (int)Math.Ceiling( (double)(arr[i]) / (double)(x)); } // Find the ceil of the // total sum divided by x int totalCeilSum = (int)Math.Ceiling( (double)(totalSum) / (double)(x)); // Return absolute difference return Math.Abs(perElementSum - totalCeilSum); } // Driver Code public static void Main(string[] args) { int[] arr = { 1, 2, 3, 4, 5, 6 }; int K = 4; int N = arr.Length; Console.Write(ceilDifference(arr, N, K)); } } // This code is contributed by ukasp
Javascript
<script> // Javascript approach for the above approach // Function to find absolute difference // between array sum divided by x and // sum of ceil of array elements divided by x function ceilDifference(arr , n, x) { // Stores the total sum var totalSum = 0; // Stores the sum of ceil of // array elements divided by x var perElementSum = 0; // Traverse the array for (var i = 0; i < n; i++) { // Adding each array element totalSum += arr[i]; // Add the value ceil of arr[i]/x perElementSum += parseInt(Math.ceil((arr[i]) / (x))); } // Find the ceil of the // total sum divided by x var totalCeilSum = parseInt( Math.ceil((totalSum) / (x))); // Return absolute difference return Math.abs(perElementSum - totalCeilSum); } // Driver Code var arr = [ 1, 2, 3, 4, 5, 6 ]; var K = 4; var N = arr.length; document.write(ceilDifference(arr, N, K)); // This code contributed by shikhasingrajput </script>
Producción:
2
Complejidad temporal: O(N)
Espacio auxiliar: O(1)