Sum of multiples of Array elements within a given range [L, R]
Last Updated :
08 Jul, 2021
Given an array arr[] of positive integers and two integers L and R, the task is to find the sum of all multiples of the array elements in the range [L, R].
Examples:
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
Naive Approach: The naive idea is for each element in the given array arr[] find the multiple of the element in the range [L, R] and print the sum of all the multiples.
Time Complexity: O(N*(L-R))
Auxiliary Space: O(1)
Efficient Approach: To optimize the above naive approach we will use the concept discussed below:
- For any integer X, the number of multiples of X till any integer Y is given by Y/X.
- Let N = Y/X
Then, the sum of all the above multiple is given by X*N*(N-1)/2.
For Example:
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.
Using the above concept the problem can be solved using the below steps:
- Calculate the sum of all multiples of arr[i] up to R using the above-discussed formula.
- Calculate the sum of all multiples of arr[i] up to L - 1 using the above-discussed formula.
- Subtract the above two values in the above steps to get the sum of all multiples between ranges [L, R].
- Repeat the above process for all the elements and print the sum.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
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
JavaScript
<script>
// Javascript implementation for the above approach
// Function to find the sum of
// all multiples of N up to K
function calcSum(k, n)
{
// Calculate the sum
let value = (k * n * (n + 1)) / 2;
// Return the sum
return value;
}
// Function to find the total sum
function findSum(a, n, L, R)
{
let sum = 0;
for(let 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], Math.floor(R / a[i])) -
calcSum(a[i], Math.floor((L - 1) / a[i]));
}
// Otherwise
else
{
sum += calcSum(a[i], Math.floor(R / a[i])) -
calcSum(a[i], Math.floor(L / a[i]));
}
}
// Return the final sum
return sum;
}
// Driver Code
// Given array arr[]
let arr = [ 2, 7, 3, 8 ];
let N = arr.length;
// Given range
let L = 7;
let R = 20;
// Function Call
document.write(findSum(arr, N, L, R));
</script>
Time Complexity: O(N), where N is the number of elements in the given array.
Auxiliary Space: O(1)
Similar Reads
Sum of array elements which are multiples of a given number Given an array arr[] consisting of positive integers and an integer N, the task is to find the sum of all array elements which are multiples of N Examples: Input: arr[] = {1, 2, 3, 5, 6}, N = 3Output: 9Explanation: From the given array, 3 and 6 are multiples of 3. Therefore, sum = 3 + 6 = 9. Input:
5 min read
Sum of elements of an AP in the given range Given an arithmetic series in arr and Q queries in the form of [L, R], where L is the left boundary of the range and R is the right boundary. The task is to find the sum of the AP elements in the given range.Note: The range is 1-indexed and 1 ? L, R ? N, where N is the size of arr. Examples: Input:
10 min read
Find array elements with frequencies in range [l , r] Given an array of integers, find the elements from the array whose frequency lies in the range [l, r]. Examples: Input : arr[] = { 1, 2, 3, 3, 2, 2, 5 } l = 2, r = 3 Output : 2 3 3 2 2 Approach : Take a hash map, which will store the frequency of all the elements in the array.Now, traverse once agai
9 min read
Count of elements which is the sum of a subarray of the given Array Given an array arr[], the task is to count elements in an array such that there exists a subarray whose sum is equal to this element.Note: Length of subarray must be greater than 1. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7} Output: 4 Explanation: There are 4 such elements in array - arr[2] = 3
7 min read
Find the summation of the product of Array elements in range [L, R] Given an array arr[] and two integers L and R. The task is to find the sum of the product of all the pairs (i, j) in the range [L, R], such that i ⤠j. Input: arr[] = { 1, 3, 5, 8 }, L = 0, R = 2Output: 58Explanation: As 1*1 + 1*3 + 1*5 + 3*3 + 3*5 + 5*5 = 58 Input: arr[] = { 2, 1, 4, 5, 3, 2, 1 },
8 min read