给定一个正整数的数组arr []以及两个整数L和R ,任务是找到范围[L,R]中的数组元素的所有倍数之和。
例子:
Input: arr[] = {2, 7, 3, 8}, L = 7, R = 20
Output: 197
Explanation:
In the range 7 to 20:
Sum of multiples of 2: 8 + 10 + 12 + 14 + 16 + 18 + 20 = 98
Sum of multiples of 7: 7 + 14 = 21
Sum of multiples of 3: 9 + 12 + 15 + 18 = 54
Sum of multiples of 8: 8 + 16 = 24
Total sum of all multiples = 98 + 21 + 54 + 24 = 197
Input: arr[] = {5, 6, 7, 8, 9}, L = 39, R = 100
Output: 3278
天真的方法:天真的想法是对于给定数组arr []中的每个元素,找到范围[L,R]中该元素的倍数,并打印所有倍数的总和。
时间复杂度: O(N *(LR))
辅助空间: O(1)
高效的方法:为了优化上述朴素的方法,我们将使用以下讨论的概念:
- 对于任何整数X ,由Y / X给出直到任何整数Y的X的倍数。
- 令N = Y / X
然后,上述所有倍数的总和由X * N *(N-1)/ 2给出。
例如:
For X = 2 and Y = 12
Sum of multiple is:
=> 2 + 4 + 6 + 8 + 10 + 12
=> 2*(1 + 2 + 3 + 4 + 5 + 6)
=> 2*(6*5)/2
=> 20.
使用以上概念,可以使用以下步骤解决问题:
- 使用以上讨论的公式计算arr [i]直至R的所有倍数的总和。
- 使用上面讨论的公式,计算arr [i]直到L – 1的所有倍数的总和。
- 在上述步骤中将上述两个值相减,得出范围[L,R]之间的所有倍数之和。
- 对所有元素重复上述过程,然后打印总和。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the sum of all
// multiples of N up to K
int calcSum(int k, int n)
{
// Calculate the sum
int value = (k * n * (n
+ 1))
/ 2;
// Return the sum
return value;
}
// Function to find the total sum
int findSum(int* a, int n, int L, int R)
{
int sum = 0;
for (int i = 0; i < n; i++) {
// Calculating sum of multiples
// for each element
// If L is divisible by a[i]
if (L % a[i] == 0 && L != 0) {
sum += calcSum(a[i], R / a[i])
- calcSum(a[i],
(L - 1) / a[i]);
}
// Otherwise
else {
sum += calcSum(a[i], R / a[i])
- calcSum(a[i], L / a[i]);
}
}
// Return the final sum
return sum;
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = { 2, 7, 3, 8 };
int N = sizeof(arr) / sizeof(arr[0]);
// Given range
int L = 7;
int R = 20;
// Function Call
cout << findSum(arr, N, L, R);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to find the sum of
// all multiples of N up to K
static int calcSum(int k, int n)
{
// Calculate the sum
int value = (k * n * (n + 1)) / 2;
// Return the sum
return value;
}
// Function to find the total sum
static int findSum(int[] a, int n,
int L, int R)
{
int sum = 0;
for(int i = 0; i < n; i++)
{
// Calculating sum of multiples
// for each element
// If L is divisible by a[i]
if (L % a[i] == 0 && L != 0)
{
sum += calcSum(a[i], R / a[i]) -
calcSum(a[i], (L - 1) / a[i]);
}
// Otherwise
else
{
sum += calcSum(a[i], R / a[i]) -
calcSum(a[i], L / a[i]);
}
}
// Return the final sum
return sum;
}
// Driver Code
public static void main (String[] args)
{
// Given array arr[]
int arr[] = { 2, 7, 3, 8 };
int N = arr.length;
// Given range
int L = 7;
int R = 20;
// Function Call
System.out.println(findSum(arr, N, L, R));
}
}
// This code is contributed by shubhamsingh10
Python3
# Python3 program for the above approach
# Function to find the sum of
# all multiples of N up to K
def calcSum(k, n):
# Calculate the sum
value = (k * n * (n + 1)) // 2
# Return the sum
return value
# Function to find the total sum
def findSum(a, n, L, R):
sum = 0
for i in range(n):
# Calculating sum of multiples
# for each element
# If L is divisible by a[i]
if (L % a[i] == 0 and L != 0):
sum += (calcSum(a[i], R // a[i]) -
calcSum(a[i], (L - 1) // a[i]))
# Otherwise
else:
sum += (calcSum(a[i], R // a[i]) -
calcSum(a[i], L // a[i]))
# Return the final sum
return sum;
# Driver code
if __name__=="__main__":
# Given array arr[]
arr = [ 2, 7, 3, 8 ]
N = len(arr)
# Given range
L = 7
R = 20
# Function call
print(findSum(arr, N, L, R))
# This code is contributed by rutvik_56
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the sum of
// all multiples of N up to K
static int calcSum(int k, int n)
{
// Calculate the sum
int value = (k * n * (n + 1)) / 2;
// Return the sum
return value;
}
// Function to find the total sum
static int findSum(int[] a, int n,
int L, int R)
{
int sum = 0;
for(int i = 0; i < n; i++)
{
// Calculating sum of multiples
// for each element
// If L is divisible by a[i]
if (L % a[i] == 0 && L != 0)
{
sum += calcSum(a[i], R / a[i]) -
calcSum(a[i], (L - 1) / a[i]);
}
// Otherwise
else
{
sum += calcSum(a[i], R / a[i]) -
calcSum(a[i], L / a[i]);
}
}
// Return the final sum
return sum;
}
// Driver Code
public static void Main (string[] args)
{
// Given array arr[]
int []arr = new int[]{ 2, 7, 3, 8 };
int N = arr.Length;
// Given range
int L = 7;
int R = 20;
// Function Call
Console.Write(findSum(arr, N, L, R));
}
}
// This code is contributed by Ritik Bansal
197
时间复杂度: O(N),其中N是给定数组中元素的数量。
辅助空间: O(1)