Last active
May 19, 2023 06:54
-
-
Save ShahStavan/45228a1a0b5a1f5663ded1c41176ba77 to your computer and use it in GitHub Desktop.
Program to find second maximum of n numbers without using arrays in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Let's see Logic behind the code🤘 | |
We have declared four variables .. | |
curr = Current element | |
first = Will be largest number | |
second = Will be second max number | |
i = So that we can break the while loop | |
while(i==1) | |
-> It checks whether i is 1 or not. | |
-> If yes goes inside the loop | |
-> If we enter -1 that is we are done for elements [In short we have entered all elements]. Now we need to break the loop | |
-> So now we will set i = 0 and we will break the loop | |
Let's assume that our inputs are 5 , 6 , 8 , 7 and -1 [=1 to break the loop] | |
curr = 5 | |
first = curr | |
second = curr | |
Now we enter the loop | |
-> And we have now 6 as our curr value [curr = 6] | |
-> We check whether curr = -1 is true or not. [False] | |
-> Second Condition : | |
-> (curr > first) [6 > 5] [True] | |
-> second = 5 | |
-> first = 6 | |
curr = 6 | |
first = 6 | |
second = 5 | |
Now we again enter the loop | |
-> And now we have 8 as our curr value [curr = 8] | |
-> We check whether curr = -1 is true or not. [False] | |
-> Second Condition : | |
-> (curr > first) [8 > 6] [True] | |
-> second = 6 | |
-> first = 8 | |
curr = 8 | |
first = 8 | |
second = 6 | |
Now we again enter the loop | |
-> And now we have 7 as our curr value [curr = 7] | |
-> We check whether curr = -1 is true or not. [False] | |
-> Second Condition : | |
-> (curr > first) [7 > 8] [False] | |
-> Third Condition : | |
-> (curr > second) [7 > 6] [True] | |
-> second = curr | |
curr = 7 | |
first = 8 | |
second = 7 | |
Now we again enter the loop | |
-> And now we have -1 as our curr value [curr = -1] | |
-> We check whether curr = -1 is true or not. [True] | |
-> i = 0 | |
-> break; | |
-> As now i is 0 we will not enter into the loop | |
-> System.out.println("Second Max element = " + second); | |
-> Output : | |
Second Max element = 7 | |
Hope you have understood logic behind the code😅 | |
See you in the next coding questions👋 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class Secondmax { | |
//We have to find Second max number till n number withour using arrays | |
public static void main(String[] args) { | |
int curr, second, first , i=1; | |
Scanner input = new Scanner(System.in); | |
System.out.println("Enter element: "); | |
curr = input.nextInt(); | |
second = curr; | |
first = curr; | |
while (i==1){ | |
System.out.println("Enter element: "); | |
curr = input.nextInt(); | |
if (curr == -1) { | |
i=0; | |
break; | |
} | |
if (curr > first) { | |
second = first; | |
first = curr; | |
} | |
else if (curr > second) { | |
second = curr; | |
} | |
} | |
System.out.println("Second Max element = " + second); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment