Find out array contains duplicates within k distance

1. Given an array A of N integers, and a number k which is smaller than the size of the array. Write a program which returns true if the array contains duplicates within k distance. 

Input: The first line contains the integers N and k separated by space. The second line contains the N integers separated by space. 

Output: Output "Yes" if it contains duplicates within distance k else output "No".


SOLUTION IN C

#include<stdio.h>
int main()
{
  int a[100],i,n,size,flag=0,j;
  scanf("%d %d",&n,&size);
  for(i=0;i<n;i++)
    scanf("%d",&a[i]);
  for(i=0;i<n;i++)
  {
    for(j=i+1;j<(i+1+size)&&j<n;j++)
    {
      if(a[i]==a[j])
      {
        flag=1;
        break;
      }
    }
  }
  if(flag==1)
    printf("Yes");
  else
    printf("No");
  return 0;
}


SOLUTION IN C++

#include<iostream>
using namespace std;
int main()
{
  int a[100],i,n,size,flag=0,j;
  cin>>n>>size;
  for(i=0;i<n;i++)
    cin>>a[i];
  for(i=0;i<n;i++)
  {
    for(j=i+1;j<(i+1+size)&&j<n;j++)
    {
      if(a[i]==a[j])
      {
        flag=1;
        break;
      }
    }
  }
  if(flag==1)
    cout<<"Yes";
  else
    cout<<"No";
}