In the previous article, we have seen Java Program to Delete All 0 Element Values from an Array of Integers
In this article we will see how to find number of elements greater than a given number in a given sub array using Java programming language.
Java Program to Find Number of Elements Greater Than a Given Number in a Given Sub Array
Prerequisite:
See below articles to know more about Array, array declaration, array instantiation and array initialization.
- How to Declare an Array in Java?
- How to Instantiate an Array in Java?
- How to Initialize an Array in Java?
Let’s see different ways how to find number of elements greater than a given number in a given sub array.
Method-1: Java Program to Find Number of Elements Greater Than a Given Number in a Given Sub Array By Static Initialization of Array Elements
Approach:
- Declare an array say
arr[]
with numbers as array elements. - Enter a specific number and store it in an integer variable say int
number
which will be used for comparison. - Declare two integer variables say
start
andend
and assign the start index and end index value to it by taking user input. - Then compare the specific number with all the elements of the sub array.
- If any sub array element is greater than that specific number then print it as well as keep track on number of sub array elements which are greater than that specific element by using an integer variable say
count
. - At last print the value of
count
as well.
Program:
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); //array declared int arr[]={4,6,1,8,2,9,7,3,5}; // Displaying the original array System.out.print("Original array: "); //printing the array for(int i = 0; i < arr.length; i++) { System.out.print(arr[i]+" "); } System.out.println(); //Asking the user to take input of a number //We will find sub array elements which are greater than this number System.out.print("Enter a specific number: "); int number=sc.nextInt(); //Entering the start and end index value for sub array System.out.print("Enter sub array start index: "); int start=sc.nextInt(); System.out.print("Enter sub array end index: "); int end=sc.nextInt(); // Displaying the sub array System.out.print("Sub array: "); //printing the array for(int i = start; i <= end; i++) { System.out.print(arr[i]+" "); } System.out.println(); System.out.print("The numbers which are greater than "+number+" are:"); //count value initialized as 0 //to keep track on number of elements which are greater than the specific number. int count=0; for(int i = start; i <= end; i++) { if(arr[i]>number) { System.out.print(arr[i]+" "); count++; } } System.out.println(); System.out.print("So, total "+count+" elements are greater than "+number+" in the given sub array"); } }
Output: Original array: 4 6 1 8 2 9 7 3 5 Enter a specific number: 8 Enter sub array start index: 1 Enter sub array end index: 6 Sub array: 6 1 8 2 9 7 The numbers which are greater than 8 are:9 So, total 1 elements are greater than 8 in the given sub array
Method-2: Java Program to Find Number of Elements Greater Than a Given Number in a Given Sub Array By Dynamic Initialization of Array Elements
Approach:
- Create scanner class object.
- Ask user for input of array length.
- Initialize the array with given size.
- Ask the user for input of array elements.
- Display the original array
arr[]
. - Enter a specific number and store it in an integer variable say int
number
which will be used for comparison. - Declare two integer variables say
start
andend
and assign the start index and end index value to it by taking user input. - Then compare the specific number with all the elements of the sub array.
- If any sub array element is greater than that specific number then print it as well as keep track on number of sub array elements which are greater than that specific element by using an integer variable say
count
. - At last print the value of
count
as well.
Program:
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("Enter the number of elements in the array: "); int num = sc.nextInt(); int arr[] = new int[num]; System.out.print("Enter the elements: "); for (int i = 0; i < num; i++) { arr[i] = sc.nextInt(); } // Displaying the original array System.out.print("Original array: "); //printing the array for(int i = 0; i < arr.length; i++) { System.out.print(arr[i]+" "); } System.out.println(); //Asking the user to take input of a number //We will find sub array elements which are greater than this number System.out.print("Enter a specific number: "); int number=sc.nextInt(); //Entering the start and end index value for sub array System.out.print("Enter sub array start index: "); int start=sc.nextInt(); System.out.print("Enter sub array end index: "); int end=sc.nextInt(); // Displaying the sub array System.out.print("Sub array: "); //printing the array for(int i = start; i <= end; i++) { System.out.print(arr[i]+" "); } System.out.println(); System.out.print("The numbers which are greater than "+number+" are:"); //count value initialized as 0 //to keep track on number of elements which are greater than the specific number. int count=0; for(int i = start; i <= end; i++) { if(arr[i]>number) { System.out.print(arr[i]+" "); count++; } } System.out.println(); System.out.print("So, total "+count+" elements are greater than "+number+" in the given sub array"); } }
Output: Enter the number of elements in the array: 6 Enter the elements: 1 2 3 4 5 6 Original array: 1 2 3 4 5 6 Enter a specific number: 4 Enter sub array start index: 2 Enter sub array end index: 5 Sub array: 3 4 5 6 The numbers which are greater than 4 are:5 6 So, total 2 elements are greater than 4 in the given sub array
The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.
Related Java Articles: