i've a problem that asks me to print the index of the integers whose difference is smallest. If the inputs are 5
and 10 12 13 15 10
, I have to print 5 1
. The problem is here. Below is my code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int n;
int temp=1005,pos1=0,pos2=0;
cin>>n;
vector <int> vec;
for(int i=0;i<n;i++){
int in; cin>>in;
vec.push_back(in);
}
for(int i=0;i<n;i++){
for(int j=1;j<n;j++){
if(i==j)break;
int ans=abs(vec[i]-vec[j]);
cout<<i+1<<"-"<<j+1<<" = "<<ans<<endl;
if(ans<temp){
temp=ans;
pos1=i,pos2=j;
}
}
}
cout<<pos1+1<<" "<<pos2+1<<endl;
return 0;
}