0

I was solving missing number in an array problem. the editorial showed it is solved by

  1. first get sum on n number.
  2. minus each element from given array the result will be the missing number.

but I solved it by another method. 1. I declared another array of n+1 size. 2. For each element in given array. eg n=5 a[1,2,3,5] where 4 is missing. I declare array[ignore,1,1,1,garbage,1] {here index 0 is ignored. notice given array has element 1 2 3 and 5, So i save 1 on index 1,2,3 and5 } now traverse the array from i=1 and the index which has value not equal to 1 is the missing number.

here is my code.

**

int missing(int arr[], int n)
{
    int a[n + 1];
    for (int i = 0; i < n - 1; i++)
    {
        a[arr[i]] = 1;
    }
    for (int i = 1; i < n + 1; i++)
    {
        if (a[i] != 1)
        {
            return i;
        }
    }
}

**

but for n=706 in which 621 is missing. geek for geek code editor shows case fialed while my personal editor show result 621 which is right.

notice the problem constraints 1<=N<=10^6

thanks in advance

  • 8
    That site is a bad resource for learning programming. Case in points: It's teaching to use a [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array), despite [it's not part of the C++ language](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). If you want to learn C++ please invest in [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and take classes. – Some programmer dude Apr 06 '22 at 11:43
  • 3
    Please just use something like `std::vector` instead. – user438383 Apr 06 '22 at 11:44
  • Furthermore, you could leave some elements of the array `a` *uninitialized* which means they will have *indeterminate* values (look at them as garbage). Using indeterminate values in any way (like you do in your second loop) leads to *undefined behavior*. – Some programmer dude Apr 06 '22 at 11:46
  • 1
    Avoid these sites: www.geeksforgeeks.org www.w3schools.com www.tutorialspoint.com www.cplusplus.com leetcode.com www.codewars.com codeforces.com www.hackerrank.com (some of them may have some *entertainment* value, but **none** of them have tutorial/learning value). – Eljay Apr 06 '22 at 12:41
  • None of those mentioned sites should be used to learn C++. As far as a couple of them (not geeksforgeeks), the questions asked on those sites assume you know the computer language you will be using to solve their problems. You must know the computer language well-enough to almost *never* need to ask a question here on StackOverflow concerning basic language syntax, and *never* need to make mistakes similar to what you're making now (variable-length-arrays, uninitialized variables, etc.). – PaulMcKenzie Apr 06 '22 at 13:15
  • 1
    Also, with those books mentioned, get yourself a compiler that you can run locally. Do not depend on online compilers to learn and do debugging work. – PaulMcKenzie Apr 06 '22 at 13:17
  • Can u please tell where i have made mistake. Variable length arrays and uninitialized varibles. Enlighten me a little bit please – Mohammed faiz Khan Apr 06 '22 at 14:26
  • Also i have my local c++ compiler in which code runs fine and i get the expected output. I think i have made mistake with time constrainst or something related to contraints. – Mohammed faiz Khan Apr 06 '22 at 14:32
  • @MohammedfaizKhan `int a[n + 1];` -- This is not valid C++. Here is valid C++ `std::vector a(n + 1);`. Again, `int a[n + 1]` is not part of C++, and it doesn't matter if it "compiles fine" with your compiler. If you switched the settings on your compiler to compile using strict ANSI, or if you changed the compiler to Visual C++, you will quickly see the code will fail to compile. – PaulMcKenzie Apr 08 '22 at 02:00

0 Answers0