Advertisement
satishfrontenddev5

Untitled

Jan 6th, 2024
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Find the minimum difference possible between any two elements in the given array.
  3.  
  4. Input format
  5. There are 2 lines of input.
  6.  
  7. First line will contain a single integer n representing the size of the array.
  8.  
  9. Second line will contain n space separated integers representing the array.
  10.  
  11. Output format
  12. Output the answer in single line.
  13.  
  14. Sample Input 1
  15. 3
  16.  
  17. 1 2 4
  18.  
  19. Sample Output 1
  20. 1
  21.  
  22. Explanation 1
  23. 2 - 1 = 1 minimum difference
  24.  
  25. Constraints
  26. 2<=n<=100000
  27.  
  28. 1<=A[i]<=1000000000
  29. */
  30.  
  31. /**
  32.  * @param {number} n
  33.  * @param {number[]} arr
  34.  * @return {number}
  35.  */
  36. function minDiff(n, arr) {
  37.   let minDiff=1e9;
  38.   arr.sort((a,b)=>a-b);
  39.   for(let i=1;i<n;i++){
  40.       minDiff=Math.min(minDiff,arr[i]-arr[i-1]);
  41.   }
  42.   return minDiff;
  43. //   console.log(arr)
  44. //   return arr[1]-arr[0];
  45. }
  46.  
  47. function main() {
  48.     let n = parseInt(readLine());
  49.     let arr = readIntArr();
  50.  
  51.     let minimumDifference = minDiff(n, arr);
  52.     print(minimumDifference)
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement