-2
#include<bits/stdc++.h>
using namespace std;

int main() {
    int a[5] = {1, 2, 3, 4, 5};
    int k;
    cin >> k;
    int i, j, ct;

    i = 0;
    j = 4;
    ct = 0;

    while (i < j) {
        if (a[i] + a[j] > k) {
            --j;
        }
        else if (a[i] + a[j] < k) {
            ++i;
        }
        else {
            ct++;             
        }     
    }
    cout << ct;
}

Trying to print the total number of pairs ct in a given sorted array whose sum is equal to k, k is any given input. But the problem is, value of i changes once and value of j remains same. why? hence i < j is always true and loop runs to infinite hence no certain value of ct comes out. Where is the problem in this code?

Yun
  • 3,056
  • 6
  • 9
  • 28
  • 2
    Once you hit the else case, i and j will never change after that. You need to modify one of them. – cigien Sep 19 '21 at 03:51
  • What happens if `a[i] + a[j]` is exactly equal to `k`? You leave `i` and `j` the same and increment `ct`. And then the next time through your loop, because `i` and `j` are the same, you enter the same branch of your conditional statement... – Nathan Pierson Sep 19 '21 at 03:51
  • The only place where the value of `j` could change is the `--j;` line. Does that line ever get executed? If not, why not? – Jeremy Friesner Sep 19 '21 at 03:52
  • 1
    Stepping through the code in the debugger will show you your logic error. If you don't know how to use the debugger, now is the perfect time to learn. There is no better tool available to a programmer, because the debugger allows you to step through the code line by line as it executes, where you can inspect the value of variables and the flow of execution to see exactly what's happening. – Ken White Sep 19 '21 at 03:52
  • See [Why should I not #include ?](https://stackoverflow.com/q/31816095/3422102). See [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/), consider a chat with the duck, and see [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/3422102) – David C. Rankin Sep 19 '21 at 04:10
  • Style suggestion: in C++ there is no need for separate declaration and initialization of variables. `int n; n = 1;` for example, could simply be: `int n = 1;` – Chris Sep 19 '21 at 04:31

2 Answers2

3

There are many issues in your code, but the reason for it being stuck is pretty simple. You have three cases

  1. larger
  2. smaller
  3. equal

If you reach the equal case, then you do not change i nor j so it will always go to case equal, forever.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
2
Let's say k=5:

In the while loop (i>j) or (0<4):

First case (1+5>k): true
j = 3

the while loop (0<3):
Second case (1+4 = 5)
ct = 1


the while loop (0<3):
Third case (1+4 = 5)
ct = 2


the while loop (0<3):
Fourth case (1+4 = 5)
ct = 3


the while loop (0<3):
Fifth case (1+4 = 5)
ct = 4

So you end up with an infinite loop that never ends because neither the i or j value is being updated

i.e the "else{} " is being infinitely run because the loop condition still holds.