Tough Competitors | Practice | GeeksforGeeks

There are n competitors in an exam. Each competitor has his own skill value which is given by the array arr where arr1 is the skill of the first competitor, arr2 is the skill of second competitor and so on. Two competitors are said to be tough competitors if their skill difference is least i.e. they are very close in their skill values. Given n and an array arr as input, find the tough competitors among the n competitors and print the absolute of the difference of their skill values.
Example 1:
Input:
n = 4
arr[] = {9, 4, 12, 6}
Output: 2
Explanation: As |9-4|=5, |9-12|=3, |9-6|=3,
|4-12|=8, |4-6|=2, |12-6|=6 so, the tough
competitors are competitors having skill
values 4, 6 having their skill difference
as 2.

Example 2:
Input:
n = 5
arr[] = {4, 9, 1, 32, 12}
Output: 3
Explanation: Tough competitors are
competitors having skill values (4,1)
and (9,12) having their skill difference
as 3.

Your Task:
You don't need to read input or print anything. Your task is to complete the function toughCompetitor() which takes the array of integers arr and n as parameters and returns an integer denoting the answer.
Expected Time Complexity: O(n*logn)
Expected Auxiliary Space: O(1)
Constraints:
2 <= n <= 105
1 <= arr[i] <= 106
0
arvind16yadav2 weeks ago
/*JAVA CODE*/
int toughCompetitor(int[] arr, int n) {       // code here              Arrays.sort(arr);              int min = arr[1] - arr[0];              for(int i=1; i<n-1; i++){           int temp = arr[i+1] - arr[i];           if(min > temp){               min = temp;           }       }              return min;   }
0
codewithshoaib193 weeks ago
 int toughCompetitor(int[] arr, int n) {       // code here       Arrays.sort(arr);       int x=Integer.MAX_VALUE;       int abs=0;       for(int i=0;i<n-1;i++){            abs=Math.abs(arr[i]-arr[i+1]);            if(abs<x){                x=abs;            }       }       return x;   }
0
hasnainraza1998hr2 months ago
C++, 0.3
int toughCompetitor(int *arr, int n) {       sort(arr,arr+n);       int min = INT_MAX;       for(int i=1;i<n;i++){           if(arr[i]-arr[i-1]<min)               min = arr[i]-arr[i-1];       }       return min;   }
0
ravichaunal309002 months ago
 l=[]       for i in range(len(arr)):           for j in range(1,len(arr)):               if arr[i]==arr[j]:                   break               else:                   p=abs(arr[i]-arr[j])                   l.append(p)       return min(l)  
test case fail please help
0
vidhigupta123 months ago
int toughCompetitor(int *arr, int n) {       // code here       sort(arr,arr+n);       int ans=INT_MAX;       for(int i=0;i<n-1;i++)       {          int diff=abs(arr[i]-arr[i+1]);          ans=min(diff,ans);       }       return ans;   }
0
cshubham4396 months ago
 int min = Integer.MAX_VALUE;       int dif = 0;       Arrays.sort(arr);       for(int i = 0; i<n-1; i++){                      dif = Math.abs(arr[i] - arr[i+1]);           if(dif<min)           min = dif;       }       return min;
+1
badgujarsachin836 months ago
int toughCompetitor(int *arr, int n) {
        // code here
        sort(arr,arr+n);
        int min=abs(arr[0]-arr[1]);
        int di=0;
        for(int i=1;i<n;i++){
            di=abs(arr[i]-arr[i+1]);
            if(di<min){
                min=di;
            }
        }
        return min;
    }
0
Aashutosh Jha1 year ago
Aashutosh Jha
int i,j,min=0,diff=0;        sort(arr,arr+n);        min=abs(arr[1]-arr[0]);        for(i=0;i<n;i++) {="" diff="abs(arr[i]-arr[i+1]);" if(diff<min)="" {="" min="diff;" }="" }="" return="" min;="" "execution="" time:0.50"="">
0
Abhishek Gupta1 year ago
Abhishek Gupta
SIMPLE C++ SOL:https://practice.geeksforge...
0
Tulsi Dey2 years ago
Tulsi Dey
Solution in C++:Execution time : 0.01 https://uploads.disquscdn.c...
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.