Yashraj1402's blog

By Yashraj1402, history, 3 hours ago, In English

The article in discussion is Find the largest three elements in an array in c++

Method 1 as given in the article (standard 3 variable approach)

But its implementation has a mistake. When they declare the 3 variables (max, max2, max3) they initialize them as:

int max, max2, max3;
max3 = max = max2 = arr[0];

This works fine until the first element of the array (arr[0]) is the greatest element, then there will be no arr[i] which satisfies any of the deciding conditions, which are:

arr[i] > max
arr[i] > max2
arr[i] > max3

Hence, if the first element of the array is the greatest element then max, max2, max3 will be equal to that element to fix this we can just initialize max, max2, max3 with a low infinity like INT_MIN, etc.

  • Vote: I like it
  • -7
  • Vote: I do not like it

»
2 hours ago, # |
  Vote: I like it +1 Vote: I do not like it

maybe you should contact their support to correct this mistake instead of posting this on codeforces

  • »
    »
    99 minutes ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I have done it but I posted this so that if someone copy-pastes this code (like I did for a problem) they should know this.