A program to find all pair of numbers between 0 and N that equal K

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int main()
{
int a=0,  c, d, N, K;
bool stopBool = 0;
cin >> N >> K;
c = N;
      while (1)
      {
       if (N-c ==K)
       {
           a++;
       }
      c--;
      if (c == -1)
      {
          N--;
          c = N;
      }
      if (N == -1)
      {
          break;
      }
      }
      cout << a;


}


This is supposed to find take a number N and K and find all numbers between 0 and N that equal K and cout the number of pairs that fit it but it doesn't work.
Last edited on

find all numbers between 0 and N that equal K and cout the number of pairs that fit it but it doesn't work.


Number between 0 and N that equals K is K itself by the way.

Why don't you try to describe your problem more accurately and not explain what you mean by "doesn't work" - you could not compile it at all or it gives wrong answer or what?
I think the question is the number of pairs of numbers whose difference is equal to K.

According to me, if K > N, it is zero.
if K <= N, it is ~ N-K (remains to be checked)

If the question is to find the numbers whose sum = K, then also similar solution can be applied directly.
Topic archived. No new replies allowed.