#include<bits/stdc++.h>
using namespace std;
void display(int a[], int n)
{
cout<<"INSIDE DISPLAY ";
for(int i=0; i<n; i++)
{
cout<<*(a+i);
cout<<" ";
}
}
int pairswithdiffk(vector<int> arr, long k)
{
int count = 0;
int a_size = arr.size();
int a[a_size];
int x = 0;
int m = 0;
int l = 0;
int r = 0;
sort(arr.begin(), arr.end());
vector<int>::iterator i;
for(i = arr.begin(); i!=arr.end(); i++)
{
a[x] = *i;
++x;
}
for(int j = 0; j<a_size; j++)
{
if(a[j] != a[j+1])
{
a[m] = a[j];
m++;
}
}
a[m] = a[a_size];
a_size = m;
display(a, a_size);
while(l<a_size)
{
if(r<a_size)
{
if(a[r] + a[l] == k)
{
count++;
l++;
r = l+1;
}
else if(a[r] + a[l] < k)
{
r++;
}
else
{
l++;
}
}
else
{
r = l+1;
}
}
return count;
}
int main()
{
int N;
int k;
int ele;
vector<int> v1;
cout<<"Enter the number of elements in the array: ";
cin>>N;
cout<<"Enter the k value: ";
cin>>k;
cout<<"Enter the array elements: ";
for(int i=0; i<N;i++)
{
cout<<"---";
cin>>ele;
v1.push_back(ele);
}
cout<<"For loop exited";
int num_pairs = pairswithdiffk(v1, k);
cout<<"\nThe number of distinct pairs with sum = "<<k<<" are: "<<num_pairs;
return 0;
}
the code above is to find distinct pair of numbers with sum k
and in that when i enter more than 4 same inputs consecutively the code does not stop taking input, in other case it works fine.
what could be the reason for this ?