Minimum difference pair | Practice | GeeksforGeeks

Given an unsorted array, find the minimum difference between any pair in given array.
 
Example 1:
Input: nums = [2, 4, 5, 9, 7]
Output: 1
Explanation: Difference between 5 and 4 is 1.

Example 2:
Input: nums = [3, 10, 8, 6]
Output: 2
Explanation: Difference between 8 and 6 is 2.

 
Your Task:
You don't need to read or print anything. Your task is to complete the function minimum_difference() which takes the array as input parameter and returns the minimum difference  between any pair in given array.

Expected Time Compelxity: O(N* log(N)) where N is length of array.
Expected Space Complexity: O(1)
 
Constraints:
2 <= N <= 105
1 <= nums[i] <= 109
0
skzaid96411 day ago
Java code:
 
public int minimum_difference(int[] nums)
    {
        Arrays.sort(nums);
        int min=Integer.MAX_VALUE;
        for(int i=1;i<nums.length;i++)
            min=Math.min((nums[i]-nums[i-1]),min);
        return min;
    }
0
thecipher1 week ago
	int  minimum_difference(vector<int>nums){
   	   sort(nums.begin(),nums.end());
   	    int min=INT_MAX;
   	    for(int i=1;i<nums.size();i++)
   	    {
   	        if(abs(nums[i]-nums[i-1])<min)
   	        {
   	            min=abs(nums[i]-nums[i-1]);
   	        }
   	        
   	    }
   	    return min;
   	} 
0
thecipher1 week ago
//this is also correct
int  minimum_difference(vector<int>nums){      sort(nums.begin(),nums.end());       int min=INT_MAX;       for(int i=1;i<nums.size();i++)       {           if(abs(nums[i]-nums[i-1])<min)           {               min=abs(nums[i]-nums[i-1]);           }                  }       return min;   } 
0
nittyswami011 week ago
 public int minimum_difference(int[] nums)   {       // code        int min=0;       int d =Integer.MAX_VALUE;       Arrays.sort(nums);       for(int i=0;i<nums.length-1;i++){           min=nums[i+1]-nums[i];           if(min<d){               d=min;           }       }       return d;
0
vikasrajpoot4791 week ago
//easy c++ solution using sorting algo
int  minimum_difference(vector<int>nums){       // Code here       int ans=INT_MAX;       sort(nums.begin(),nums.end());       for(int i=0;i<nums.size()-1;i++)       {           int check=nums[i+1]-nums[i];           if(check<ans)           {               ans=check;           }       }       return ans;   } 
0
gupta2411sumit1 month ago
 int  minimum_difference(vector<int>nums){       // Code here              sort( nums.begin() , nums.end()) ;              int d = INT_MAX  ;              for( int i = 0 ; i<nums.size()-1 ; i++)       {           if(nums[i+1]-nums[i]<d)           {               d = nums[i+1]-nums[i] ;           }       }              return d ;   } 
0
subhammahanty2352 months ago
C++ , 100% accurate code
class Solution{
public:
   int  minimum_difference(vector<int>nums){
       sort(nums.begin(), nums.end());
       vector<int>v;
       for(int i =0;i<nums.size()-1;i++){
           v.push_back(nums[i+1]-nums[i]);
       }
       sort(v.begin() , v.end());
       return (v[0]);
   }    
};
0
ishantgarg20203 months ago
Go through this to get more understanding of problem
https://youtu.be/ZO01593O0u0
0
mayank20213 months ago
pythonclass Solution:def minimum_difference(self, nums): nums.sort() min1=abs(nums[0]-nums[1]) for i in range(1,len(nums)-1):     if(abs(nums[i]-nums[i+1])<min1):         min1=abs(nums[i]-nums[i+1]) return min1
0
radhika31120013 months ago
  int  minimum_difference(vector<int>nums){        vector<int> y;int n=nums.size();sort(nums.begin(),nums.end()); for(int i=1;i<n;i++) {    int x=nums[i]-nums[i-1];    y.push_back(x); } int min = *min_element(y.begin(), y.end()); return min;}      
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.