For second maximum element in an array
The code is not working for array with all 5 elements as zero. https://code.sololearn.com/chaffWJy6898/?ref=app
The program has been improved and the duplicate numbers are removed. https://code.sololearn.com/ckbuVn1dloGm Keep learning & happy coding :D
Yes!!! This is because you are providing INT_MIN value to max2, Which is the minimum value of its limit and that's why it is printing minimum value. If you will provide INT_MAX value to this then it will print maximum value of its limit. your else if condition will always false and will never execute. i.e. line 22 on putting all 5 elements as zero, "if condition (line 16) will true for only once when i=0; and for i=0, all other values are.... arr =0, max1=0, max2 = INT_MIN. (P S. - value of max2 will never gonna change as all next if condition are false)." when i becomes 1 then if condition ( line 16 for i=1) will be like : if(0>0) that is false, and it will be false for all next values (2,3,4) of i. and your else if condition is always false as stated above. so, value of max2 can't be changed and it will print the value of INT_MIN. Thank you!!! Your effort is pretty good and doubt too, I appreciate that. if you didn't understand then kindly let me know.
Hey Riya Chawla !!! I didn't write my own code, I have suggested on the basis of code provided above. If you want the array to work for all elements 0.. Just replace max2 to max1 inside the printf statement (in line 27)! it will work for all elements as 0 for array
Riya Chawla did you understood? If yes then kindly let me know!!! Regards
The problem is this part of the condition: else if ( ... & arr[ i ] < max1 ) ^--- It prevents you from updating "max2" in case the current value is equal to the currently largest value, hence your problem with duplicate values. Given that you also want to update "max2" in such cases (why wouldn't you in the first place), the correct condition would be: else if ( ... && arr[ i ] <= max1 ) However, you can now simply drop that part, because it is already implied by the if statement (else if will only be executed if the if condition was wrong): !( arr[ i ] > max1 ) <=> ( arr[ i ] <= max1 ) Therefore, it would be sufficient to use: else if ( arr[ i ] > max2 )
max1 = max2 = arr; //INT_MIN;
https://code.sololearn.com/ckbuVn1dloGm Keep learning & happy coding :D
Saurabh Kumar Yadav can you please share code... i want the array to work for all elememts 0.. negative intezers and repetitive elements .
SoloProg thanks :)
SoloProg thanks for response but by doing this it is not working properly for repetitive elements..
Saurabh Kumar Yadav your answer is correct for maximum element but not for second maximum element 😕
Why are my users being logged as [object Object] when logging my array/user instead of giving me the values [name, age]?
JS in sololear is latest version or older? What do we use often VAR or LET and CONST?? I Have VAR is no longer used is it true??
Как сравнивать строки в Phyton?
Kotlin get if Var doesn't exist
Sololearn bug @mentors