Saturday, July 25, 2015

Repetition of an element in array within k distance

#include <fstream>
#include <iostream>
using namespace std;

int hash(int h[],int a)
{

if(h[a])
return 1;
h[a]++;
return 0;
}
int main()
{
int a[]={2,1,2,4,3,2,1};
int n=sizeof(a)/sizeof(a[0]);
cout<<" Enter the distance\n";
int m;
cin>>m;
int h[n];

for (int i = 0; i < n; i++)
h[i]=0;

for (int i = 0; i < n; i++)
{
if(i>m)
h[a[i-m-1]]=0;//delete outside k diste=ance
if(hash(h,a[i])){
cout<<a[i]<<" ";
cout<<"Duplicate\n";
return 0;
}
}
cout<<"No Duplicate";
return 0;
}

No comments:

Post a Comment

Contributors

Translate