Find numbers in between [L, R] which are divisible by all Array elements
Last Updated :
10 Jan, 2022
Given an array arr[] containing N positive integers and two variables L and R indicating a range of integers from L to R (inclusive). The task is to print all the numbers between L to R which are divisible by all array elements. If no such value exists print -1.
Input: arr[] = {3, 5, 12}, L = 90, R = 280
Output: 120 180 240
Explanation: 120, 180, 240 are the numbers which are divisible by all the arr[] elements.
Input: arr[] = {4, 7, 13, 16}, L = 200, R = 600
Output: -1
Naive Approach: In this approach for every element in range [L, R] check if it is divisible by all the elements of the array.
Time Complexity: O((R-L)*N)
Auxiliary Space: O(1)
Efficient Approach: The given problem can be solved using basic math. Any element divisible by all the elements of the array is a multiple of the LCM of all the array elements. Find the multiples of LCM in the range [L, R] and store in an array. At last print the numbers stored in the array.
Time Complexity: O(N)
Auxiliary Space: O(R – L)
Space Optimized Approach: Below steps can be used to solve the problem:
- Calculate the LCM of all the elements of given arr[]
- Now, check the LCM for these conditions:
- If (LCM < L and LCM*2 > R), then print -1.
- If (LCM > R), then print -1.
- Now, take the nearest value of L (between L to R) which is divisible by the LCM, say i.
- Now, start printing i and increment it by LCM every time after printing, until it becomes greater than R.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void solve( int * arr, int N, int L, int R)
{
int LCM = arr[0];
for ( int i = 1; i < N; i++) {
LCM = (LCM * arr[i]) /
(__gcd(LCM, arr[i]));
}
if ((LCM < L && LCM * 2 > R) || LCM > R) {
cout << "-1" ;
return ;
}
int k = (L / LCM) * LCM;
if (k < L)
k = k + LCM;
for ( int i = k; i <= R; i = i + LCM) {
cout << i << ' ' ;
}
}
int main()
{
int L = 90;
int R = 280;
int arr[] = { 3, 5, 12 };
int N = sizeof (arr) / sizeof (arr[0]);
solve(arr, N, L, R);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int __gcd( int a, int b)
{
if (a == 0 )
return b;
if (b == 0 )
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
static void solve( int [] arr, int N, int L, int R)
{
int LCM = arr[ 0 ];
for ( int i = 1 ; i < N; i++)
{
LCM = (LCM * arr[i]) /
(__gcd(LCM, arr[i]));
}
if ((LCM < L && LCM * 2 > R) || LCM > R)
{
System.out.println( "-1" );
return ;
}
int k = (L / LCM) * LCM;
if (k < L)
k = k + LCM;
for ( int i = k; i <= R; i = i + LCM)
{
System.out.print(i + " " );
}
}
public static void main(String args[])
{
int L = 90 ;
int R = 280 ;
int arr[] = { 3 , 5 , 12 };
int N = arr.length;
solve(arr, N, L, R);
}
}
|
Python3
def __gcd(a, b):
if (a = = 0 ):
return b;
if (b = = 0 ):
return a;
if (a = = b):
return a;
if (a > b):
return __gcd(a - b, b);
return __gcd(a, b - a);
def solve(arr, N, L, R):
LCM = arr[ 0 ];
for i in range ( 1 , N):
LCM = (LCM * arr[i]) / / (__gcd(LCM, arr[i]));
if ((LCM < L and LCM * 2 > R) or LCM > R):
print ( "-1" );
return ;
k = (L / / LCM) * LCM;
if (k < L):
k = k + LCM;
for i in range (k,R + 1 ,LCM):
print (i, end = " " );
if __name__ = = '__main__' :
L = 90 ;
R = 280 ;
arr = [ 3 , 5 , 12 ];
N = len (arr);
solve(arr, N, L, R);
|
C#
using System;
public class GFG{
static int __gcd( int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
static void solve( int [] arr, int N, int L, int R)
{
int LCM = arr[0];
for ( int i = 1; i < N; i++)
{
LCM = (LCM * arr[i]) /
(__gcd(LCM, arr[i]));
}
if ((LCM < L && LCM * 2 > R) || LCM > R)
{
Console.WriteLine( "-1" );
return ;
}
int k = (L / LCM) * LCM;
if (k < L)
k = k + LCM;
for ( int i = k; i <= R; i = i + LCM)
{
Console.Write(i + " " );
}
}
public static void Main(String []args)
{
int L = 90;
int R = 280;
int []arr = { 3, 5, 12 };
int N = arr.Length;
solve(arr, N, L, R);
}
}
|
Javascript
<script>
function __gcd(a, b) {
if (b == 0) {
return a;
}
return __gcd(b, a % b);
}
function solve(arr, N, L, R)
{
let LCM = arr[0];
for (let i = 1; i < N; i++)
{
LCM = Math.floor((LCM * arr[i]) /
(__gcd(LCM, arr[i])));
}
if ((LCM < L && LCM * 2 > R) || LCM > R) {
document.write( "-1" );
return ;
}
let k = Math.floor((L / LCM)) * LCM;
if (k < L)
k = k + LCM;
for (let i = k; i <= R; i = i + LCM) {
document.write(i + " " );
}
}
let L = 90;
let R = 280;
let arr = [3, 5, 12];
let N = arr.length;
solve(arr, N, L, R);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...