Created
January 26, 2021 05:46
-
-
Save sritasngh/9dad9e76927e4034cab20c1dbdc3808d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<bits/stdc++.h> | |
| using namespace std; | |
| int main(){ | |
| int n; | |
| long long min_diff=10e7; | |
| long long current_diff=0; | |
| cin>>n; | |
| vector<long long> arr(n); | |
| for (int i=0;i<n;i++) | |
| cin>>arr[i]; | |
| sort(arr.begin(),arr.end()); | |
| for(int j=1;j<n;j++){ | |
| current_diff=arr[j]>arr[j-1]?arr[j]-arr[j-1]:arr[j-1]-arr[j]; | |
| if(current_diff<min_diff) | |
| min_diff=current_diff; | |
| } | |
| for(int k=1;k<n;k++){ | |
| if(arr[k]>arr[k-1] && arr[k]-arr[k-1]==min_diff) | |
| cout<<arr[k-1]<<" "<<arr[k]<<" "; | |
| else if(arr[k]<arr[k-1] && arr[k-1]-arr[k]==min_diff) | |
| cout<<arr[k-1]<<" "<<arr[k]<<" "; | |
| } | |
| return 0; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
problem url: https://www.hackerrank.com/challenges/closest-numbers/problem?utm_campaign=challenge-recommendation&utm_medium=email&utm_source=7-day-campaign