Minimum Distances | Hackerrank Solutions in C++

Minimum Distances | Hackerrank Solution

           
 It is an hackerrank problem you can find it by clicking here.
Below given solution is prepared by one of the coder of our.
                           
                                               Soultion in C++

#include<iostream>

using namespace std;

int main()
{
    int n,min=1000;
    cin>>n;
    int *arr = new int [n];
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        for(int j=0;j<i;j++)
        {
            if(arr[i]==arr[j])
                {int d=i-j;
                    if(d<min)min=d;}
        }
    }
    if(min==1000)
    cout<<"-1";
    else 
    cout<<min;
    return 0;
}

Hope your doubt regarding this problem is clear.

This solution is POWERED By : Coding Winds

Comments

Popular posts from this blog

Apple and Orange | Hackerrank Solution in C++

Maximum Draws | Hackerrank Solution in C++