Number of elements in an array between upper and lower bounds off campus coding question solved with step by step solution in java.
Question:
You are given two integers,num1 and num2. you are also given an array of integers arr of length array_lengt.
How many elements are there in arr such that num1<=arr[i]<=num2 ?
Notes:
It is given that num2>=num1
Function Description:
Complete the numElementsInBetweenInclusive function in the editor below. It has the following parameters
num1: Integer type , the first number you are given.
num2: integer type, the second number you are given.
arr: integer array, the given array
Return: The function must return an INTEGER denoting the number of elements such that num1<=arr[i]<=num2 as specified in the problem statement.
constraints:
- -10^5<=num1<=10^5
- -10^5<=num2<=10^5
- 3<=array_length<=10^5
- -10^5<=arra[i]<=10^5
Input format for debugging
- The first line contains an integer, num1, denoting the first number you are given.
- The second line contains an integer, num2, denoting the second number you are given.
- The next line contains an integer, array_length, denoting the number of elements in an array.
- Each line i of array_length subsequent lines (where 0<=i<array_length) contains an integer describing arr[i].
Sample Test Cases
Example1:
input:
num1=-3
num2=-1
arr={1,2,3}
Output:
0
Explanation:
There are no numbers in arr such that num1<=arr[i]<=num2. Hence the output is 0.
Example2:
input:
num1=5
num2=10
arr={1,2,3,4,5}
output:
There is 1 number in arr such that num1<=arr[i]<=num2. hence the output is 1.



Algorithm to solve
- Take in the input from user that is num1, num2, arr
- Implement the condition num1<=arr[i]<num2 using while loop and if conditions
- keep a counter to find how many array elements satisfy the given condition
- at the end just print the counter value which will give the required answer.
JAVA CODE
package arrayPrograms; import java.util.*; class Problem11 { public static void main(String[] args) { // coded by VRASHIKESH PATIL int num1=2; int num2=3; int[] array1= {0,1,2,3,4,5}; int count=0; for(int i=0;i<array1.length;i++) { if(array1[i]>num1 || array1[i]==num1) { if(array1[i]<num2 || num2==array1[i]) { count++; } } } System.out.println(count); } }
Watch Complete Explanation for Above Code Below
Similar Off Campus Coding Questions
Accenture off campus coding question
Count Buildings Hacker Earth coding question with solution
Pingback: Saddle points in 2D array off campus problem solved - Is It Actually
Pingback: Reducing Dishes coding Complete solution - Is It Actually