-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinimum Difference in an Array.cpp
71 lines (67 loc) · 1.51 KB
/
Minimum Difference in an Array.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Minimum Difference in an Array
// Easy
// 0/40
// Asked in companies
// Problem statement
// Given an array of integers, print the minimum of the absolute difference of all possible pairs of elements.
// Example :
// N = 5
// ARR = [ 3, -6, 7, -7, 0 ]
// Out of all pairs, (-7,-6) have a difference of ‘1’, and no other pair has less difference. So ‘ANS’ is ‘1’.
// Detailed explanation ( Input/output format, Notes, Images )
// Constraints :
// 1 <= T <= 10
// 2 <= N <= 10^5
// -10^8 <= ARR[i] <= 10^8
// Sum of N <= 10^5
// Time Limit: 1 sec
// Sample Input 1 :
// 2
// 4
// 1 8 3 10
// 2
// 5 5
// Sample Output 1 :
// 2
// 0
// Explanation Of Sample Input 1 :
// For test case 1,
// Out of all pairs (1,3) and (8,10) have the minimum difference ‘2’ so the answer is ‘2’.
// For test case 2,
// There is only one possible pair (5,5) so the answer is ‘0’.
// Sample Input 2 :
// 2
// 3
// 8 1 8
// 2
// -3 3
// Sample Output 2 :
// 0
// 6
// C++ (g++ 11)
// 00
// :
// 00
// :
// 22
// 12345678910
// #include <bits/stdc++.h>
// int minDiff(int n, vector < int > arr) {
// // Write your code here.
// sort(arr.begin(),arr.end());
// int minD=INT_MAX;
// for(int i=1;i<n;i++){
// minD=min(minD,arr[i]-arr[i-1]);
// }
// return minD;
// }
#include <bits/stdc++.h>
int minDiff(int n, vector < int > arr) {
// Write your code here.
sort(arr.begin(),arr.end());
int minD=INT_MAX;
for(int i=1;i<n;i++){
minD=min(minD,arr[i]-arr[i-1]);
}
return minD;
}