Count In Range and Specific in java



Task - 
You will be given an array and a range and you need to count how many array elements lies in that range and not divisible by 3 and 5. 

Input Format - 
You will take an integer as input from STDIN which represent the length of the array and on another line array elements will be there separated by single space. Another line will have the starting point of the range and end point separated by space. 

Output Format 
print the count to the STDOUT. 



/**
* You will be given an array and a range and you need to count
* how many array elements lies in that range and not divisible
* by 3 and 5.
*
* You will take an integer as input from STDIN which represent
* the length of the array and on another line array elements will
* be there separated by single space. Another line will have the
* starting point of the range and end point separated by space
*
* Output Format - print the count to the STDOUT.
Sample Test Case:
Sample Input:-
6
16 17 4 3 5 2
2 10
Sample Output -
2
*
*/
import java.util.*;
public class CountInRangeAndSpecific {
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
int size;
size=sc.nextInt();
int [] arr=new int[size];
for(int i=0;i<size;i++){
arr[i]=sc.nextInt();
}
sc.nextLine();
int start=sc.nextInt(); //starting range
int end=sc.nextInt(); //ending range
int count=0;
for(int a=0;a<arr.length;a++){
for(int b=start;b<=end;b++){
if(arr[a]==b){ //verifying whether array contains the following range
if(arr[a]%3!=0&&arr[a]%5!=0){ //if above condition passed, check whether it is not divisible by 3 and 5.
count++; //if above condition passed, then start counting.
}
}
}
}
System.out.println(count); //display the total count.
sc.close();
}
}



Sample Test Case: 
Sample Input:-
6
16 17 4 3 5 2
2 10

Sample Output 
2

Comments

Popular posts from this blog

Reasoning-Number Series

Profit and Loss

Reasoning-Letter Series