数组总和除以 X 的下限与每个数组元素除以 X 的下限总和之间的绝对差
给定一个数组A[]和一个正整数X 。任务是求总和除以X的下限与A[]的每个元素的下限除以X的和之间的绝对差。
例子:
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
方法:按照给定的步骤解决问题
- 初始化两个变量, totalFloorSum = 0和FloorSumPerElement = 0
- 遍历数组,因为i在[0, N – 1]范围内
- 更新totalFloorSum = totalFloorSum + A[i]和FloorSumPerElement = FloorSumPerElement + floor(A[i] / X)
- 更新totalFloorSum = totalFloorSum / N
- 完成上述步骤后,打印totalFloorSum和FloorSumPerElement的绝对差值
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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
输出
2
时间复杂度: O(N)
辅助空间: O(1)