0

I have tried to solve by hashing and return via dynamic array.
but facing error in test case:

14  
12 7 5 1 13 1 10 8 11 9 2 4 3 6  

Correct output is:

1 14  

And my code's output is:

1 0

else all outputs are correct by far. Here is my code:

int *findTwoElement(int *arr, int n) {

    int *res=new int(2);
    int hsh[n]={0};
    int mini;
    for(int i=1;i<=n;i++)
        {
            hsh[arr[i-1]]++;
        }
     for(int i=1;i<=n;i++)
     {
        if(hsh[i]>1)
              {
                  mini=min(mini,i);
                  res[0]=mini;
              }
        if(hsh[i]==0)
              {
                  res[1]=i;
              }
            
     }
   return res;
}
James Z
  • 12,209
  • 10
  • 24
  • 44
  • 1
    Your code contains a [variable-length array](https://en.wikipedia.org/wiki/Variable-length_array), which [isn't a part of C++](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). Use `std::vector` instead. And get [some good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and take some classes to learn C++ and programming properly. So-called "competition" or online judge sites aren't learning or teaching resources. – Some programmer dude Jul 07 '21 at 08:01
  • 2
    As a hint: `new int(2)` allocates ***one single*** `int` element. An array of size `1`, essentially. – Some programmer dude Jul 07 '21 at 08:02
  • 4
    "new int(2)" does not create an array of size 2. it creates onw integer with value 2. You need "new int[2]" – gerum Jul 07 '21 at 08:03
  • 1
    side note: Rather than a 2 element dynamic array, consider using `std::pair` and returning it by value. Copying 2 `int`s is probably cheaper than any amount of dynamic allocation and a lot cleaner. – user4581301 Jul 07 '21 at 08:11
  • 1
    whenever I see `<=` in a for loop's exit condition I examine more closely for bugs. I recommend you do the same because you probably have one. – user4581301 Jul 07 '21 at 08:13
  • @Someprogrammerdude thankyou ! i am still learning , i have to return it in a single "int" data type not by using a vector , so i thought variable length array would help. could you have any idea whats wrong in my code ? – magician99 Jul 07 '21 at 08:14
  • @user4581301 thankyou !! i will check it – magician99 Jul 07 '21 at 08:19
  • 1
    While learning, stay as far away from "competition" sites as you can! All they can possible teach you are really bad habits. Habits so bad that they can make you virtually unemployable. – Some programmer dude Jul 07 '21 at 08:26
  • @Someprogrammerdude thats maybe your experience with them was like that ! World is learning with them only and IT sectors encourage the ratings too. Hands on practice in real environmnt is probably the best way to learn ,i dont know about yours . – magician99 Jul 07 '21 at 08:31
  • 2
    But the problems on such sites *aren't* what's expected in "the real world". And the code show-cased are truly bad. You don't learn proper data-structures or algorithms, you don't learn proper use of the standard functionality available for the language. You don't learn proper syntax even. And you don't learn such important part as team-work or debugging or maintenance, or even the math behind the code you write. Unfortunately, code-monkeys without any chance of career-paths forward will always be needed. :( – Some programmer dude Jul 07 '21 at 08:38
  • how is the caller of the function supposed to know how many elements are in the array pointed to by the returned pointer? I suggest you to forget about manually mangaged arrays and VLAs and use `std::vector` instead – 463035818_is_not_an_ai Jul 07 '21 at 09:07
  • Academia has made a mess of teaching programming at least since the 1990s, and the bulk of computer science graduates come out of universities requiring a massive amount of retraining. I keep finding it's more cost effective to hire electrical and mechanical engineers and train them. – user4581301 Jul 07 '21 at 16:05
  • @463035818_is_not_a_number exactlyyyy , its online locked function using : auto to retrieve the data and predefined function with int . – magician99 Jul 07 '21 at 18:01
  • auto what? The signature of the function is given to you and you cannot modify it? The thing to learn here is that this is the wrong signature for a function that is supposed to return an array – 463035818_is_not_an_ai Jul 07 '21 at 19:36

1 Answers1

0

thankyou everyone !! actually Reason is simple new will allocate memory dynamically otherwise normal array will be allocated memory in stack and once our function is out of scope all stack memory will be automatically cleared. We can also use malloc for this .

1> long long int hsh[n+1]={0};

2> first iteration should be from :for(int i=0;i<n;i++)

3> second iteration will be from : for(int i=0;i<=n;i++)

4> question was modified so now we have only 1 repeating element ,so no need to get smallest , correct code should be : res[0]=i