Below hackerearth qn has been asked in one of coding qns
Farthest from zero
You are given an integer array A of size N.
Task Write a program to print the farthest element from 0. If there are multiple elements, print the number with the least value.
Input format
- The first line contains a single integer N denoting the size of the array A.
- The next line contains N integers denoting the elements of the array A.
Output format
Print the farthest element from 0.
Sample input 1
5
1 2 3 4 5
Sample Output1
5
Solution prepared by me:
public static farthestfromzero(int N, int [] Arr) {
TreeSet<Integer> ts = new TreeSet<Integer>();
for (int i=0; i<N; i++){
ts.add(Arr[i]);
}
return ts.last();
}
Ask: This solution worked for me for the initial scenario, but when I submitted it , it didn't worked.