Largest Element in Array -GFG Problem
Given an array arr[]. The task is to find the largest element and return it.
Examples:
Input: arr[] = [1, 8, 7, 56, 90]
Output: 90
Explanation: The largest element of the given array is 90.
Input: arr[] = [5, 5, 5, 5]
Output: 5
Explanation: The largest element of the given array is 5.
Input: arr[] = [10]
Output: 10
Explanation: There is only one element which is the largest.
code
//{ Driver Code Starts
import java.io.*;
import java.util.*;
class IntArray {
public static int[] input(BufferedReader br, int n) throws IOException {
String[] s = br.readLine().trim().split(" ");
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = Integer.parseInt(s[i]);
return a;
}
public static void print(int[] a) {
for (int e : a) System.out.print(e + " ");
System.out.println();
}
public static void print(ArrayList<Integer> a) {
for (int e : a) System.out.print(e + " ");
System.out.println();
}
}
class GFG {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t;
t = Integer.parseInt(br.readLine());
while (t-- > 0) {
String input = br.readLine();
String[] inputs = input.split(" ");
int[] arr = new int[inputs.length];
for (int i = 0; i < inputs.length; i++) {
arr[i] = Integer.parseInt(inputs[i]);
}
Solution obj = new Solution();
int res = obj.largest(arr);
System.out.println(res);
System.out.println("~");
}
}
}
// } Driver Code Ends
class Solution {
public static int largest(int[] arr) {
// code here
int max=0;
for(int i=0;i<arr.length;i++)
{
if(arr[i]>max)
{
max=arr[i];
}
}
return max;
}
}
Comments
Post a Comment