Vector keeps on taking input after loop count is exhausted

#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";![Selection_001|690x429](upload://yggqP64z2kcCMIsvATFTd6MlDVj.png) 

    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 ?

It actually stops “taking input” after the 6th 1 has been entered, and gets stuck in an infinite loop in the while loop in pairswithdiffk (you don’t see For loop exited being printed out because you don’t flush std::cout after “printing” it).

Add

cout << "l: " << l << " r: " << r << endl;

after

while(l<a_size)
    {

and you should be able to see what’s going on :slight_smile:

Edit:

Also, maybe change

cout<<"For loop exited";

to

cout<<"For loop exited" << endl;

to eliminate some of the confusion :slight_smile:

2 Likes

what would be a good solution to get rid of this problem ?
i have thought of adding a loop count ( the number of times it runs the while loop) is that a good solution ? @ssjgz

The problem of the infinite loop?

I’d recommend just reviewing all of your logic from scratch - when do you know that you have found all possible pairs? That’s when the loop should end :slight_smile:

Sorry to be cagey, but it’s hard to give hints without giving the whole solution away :slight_smile:

thank you!!
this will definitely help

1 Like