Java O(nlogn) solution:Sorting done once
To do this question,we need to sort the array,after sorting we can find the median by using the formula given in question.\nA value arr[i] is said to be stronge
To do this question,we need to sort the array,after sorting we can find the median by using the formula given in question.\nA value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array.\nSince we need to find stronger values,we can see from the above expression that a value is strong if its distance from median is greater.For eg:the distance of median from itself is zero.\nFor example:\narr=[1,1,3,4,5], k = 2\nThe median here is 3, and our strongest 2 elements are going to be [1,5]. After sorting we can see that the elements at extreme ends have max distance from median.So we can simply check which of the elements at extreme end(start vs end) has a greater distance from median.\nAnd to account for |arr[i] - m| == |arr[j] - m|, as given in question that arr[i] is said to be stronger than arr[j] if arr[i] > arr[j],so we can just simply consider the last element as the array is sorted and it will definitely be greater than starting element.\n\nAs the sorting takes O(nlogn)) time and the while loop will run for O(k),the total time complexity is O(nlogn)\n\n\n```\nclass Solution {\n    public int[] getStrongest(int[] arr, int k) {\n        Arrays.sort(arr);  \n        int median=arr[(arr.length-1)/2];\n        int i=0;\n        int j=arr.length-1;\n        int res=0;\n        int[] result=new int[k];\n        while(res<k)\n        {\n\t\t//while loop will stop after we hace chosen the k greatest elements.\n            if(Math.abs(arr[i]-median)>Math.abs(arr[j]-median))\n            {\n                result[res]=arr[i];\n                i++;\n                res++;\n            }\n            else{\n                result[res]=arr[j];\n                j--;\n                res++;\n            }\n        }\n        return result;\n        \n    }\n}\n```\nHope this helps someone!
['Java']