Find Number of Array Elements Smaller than a Given Number in Java



An array is a linear data structure in which elements are stored in contiguous memory locations.

As per the problem statement, finding number elements smaller than a given number means we need to compare and count only the smaller elements in the array.

Let's explore the article to see how it can be done by using Java programming language.

To show you some instances

Instance-1

Suppose we have the below array

[10, 2, 3, -5, 99, 12, 0, -1]
and the number is 9
Now the number of elements that are smaller than 9 are
[2, 3, -5,0, -1] = 5 elements

Instance-2

Suppose we have the below array

[55, 10, 29, 74, 12, 45, 6, 5, 269]
and the number is 50
Now the number of elements that are smaller than 50 are
[10, 29, 12, 45, 6, 5] = 6

Instance-3

Suppose we have the below array

[556, 10, 259, 874, 123, 453, -96, -54, -2369]
and the number is 0
Now the number of elements that are smaller than 0 are
[-96, -54, -2369] = 3

Algorithm

Algorithm-1

  • Step 1 ? Store the array elements

  • Step 2 ? Use a for loop to traverse through all array elements.

  • Step 3 ? Compare all elements with the number

  • Step 4 ? Use a counter to count all elements that are smaller than that number and print the count.

Algorithm-2

  • Step 1 ? Store the array elements

  • Step 2 ? Sort the array.

  • Step 3 ? Compare and find the index of the element larger than the given number

  • Step 4 ? To find the number of elements smaller than the given number we print the index we obtained.

Syntax

To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length.

Below refers to the syntax of it ?

array.length

where, ?array' refers to the array reference.

You can use the Arrays.sort() method to sort the array in ascending order.

Arrays.sort(array_name);

Multiple Approaches

We have provided the solution in different approaches.

  • Without Using Sorting

  • By Using Sorting

Let's see the program along with its output one by one.

Approach-1: Without Using Sorting

In this approach, we use a for loop to compare all elements with the number and only count the smaller ones.

Example

Open Compiler
public class Main { public static void main(String[] args) { // The array elements int arr[] = { 556, 10, 259, 874, 123, 453, -96, -54, -2369}, num = 0; System.out.println("The array elements are-"); // Print the array elements for (int i : arr) { System.out.print(i + ", "); } // The counter two count all elements smaller than the number int count = 0; // Count all elements smaller than num for (int i = 0; i < arr.length; i++) { if (arr[i] < num) { count++; } } System.out.println("\nThe number of array elements that are smaller than " + num + " are " + count); } }

Output

The array elements are-
556, 10, 259, 874, 123, 453, -96, -54, -2369, 
The number of array elements that are smaller than 0 are 3

Approach-2: By Using Sorting

In this approach, we sort the array using Arrays.sort() method and then find the index of the first occurrence where the element is larger than the number. The index is the number of elements that are lesser than the number.

Example

Open Compiler
import java.util.Arrays; public class Main{ public static void main(String[] args) { // The array elements int arr[] = { 556, 10, 259, 874, 123, 453, -96, -54, -2369}, num = 20; System.out.println("The array elements are-"); // Print the array elements for (int i : arr) { System.out.print(i + ", "); } // Sort the array Arrays.sort(arr); // Find the index of the first element in the array greater than the given number int index = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] > num) { index = i; break; } } // To find the number of elements smaller than // the number we print the index we onbtained System.out.println("\nThe number of array elements that are lesser than " + num + " are " + (index)); } }

Output

The array elements are-
556, 10, 259, 874, 123, 453, -96, -54, -2369, 
The number of array elements that are lesser than 20 are 4

In this article, we explored how to find the number of elements smaller than a given number in an array by using Java programming language.

Updated on: 2023-01-31T16:47:10+05:30

769 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements