Largest element in an array GFG

 package com.company;


public class ARRAY2 {
// Sir's efficient approach
public static int getLargest(int[] arr){
int res = 0;
for(int i = 1; i < arr.length; i++){
if(arr[i] > arr[res]){
res = i;
}
}
return res;
}
// Finding the index of the largest number in an array
public static int largestElement(int[] arr, int n) {
int max = 0;
for (int i = 0; i < n - 1; i++) {
max = (int) Math.max(arr[i], arr[i + 1]);
}
for (int j = 0; j < n; j++) {
if (arr[j] == max) {
return j;
}
}
return 0;
}

public static void main(String[] args) {
int[] a = {10,0,5,6,7,98};
System.out.println(getLargest(a));
}
}

Comments