+ 1

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

8th Sep 2021, 6:25 PM
Riya Chawla
Riya Chawla - avatar
11 Respuestas
+ 2
The program has been improved and the duplicate numbers are removed. https://code.sololearn.com/ckbuVn1dloGm Keep learning & happy coding :D
10th Sep 2021, 8:53 AM
SoloProg
SoloProg - avatar
+ 4
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] =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.
8th Sep 2021, 6:52 PM
Saurabh
Saurabh - avatar
+ 4
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
9th Sep 2021, 6:21 AM
Saurabh
Saurabh - avatar
+ 3
Riya Chawla did you understood? If yes then kindly let me know!!! Regards
9th Sep 2021, 6:34 AM
Saurabh
Saurabh - avatar
+ 3
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 )
9th Sep 2021, 7:08 AM
Shadow
Shadow - avatar
+ 2
max1 = max2 = arr[0]; //INT_MIN;
8th Sep 2021, 7:11 PM
SoloProg
SoloProg - avatar
+ 2
https://code.sololearn.com/ckbuVn1dloGm Keep learning & happy coding :D
9th Sep 2021, 9:48 AM
SoloProg
SoloProg - avatar
+ 1
Saurabh Kumar Yadav can you please share code... i want the array to work for all elememts 0.. negative intezers and repetitive elements .
9th Sep 2021, 6:02 AM
Riya Chawla
Riya Chawla - avatar
+ 1
SoloProg thanks :)
9th Sep 2021, 10:44 AM
Riya Chawla
Riya Chawla - avatar
0
SoloProg thanks for response but by doing this it is not working properly for repetitive elements..
9th Sep 2021, 6:04 AM
Riya Chawla
Riya Chawla - avatar
0
Saurabh Kumar Yadav your answer is correct for maximum element but not for second maximum element 😕
9th Sep 2021, 6:39 AM
Riya Chawla
Riya Chawla - avatar