
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Elements Greater Than a Given Number in a Subarray in Java
In Java, Array is an object. It is a non-primitive data type which stores values of similar data type.
As per the problem statement we have given a sub array and a number and we have to find how many elements in this sub array are greater than that given number. So, we have to compare every element of the array with the given number and print the element if anyone is greater.
Let's explore the article to see how it can be done by using Java programming language.
To Show you Some Instances
Instance-1
Given Array= [12, 23, 34, 45, 15]. Given sub array= [12, 23, 34] Given number = 25 The elements which are greater than the given number = 34
Instance-2
Given Array= [38, 94, 86, 63, 36]. Given sub array= [94, 86, 63, 36] Given number = 90 The elements which are greater than the given number = 94
Instance-3
Given Array= [54, 67, 23, 95, 74, 60]. Given sub array= [23, 95, 74] Given number = 30 The elements which are greater than the given number = 95, 74
Algorithm
Algorithm-1 (Without an Extra Array)
Step 1 ? Declare an array of integer types either by static or user input method.
Step 2 ? Take a for loop and iterate it from the subarray start index to end index and check for the condition if any element is greater than the given number and print that element.
Algorithm-2 (With an Extra Array)
-
Step 1 ? Declare an array of integer type either by static or user input method.
-
Step 2 ? Declare a subarray and get the elements by using start and end index of the subarray.
-
Step 3 ? Take a for loop to print the elements present inside the sub-array if any number is greater than the given number.
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.
Multiple Approaches
We have provided the solution in different approaches.
Without an Extra Array
With an Extra Array
Let's see the program along with its output one by one.
Approach-1: Without an Extra Array
In this approach, we declare and initialize an array and then as per the algorithm we find the numbers greater than a given element in the subarray. Here we do not make use of another extra array i.e. subarray.
Example
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc=new Scanner(System.in); //declared an array int[] inputArray = {2, 6, 1, 7, 9, 3, 5}; System.out.println("Given array: " + Arrays.toString(inputArray)); //index range for sub array int indx1=1; int indx2=5; //number to compare int num= 4; int a=0; System.out.println("Greater numbers in the subarray: "); for(int i=indx1; i<=indx2; i++) { if(inputArray[i]>num) { System.out.println(inputArray[i]+" "); } } } }
Output
Given array: [2, 6, 1, 7, 9, 3, 5] Greater numbers in the subarray: 6 7 9
Approach-2: With an Extra Array
In this approach, we declare and initialize an array and then as per the algorithm we find the numbers greater than a given element in the subarray. Here we make use of another extra array i.e. subarray.
Example
import java.util.*; public class Main{ public static void main(String args[]) { Scanner sc=new Scanner(System.in); //declared an array int[] inputArray = {2, 6, 1, 7, 9, 3, 5, 8}; System.out.println("Given array: " + Arrays.toString(inputArray)); //index range for sub array int indx1=2; int indx2=6; //Declared the subarray int[] subArray = new int[indx2 - indx1 + 1]; int a=0; //store elements in subarray for(int i=indx1; i<=indx2; i++){ subArray[a]=inputArray[i]; a+=1; } System.out.println("Your sub-array: " + Arrays.toString(subArray)); //number to compare int num= 5; System.out.println("Greater numbers present in the subArray: "); greaterNumber(subArray,num); } //method to find greater numbers than a given number public static void greaterNumber(int[] arr,int n) { for(int i=0; i<arr.length; i++) { if(arr[i]>n) { System.out.println(arr[i]+" "); } } } }
Output
Given array: [2, 6, 1, 7, 9, 3, 5, 8] Your sub-array: [1, 7, 9, 3, 5] Greater numbers present in the subArray: 7 9
In this article, we explored how to find the elements greater than a given number in a subarray by using Java programming language.