
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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
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
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.
- Related Articles
- Find Elements Greater Than a Given Number In a Subarray in Java
- Number of elements smaller than root using preorder traversal of a BST in C++
- Number of elements less than or equal to a given number in a given subarray in C++
- Find the number of elements greater than k in a sorted array using C++
- Number of smaller and larger elements - JavaScript
- How to find all pairs of elements in Java array whose sum is equal to a given number?
- Find largest number smaller than N with same set of digits in C++
- Count number of elements between two given elements in array in C++
- Program to return number of smaller elements at right of the given list in Python
- Numbers smaller than the current number JavaScript
- Java Program to display a prime number less than the given number
- Constructing an array of smaller elements than the corresponding elements based on input array in JavaScript
- How to count the number of elements in an array below/above a given number (JavaScript)
- Find if sum of odd or even array elements are smaller in Java
- C++ Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself
