Best series for DS & Algo. I am also 10 yrs java exp guy. Was looking for DS & Algo free course over RUclips with java implementation and found this. Hats Off To You Man....Excellent Work. GOD BLESS YOU :)
I do not understand to appreciate you.....Never seen this type of confidence in DSA while teaching. It means sir how much deep knowledge you have about this subject.......Superb sir no one can teach you .....Awesome such valuable content you are giving us in free of cost salute sir...Thanks for increasing my confidence.
Here's another algorithm to achieve the same goal, it's easier to understand but lower in performance : public int myFindSecondMaximum(int[] numbers){ int maximum = numbers[0]; int position = 0; for (int i=1; i< numbers.length; i++){ if (numbers[i] > maximum){ maximum = numbers[i]; position = i; } } numbers[position] = 0; int secondMaximum = numbers[0]; for (int i=1; i< numbers.length; i++){ if (numbers[i] > secondMaximum && numbers[i] != maximum){ secondMaximum = numbers[i]; } } return secondMaximum; }
I did the same thing but assigned the max to first element of the array and the second max to the second element of the array and made sure its right or swapped them. Then I started the for loop from the 3rd element and did the similar logic. Is this logic fine as well or will it have any complexity issues?
My Solution: public static int secondMax(int[] arr) { int max = Integer.MIN_VALUE; int secMax = Integer.MIN_VALUE; for (int i: arr) { if (max < i) { max = i; } } for (int j:arr) { if (secMax
Whats wrong with this snippet? public int findSecondMaximum(int[] arr) { int max = arr[0]; int secondMax = arr[1]; for(int i = 2;i < arr.length;i++) { if (arr[i] > max) { secondMax = max; max = arr[i]; } else if (arr[i] > secondMax) { secondMax = arr[i]; } } return secondMax; }
@@addytechie6948 if array is {4,5} max = 4; secondMax =5; for(int i = 2;i < arr.length;i++) //here i is 2 which is less than 2(array.length) so for loop breaks finally it return secondMax as 5.
Enjoying your course sir💌🤟 /* Find the second maximum value in an array */ // method to find the second maximum value static int find_second_maximum_value(int[] arr) { int max=0; int secondMax=0; for (int j : arr) { if (j > max) { secondMax = max; max = j; } else if (j > secondMax && j != max) { secondMax = j; } } return secondMax; } // method to print the array static void printArray(int[] yourArray) { for (int x:yourArray) { System.out.print(x+"|"); } } // main method @SuppressWarnings({"static-access"}) public static void main(String[] args) { int[] myArray = {44,57,56,25,24,89,84,26,10,49,11,98,100,55,99}; System.out.println("__Original Array__"); q6 q = new q6(); q.printArray(myArray); System.out.println(); System.out.println("Second maximum element in this array is: "+q.find_second_maximum_value(myArray)); }
Best series for DS & Algo. I am also 10 yrs java exp guy. Was looking for DS & Algo free course over RUclips with java implementation and found this. Hats Off To You Man....Excellent Work. GOD BLESS YOU :)
Thanks !!!
You make all the dynamic changes so clear with animation slides. Thank you so much, sir! I feel so grateful to have found your videos.
Glad it helped you !!!!
I do not understand to appreciate you.....Never seen this type of confidence in DSA while teaching. It means sir how much deep knowledge you have about this subject.......Superb sir no one can teach you .....Awesome such valuable content you are giving us in free of cost salute sir...Thanks for increasing my confidence.
Welcome
These animation slides make it very easy to understand the logic. Great work!!
thanks !!!
can't thank u enough really...I was so confused when I try to read books but ur videos are awesome Thanks so much🥰
Glad it helped you !!!
Here's another algorithm to achieve the same goal, it's easier to understand but lower in performance :
public int myFindSecondMaximum(int[] numbers){
int maximum = numbers[0];
int position = 0;
for (int i=1; i< numbers.length; i++){
if (numbers[i] > maximum){
maximum = numbers[i];
position = i;
}
}
numbers[position] = 0;
int secondMaximum = numbers[0];
for (int i=1; i< numbers.length; i++){
if (numbers[i] > secondMaximum && numbers[i] != maximum){
secondMaximum = numbers[i];
}
}
return secondMaximum;
}
very right!
For those who want more test cases:
# Array with unique elements:
input = [1, 2, 3, 4, 5]
# Expected output: 4
# Array with duplicate elements:
input = [2, 5, 3, 5, 7, 2]
# Expected output: 5
# Array with negative numbers:
input = [-5, -2, -9, -3, -7]
# Expected output: -3
# Array with mixed positive and negative numbers:
input = [10, -5, 3, -8, 7]
# Expected output: 7
# Array with repeated highest value:
input = [2, 5, 9, 5, 7, 9]
# Expected output: 7
# Array with only one unique value:
input = [3, 3, 3, 3, 3]
# Expected output: No second highest value (some implementations may return an exception)
Superb explanation
thanks !!!
Thanks sir 🙏🏻.It is helpful.
Welcome !!!
Thank you sir, nice explanation.
really very helpful.
Thanks Sir
Welcome !!!
sir when will the playlist be finish? thank you for your efforts
It will be a continuation playlist ... in future it will have advanced DSA topics as well ....
@@itsdineshvaryani ok sir thank you..
@@itsdineshvaryani Outstanding Dinesh. You're the best!
@@bpriorb thanks !!!
just amazing content !! what a beautiful learning experience !!
thanks !!!
I did the same thing but assigned the max to first element of the array and the second max to the second element of the array and made sure its right or swapped them. Then I started the for loop from the 3rd element and did the similar logic. Is this logic fine as well or will it have any complexity issues?
Can you please check the test case where the second largest element is in the 0th index. I think the above method is going to fail
Hi, its correct however it fails if all elements of an array are same
He already mentioned that the constraint is the array needs to have a second maximum number. There is no second maximum if all the elements are same.
Not giving correct output while implementing in IDE
So take it as exercise and correct it !!!
what about sort the array then print second last element?
sorting is higher on time complexity side !!!
My Solution:
public static int secondMax(int[] arr) {
int max = Integer.MIN_VALUE;
int secMax = Integer.MIN_VALUE;
for (int i: arr) {
if (max < i) {
max = i;
}
}
for (int j:arr) {
if (secMax
👌👍👍
Whats wrong with this snippet?
public int findSecondMaximum(int[] arr) {
int max = arr[0];
int secondMax = arr[1];
for(int i = 2;i < arr.length;i++) {
if (arr[i] > max) {
secondMax = max;
max = arr[i];
} else if (arr[i] > secondMax) {
secondMax = arr[i];
}
}
return secondMax;
}
if array is {4,5} your code will return secondMax as 5
@@nyashodhara7290 can u explain this how plz??
@@addytechie6948 if array is {4,5}
max = 4;
secondMax =5;
for(int i = 2;i < arr.length;i++) //here i is 2 which is less than 2(array.length) so for loop breaks
finally it return secondMax as 5.
@@nyashodhara7290
Sir i think that works if we put
max = arr[0]
SecondMax = arr[0]
And begin the loop for with i=1
What do you think please ?
the algorithm doesn't work if the array starts with 33
it works
For maximum value?
just compare adjacent values and return max once loop ends ..
Enjoying your course sir💌🤟
/* Find the second maximum value in an array */
// method to find the second maximum value
static int find_second_maximum_value(int[] arr) {
int max=0;
int secondMax=0;
for (int j : arr) {
if (j > max) {
secondMax = max;
max = j;
} else if (j > secondMax && j != max) {
secondMax = j;
}
}
return secondMax;
}
// method to print the array
static void printArray(int[] yourArray) {
for (int x:yourArray) {
System.out.print(x+"|");
}
}
// main method
@SuppressWarnings({"static-access"})
public static void main(String[] args)
{
int[] myArray = {44,57,56,25,24,89,84,26,10,49,11,98,100,55,99};
System.out.println("__Original Array__");
q6 q = new q6();
q.printArray(myArray);
System.out.println();
System.out.println("Second maximum element in this array is: "+q.find_second_maximum_value(myArray));
}
Thanks for Code. but works only for positive integers. @Dinesh code works for both +ve and -ve.
pls find the solution
public static int findSecondMax(int[] input) {
int firstMax = input[0];
int secondMax = input[1];
for (int i = 2; i < input.length; i++) {
if (input[i] > firstMax) {
if (firstMax > secondMax) {
secondMax = firstMax;
}
firstMax = input[i];
} else if (input[i] > secondMax) {
secondMax = input[i];
}
}
return secondMax;
}
its correct