-1

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;
}
Maisha
  • 45
  • 1
  • 1
  • 8
  • what's the problem? ("it does not work" is not an answer)) – Alberto Sinigaglia Jul 12 '20 at 13:29
  • it gives wrong output at times and doesnt get accepted. i dont understand where's the flaw here rn – Maisha Jul 12 '20 at 13:30
  • 1
    I think that you might've missed the part where it tells you that the soldiers are standing in a circle. Also, `i` gets updated every iteration, so I don't think you want to start `j` from 1. – Tortellini Teusday Jul 12 '20 at 13:32
  • Just as a side note: [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Andreas Wenzel Jul 12 '20 at 13:34
  • why not sort the vector and then do binary serche? – yaodav Jul 12 '20 at 13:37
  • Your loop does not examine neighbouring elements. The first iteration compares the first element to all other elements. The second exits immediately. The third iteration compares the second and third elements. The fourth compares the fourth element to the second and third. Work out an algorithm first. Draw numbers in a circle on paper and figure out what to do. – molbdnilo Jul 12 '20 at 13:40

2 Answers2

1

Here's my solution in C. Remember that you need only be concerned with neighboring elements and that the soldiers are organized in a circle. Also, I found it a bit weird that the solution is asking for indexes starting at 1 and not 0.

#include <stdio.h>

#define ABS(n) ((n) * (((n) > 0) - ((n) < 0)))

void solution(int n, int soldiers[static 2])
{
    int indexA  = 1,
        indexB  = 2,
        minDiff = ABS(soldiers[0] - soldiers[1]),
        heightDiff;
    
    for (int i = 1; i < n; i++)
    {
        if ((heightDiff = ABS(soldiers[i] - soldiers[(i + 1) % n])) < minDiff)
        {
            minDiff = heightDiff;
            indexA = i + 1;
            indexB = ((i + 1) % n) + 1;
        }
    }
    
    printf("%d %d\n", indexA, indexB);
}

int main()
{
    solution(5, (int[5]){10, 12, 13, 15, 10});
    //5 1
    solution(4, (int[4]){10, 20, 30, 40});
    //1 2

    return 0;
}
bugcatcher9000
  • 201
  • 2
  • 5
0

The soldiers stand in a circle and you are supposed to look at soldiers standing next to each other.

This means you don't need the j loop. You can simply compare vec[i] with vec[(i+1) % n].

Thorsten Kettner
  • 89,309
  • 7
  • 49
  • 73