-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathminAbsoluteDifference.cpp
More file actions
64 lines (41 loc) · 1.15 KB
/
minAbsoluteDifference.cpp
File metadata and controls
64 lines (41 loc) · 1.15 KB
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
// Min. Absolute Difference In Array
// Given an integer array A of size N, find and return the minimum absolute difference between any two elements in the array.
// We define the absolute difference between two elements ai, and aj (where i != j ) is |ai - aj|.
// Input format :
// Line 1 : Integer N, Array Size
// Line 2 : Array elements (separated by space)
// Output Format :
// Minimum difference
// Constraints :
// 1 <= N <= 10^6
// Sample Input :
// 5
// 2 9 0 4 5
// Sample Input :
// 1
#include <bits/stdc++.h>
using namespace std;
// arr - input array
// n - size of array
int minAbsoluteDiff(int arr[], int n) {
/* Don't write main().
* Don't read input, it is passed as function argument.
* Return output and don't print it.
* Taking input and printing output is handled automatically.
*/
sort(arr, arr+n);
int minDiff = INT_MAX;
for(int i = 1; i < n; i++) {
minDiff = min(minDiff, arr[i] - arr[i-1]);
}
return minDiff;
}
int main() {
int size;
cin >> size;
int *input = new int[1 + size];
for(int i = 0; i < size; i++)
cin >> input[i];
cout<< minAbsoluteDiff(input,size) << endl;
return 0;
}