📌  相关文章
📜  给定范围[L,R]中Array元素的倍数总和

📅  最后修改于: 2021-05-17 16:10:43             🧑  作者: Mango

给定一个正整数的数组arr []以及两个整数LR ,任务是找到范围[L,R]中的数组元素的所有倍数之和。

例子:

天真的方法:天真的想法是对于给定数组arr []中的每个元素,找到范围[L,R]中该元素的倍数,并打印所有倍数的总和。

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

高效的方法:为了优化上述朴素的方法,我们将使用以下讨论的概念:

  1. 对于任何整数X ,由Y / X给出直到任何整数YX的倍数。
  2. N = Y / X
    然后,上述所有倍数的总和由X * N *(N-1)/ 2给出

例如:

使用以上概念,可以使用以下步骤解决问题:

  1. 使用以上讨论的公式计算arr [i]直至R的所有倍数的总和。
  2. 使用上面讨论的公式,计算arr [i]直到L – 1的所有倍数的总和。
  3. 在上述步骤中将上述两个值相减,得出范围[L,R]之间的所有倍数之和。
  4. 对所有元素重复上述过程,然后打印总和。

下面是上述方法的实现:

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)