Find the number of elements in the list that are strictly less than the k
You are given a list of integers and an integer K.Write an algorithm to find the number of elements in the list that are strictly less than the k.
Input Format:
The first line of input contains element_size, that denotes a number of elements to be present.
The second line of input contains list of elements in the array.
The last line of input contains K representing the integer to be compared.
Outut Format:
Print a positive integer representing the number of elements in the list that are strictly less than K.
Sample Input 1:
7
1 7 4 5 6 3 2
5
Sample Output 1:
4
Solution:
import java.util.Scanner;
/**
* Main
*/
public class Main {
public static int noOfElement(int[] arr, int num) {
int answer = 0;
// write the code here..... lol
for (int i = 0; i < arr.length; i++) {
if (arr[i] < num)
answer++;
}
return answer;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int element_size = sc.nextInt();
int[] element = new int[element_size];
for (int i = 0; i < element.length; i++) {
element[i] = sc.nextInt();
}
int num = sc.nextInt();
sc.close();
System.out.println(noOfElement(element, num));
}
}

0 Comments