Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Find the minimum difference possible between any two elements in the given array.
- Input format
- There are 2 lines of input.
- First line will contain a single integer n representing the size of the array.
- Second line will contain n space separated integers representing the array.
- Output format
- Output the answer in single line.
- Sample Input 1
- 3
- 1 2 4
- Sample Output 1
- 1
- Explanation 1
- 2 - 1 = 1 minimum difference
- Constraints
- 2<=n<=100000
- 1<=A[i]<=1000000000
- */
- /**
- * @param {number} n
- * @param {number[]} arr
- * @return {number}
- */
- function minDiff(n, arr) {
- let minDiff=1e9;
- arr.sort((a,b)=>a-b);
- for(let i=1;i<n;i++){
- minDiff=Math.min(minDiff,arr[i]-arr[i-1]);
- }
- return minDiff;
- // console.log(arr)
- // return arr[1]-arr[0];
- }
- function main() {
- let n = parseInt(readLine());
- let arr = readIntArr();
- let minimumDifference = minDiff(n, arr);
- print(minimumDifference)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement