- 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 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.

- Related Articles
- Number of elements less than or equal to a given number in a given subarray in C++
- Find Number of Array Elements Smaller than a Given Number in Java
- Mask array elements greater than a given value in Numpy
- Find the Next perfect square greater than a given number in C++
- Find Array Elements Which are Greater than its Left Elements in Java?
- Find the number of elements greater than k in a sorted array using C++
- Mask array elements greater than or equal to a given value in Numpy
- Largest subarray having sum greater than k in C++
- Python Program to find out the number of sets greater than a given value
- Number of nodes greater than a given value in n-ary tree in C++
- Number of elements greater than modified mean in matrix in C++
- How to calculate the number of elements greater than a certain value in a vector in R?
- Python - Find words greater than given length
- Program to find out the number of consecutive elements in a matrix whose gcd is greater than 1 in Python
- Queries for number of distinct elements in a subarray in C++