-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmin_abs_diff_inArray.cpp
More file actions
47 lines (38 loc) · 872 Bytes
/
min_abs_diff_inArray.cpp
File metadata and controls
47 lines (38 loc) · 872 Bytes
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
// arr - input array
// n - size of array
#include<bits/stdc++.h>
#include <algorithm>
using namespace std;
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.
*/
// int n1 = sizeof(arr)/sizeof(arr[0]);
sort(arr,arr+n);
int x,y;
double dif=1000000;
for(int i=0;i<n-1;i++)
{
x=arr[i];
y=arr[i+1];
if(abs(x-y)<dif)
{
dif=abs(x-y);
}
}
return dif;
}
#include <iostream>
#include "solution.h"
using namespace std;
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;
}