countNumbers: Given an array and two numbers L and R, the task is to write a program which prints the count of numbers in the given range L – R which are divisible by all the numbers in the array.
Suppose the given array is arr[] = {1, 2, 3, 5} and the range is 1 – 30, then there is only one number(30) in this range that is divisible by all the elements of the array, so the output will be 1.
Input:
4
1 2 3 5
1 30
Output:
1
Solution
import java.util.*;
class Perfectice
{
static int countNumbers(int arr[], int n, int l, int r)
{
//write your code here
int count = 0;
for (int i = l; i <= r; i++) {
boolean flag = true;
for (int j = 0; j < n; j++) {
if (i % arr[j] != 0) {
flag = false;
break;
}
}
if (flag) {
count++;
}
}
return count;
}
public static void main(String args[])
{
int n,l,r;
Scanner sc = new Scanner(System.in);
n=sc.nextInt();
int arr[] = new int[n];
for(int i=0;i<n;i++)
arr[i] = sc.nextInt();
l = sc.nextInt();
r = sc.nextInt();
System.out.println(countNumbers(arr, n, l, r));
}
}Happy Learning – If you require any further information, feel free to contact me.
![[Solved] countNumbers Given an array and two numbers L and R, the task is to write a program which prints the count of numbers in the given range with Java [Solved] countNumbers Given an array and two numbers L and R, the task is to write a program which prints the count of numbers in the given range with Java](https://realcoder.techss24.com/wp-content/uploads/2022/08/Solved-countNumbers-Given-an-array-and-two-numbers-L-and-R-the-task-is-to-write-a-program-which-prints-the-count-of-numbers-in-the-given-range-with-Java.png)
![[Solved] 3ple Newton's Triwizard Contest with Java](https://realcoder.techss24.com/wp-content/uploads/2022/11/Solved-3ple-Newtons-Triwizard-Contest-with-Java-300x197.png)
![[Solved] Calculating Gain Percentage with Java](https://realcoder.techss24.com/wp-content/uploads/2022/10/Solved-Calculating-Gain-Percentage-with-Java-300x200.png)
![[Solved] Kth Smallest Difference Contest Problem](https://realcoder.techss24.com/wp-content/uploads/2022/06/Solved-Kth-Smallest-Difference-Contest-Problem-300x200.png)