-5

"unordered_set" is not working properly on codechef and giving wrong output on its online ide whereas I am getting right output on geeksforgeeks ide and codeblocks for Input like 3 2 10 1 100 4 3 i am getting 4 rows as expected in codeblocks and geeksforgeeks because n+m-1 is 4 whereas I am getting only 2 rows in codechef what can be the reason behind and now how it will work on codechef?

#include<stdio.h>
#include<bits/stdc++.h>
#include<unordered_set>
using namespace std;
int main()
{
int n,m,c=0,d,i,j,sum;
int a[10000];
int b[10000];
unordered_set <int> s;
scanf("%d %d",&n,&m);
for(i=0;i<n;i++)
    scanf("%d",&a[i]);
for(i=0;i<m;i++)
    scanf("%d",&b[i]);
for(i=0;i<n;i++)
{
    for(j=0;j<m;j++)
    {
        sum=a[i]+b[j];
        if(s.find(sum)==s.end())
        {
            s.insert(sum);
            printf("%d %d\n",i,j);
            c++;
        }
        if(c>=(n+m-1))
            {d=1;break;}
    }
    if(d==1)
        break;

}
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 8
    One of the problem with competition sites like Codechef is that it won't tell you the input to your program. That makes it *very* hard to debug it. Such sites are also very bad for teaching programming and languages, all they really teach is how to be good at such site and not much more (besides getting bad habits like [including `bits/stdc++.h`](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h)). – Some programmer dude Jan 05 '19 at 14:58
  • 1
    If you want to become a good C++ programmer, [here's a curated list of good books to read](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). – Some programmer dude Jan 05 '19 at 15:00
  • 3
    Advice -- If you're going to post attempts from an online coding site, and you know the input that gives the issue, post the code *with the input hard-coded into the program*. There is no need for `scanf` calls -- just fill the array(s) or variables with the data in the program. This makes it easier to simply take your code, compile it, run it, and see the results. No one is going to constantly have to type in known data each and every time. – PaulMcKenzie Jan 05 '19 at 15:31
  • 1
    The above also makes it easier for you to debug. With close to no effort you can run the program over and over in the debugger with the exact same input until you crack the case. – user4581301 Jan 05 '19 at 16:42
  • You also may find debugging easier if you use descriptive variable names. Not only is it much easier to turn a casual typo from a compiler error into a logic error when you're only working with one letter, It really sucks when your brain subs in `q` for `r` because that's what it expects to read when reviewing the code looking for the error. – user4581301 Jan 05 '19 at 16:47
  • If you want an example of how you should have posted your code, without all of the unnecessary `scanf` calls, [this is what it should look like](http://coliru.stacked-crooked.com/a/28820dfbcd9eafb7). As the answer that was already given stated, the value of `d` is not initialized to anything. It could be 0, 1, 889, -443, etc. (I've almost gotten to the point where I'm downvoting any "online coding site" questions, where the poster knows the data, but insists on putting `scanf` calls in their posted code). – PaulMcKenzie Jan 05 '19 at 19:37

1 Answers1

3

Your program exhibits undefined behavior, by way of accessing the value of an uninitialized variable d.

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85