Find Second Maximum value in an Array | Animation

preview_player
Показать описание

Watch all my playlist here:

Want to land a software engineering job in the IT industry? This course - 'Visualizing Data Structures and Algorithms' is here to help. The course walks you through multiple Java algorithms, data structures problems, and their solutions with step by step visualizations, so that you are actually learning instead of blindly memorizing solutions.

The course covers in and outs of Data Structures and Algorithms in Java. Java is used as the programming language in the course. Students familiar with Javascript, Python, C#, C++, C, etc will also get to learn concepts without any difficulty. The implementation of various Algorithms and Data Structures have been demonstrated and implemented through animated slides. It covers many interview room questions on Algorithms and Data Structures. The questions and solutions are demonstrated by -

1. Animated slide. (To make visualization of algorithms faster)
2. Coding algorithm on IDE.

The course covers topics such as -
0. Algorithm Analysis
1. Arrays
2. Matrix
3. Singly Linked List
4. Doubly Linked List
5. Circular Singly Linked List
6. Stacks
7. Queues
8. Binary Tree
9. Binary Search Tree
10. Graphs
11. Priority Queues and Heaps
12. Recursion
13. Searching
14. Sorting
15. Strings
16. Trie Data Structure
17. Dynamic Programming
and many more ...

#dsa #algorithms #coding
Рекомендации по теме
Комментарии
Автор

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.

huayang
Автор

These animation slides make it very easy to understand the logic. Great work!!

mvs
Автор

Best series for DS & Algo. I am also 10 yrs java exp guy. Was looking for DS & Algo free course over YouTube with java implementation and found this. Hats Off To You Man....Excellent Work. GOD BLESS YOU :)

shubhamagarwal
Автор

I do not understand to appreciate seen this type of confidence in DSA while teaching. It means sir how much deep knowledge you have about this sir no one can teach you such valuable content you are giving us in free of cost salute sir...Thanks for increasing my confidence.

bkishwarchavan
Автор

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;
}

karentechnologies
Автор

can't thank u enough really...I was so confused when I try to read books but ur videos are awesome Thanks so much🥰

TinsaeJembere
Автор

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)

thiagobrunoms
Автор

just amazing content !! what a beautiful learning experience !!

ajinxRGB
Автор

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?

yasaswynandavareek
Автор

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

modinglymods
Автор

sir when will the playlist be finish? thank you for your efforts

yellnitroy
Автор

Not giving correct output while implementing in IDE

deshansh
Автор

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<j && j!=max){
secMax = j;
}
}
return secMax;
}

alfredlotsu
Автор

Hi, its correct however it fails if all elements of an array are same

ERDheerajmann
Автор

what about sort the array then print second last element?

indranilchakraborty
Автор

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;
}

addytechie
Автор

Enjoying your course sir💌🤟
/* Find the second maximum value in an array */

// method to find the second maximum value
static 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

public static void main(String[] args)
{
int[] myArray = {44, 57, 56, 25, 24, 89, 84, 26, 10, 49, 11, 98, 100, 55, 99};
Array__");
q6 q = new q6();
q.printArray(myArray);
System.out.println();
System.out.println("Second maximum element in this array is:
}

soumyadipmajumder
Автор

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;
}

atassun
Автор

the algorithm doesn't work if the array starts with 33

imonem