📜  数组和的ceil除以K与数组元素的ceil之和除以K的差值

📅  最后修改于: 2021-09-07 02:19:02             🧑  作者: Mango

给定的阵列ARR []和一个整数K,任务是找到阵列除以K的总和的小区和除以K每个数组元素的小区的总和之间的绝对差。

例子:

方法:按照以下步骤解决给定的问题:

  • 初始化两个变量,比如totalSumperElementSum ,以存储数组的总和以及每个数组元素的 ceil 的总和除以K
  • 遍历数组并执行以下操作:
    • 将当前元素arr[i] 添加totalSum 中
    • 添加当前元素的 ceil 除以K,arr[i]/K
  • 经过上述步骤后,打印totalSumperElementSum的绝对值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
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


输出:
2

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live