HackerEarth Question solution failing for the input testcase

Viewed 30083

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.

6 Answers

Your algorithm does not account for negative integers.

Consider this array:

[-10, 7, 5, 1]

Which number is furthest from zero?
Which will your algorithm pick?

Also note, this requirement:

If there are multiple elements, print the number with the least value.

So, from an array like this:

[-10, 1, 2 ,10]

You need to pick -10, and not 10.

That is because the tree set is sorted by the values, the number can be begtive. So... I think it should be this:

public static int farthestfromzero(int N, int [] Arr) {
    TreeSet<Integer> ts = new TreeSet<Integer>();
    for (int i=0; i<N; i++){
          ts.add(Arr[i]);
    } 
  int maxV = ts.last();
  int minV = ts.first();
  
  if(Math.abs(minV) >= maxV){
      return minV;
  }
  return maxV;

}

Also if it's memory exceed, then try this:

    public static int farthestfromzero(int N, int [] Arr) {//You don't really need to store every elements
        int best = 0;
        
        for(int i = 0;i<N;i++) {
            if(Math.abs(Arr[i]) > Math.abs(best)) {
                best = Arr[i];
            }else if(Math.abs(best) == Math.abs(Arr[i]) && best > Arr[i]) {
                best = Arr[i];
            }
        }
        return best;    
    }

Here is my answer. I just wrote the core logic. Please add the basic condition check. Also, I did it in Python3. Commented code the expansion of the below single line code.

A1 = sorted(A)
print(A1[0]) if (abs(A1[0])> abs(A1[-1])) else print(A1[-1]) if (abs(A1[0]) < abs(A1[-1])) else print(A1[0])

# if (abs(A1[0])== abs(A1[-1])): 
#     print(A1[0])
# else:
#     if(abs(A1[0]) > abs(A1[-1])):
#         print(A1[0])
#     else:
#         print(A1[-1])

I have written a function in JAVA that returns lesser maximum value element if there are any duplicates.

import java.util.Arrays;

public static int farthestFromZero(int[] arr) {
    for (int i = 0; i < arr.length; i++) {
        arr[i] = Math.abs(arr[i]);
    }
    Arrays.sort(arr);

    int max = 0;
    for (int i = 0; i < arr.length-1; i++) {

        if(arr[i] > max && arr[i] != arr[i+1]){
            max = arr[i];
        }
    }
    return max;
}

Here we need to find the element which is far from 0 considering both +ve and -ve values in an array.

My idea is to just push the forthmost element to the last position of an array.

Sample Inputs and Outputs:

Input: int[] arr = new int[] { -100, 90, -80, 500, -2, 50 };

Output: 500

Input: int[] arr = new int[] { -100, 90, 80, 50, -200, 2 };

Output: -200

private static int getForthMostWithoutSorting(int[] a) {
        for (int i = 0; i < a.length - 1; i++) {
            int curEle = a[i];
            if (curEle < 0) {
                curEle = -a[i];
            }
            int nextEle = a[i + 1];
            if (nextEle < 0) {
                nextEle = -a[i + 1];
            }
            if (curEle > nextEle) {
                int temporary = a[i];
                a[i] = a[i + 1];
                a[i + 1] = temporary;
            }
        }
        return a[a.length - 1];
    }
public class FarthestFromZero {

    public static void main(String[] args) {

        int[] input = { 1, 2, 3, 4, 10, 10 };
        boolean flag = true;
        TreeSet<Integer> toTreeSet = new TreeSet<Integer>();

        for (Integer eachVal : input) {

            if (!toTreeSet.contains(eachVal)) {
                
                toTreeSet.add(eachVal);
                
            } else {

                flag = false;
            }

        }

        if (flag) {
            System.out.println("No duplicate values in array: " + toTreeSet.last());
        } else {

            System.out.println("it has duplicate values in array: " + toTreeSet.first());

        }

    }

}
Related