Last Updated on May 16, 2023 by Prepbytes
Java is a popular programming language used for developing a wide range of applications, from simple command-line tools to complex enterprise systems. One common task in programming is finding the largest element of an array. This can be useful in a variety of applications, such as sorting algorithms or data analysis.
How to Find the Maximum Element of the Array
We have different methods to find the maximum element in the array.
Method 1. Using Iteration
Following are the steps to find the maximum element in the array.
- Step 1: Create a variable called max and set the array’s first member as its starting value.
- Step 2: Check after running a loop from index 1 to N.
- Step 3: If arr[i] exceeds max, set max to arr[i].
- Step 4: After complete iteration, print max.
Code Implementation
import java.util.Scanner; public class Main { public static void main(String args[]) { int arr[] = {12, 13, 1, 10, 34, 10}; int max = arr[0]; for(int i=0; i<arr.length; i++) { if(max < arr[i]) { max = arr[i]; } } System.out.print(max); } }
Output
34
Method 2. Using Recursion
Following are the steps to find the maximum element in the array.
- Step 1: Make a recursive function with the name get max (int arr[, int n]).
- Step 2: Basis: If (n==1), return arr[0].
- Step 3: If not, give back max (arr[n-1], getmax(arr, n-1).
Code Implementation
import java.util.*; class Main { static int getmax(int arr[], int n){ if(n==1) return arr[0]; return Math.max(arr[n-1], getmax(arr, n-1)); } public static void main(String args[]) { int arr[] = {12, 13, 1, 10, 34, 10}; int n = arr.length; System.out.print(getmax(arr, n)); } }
Output
34
Method 3: Bottom-up Approach
In this approach, we will call a function findlargest(int arr[], int i, int end)
- Step 1: While calling the function findlargest(int arr[], 0, int end) providing end as the index of the final array element initially
- passing ‘i’ at first as 0.
- Step 2: Recursively read the second-to-last element in an iteration.
- Step 3: between the second-to-last and last array members, find bigger.
- Step 4: Return the outcome to the preceding recursive iteration’s maximum value.
- Step 5: Find the greatest difference between the current array index element and the current maximum value in each of the remaining recursive callups.
- Step 6: Send the final maximum value from the last recursive callup, then print
Code Implementation
import java.util.*; public class Main { static int maximum(int arr[], int i, int end) { int max; if(i == end-1) return (arr[i] > arr[i + 1]) ? arr[i] : arr[i + 1]; max = maximum(arr, i + 1, end); return (arr[i] > max) ? arr[i] : max; } public static void main(String args[]) { int arr[] = {12, 13, 1, 10, 34, 10}; int end = arr.length-1; System.out.print(maximum(arr, 0, end)); } }
Output
34
Conclusion
we have developed a Java program to find the largest element in an array of integers. The program uses a simple approach that involves iterating through the array and keeping track of the largest element seen so far. The program demonstrates the use of basic programming concepts, such as loops, conditional statements, and variable manipulation.
Overall, this program provides a solid foundation for working with arrays in Java and can be a useful tool for developers working on a wide range of applications.
Frequently Asked Questions
Q1. What is an array in Java?
Ans. An array in Java is a collection of similar data types, stored in a contiguous block of memory. Each element in the array is accessed by an index number, starting from zero.
Q2. How do I declare an array in Java?
Ans. You can declare an array in Java by specifying the data type of the elements and the size of the array. For example, to declare an array of integers with 5 elements, you can use the following code: int[] arr = new int[5];
Q3. How do I find the largest element in an array in Java?
Ans. You can find the largest element in an array in Java by iterating through the array and keeping track of the largest element seen so far. You can initialize a variable to the first element in the array, and then compare each subsequent element to this variable, updating the variable if a larger element is found.
Q4. What is the time complexity of the program to find the largest element in an array?
Ans. The time complexity of the program to find the largest element in an array using a simple iterative approach is O(n), where n is the size of the array. This is because the program needs to iterate through each element in the array to find the largest element.
Q5. Can I use this program to find the largest element in a multi-dimensional array?
Ans. No, this program is designed to work with one-dimensional arrays only. To find the largest element in a multi-dimensional array, you will need to modify the program to iterate through all the elements in the array, accounting for the dimensions of the array.