Find Second Largest Number in an Array(Multiple Approaches)(Hindi) || Array Data Structure

preview_player
Показать описание
Find Second Largest Number in an Array(Multiple Approaches)(Hindi) || Array Data Structure.

Hi
Welcome To our channel Code Bashers.

About Video:
In this video I have discussed the approach to find the second largest number in the array. In the discussed approach the array in which element is to be found is traversed a single time. I have taken two variables which will store largest and second largest element respectively. At each index the value of that particular index is compared with first with the largest variable
and then with the second largest variable.If the value is greater than the largest value then the largest value is put inside the second largest variable and current value is placed inside the largest variable.
See Video for more explanation.

Video Covers:
1)Finding second largest element in an array.
2)Multiple Approach for finding second Largest element.
3)Program to find second Largest number in an array.

#findingsecondlargest #codeforsecondlargestnumber #arrays

Please Like , Share , Subscribe and forward it to your friends.

Thank You

Links to previous videos:

find minimum and maximum value in array C++ | Arrays:

About Channel:
Our channel make videos related to programming. I hope my content helps you land the job of your dreams and become a better engineer!
Рекомендации по теме
Комментарии
Автор

Never seen such great explanation of this question😊 thank you sir 😍

ashishchhabra
Автор

Gajab explain 🤩🤩🤩🤩🤩 badhiya smaj me aya

python_master_
Автор

Time Complexity -> for 1st approach is O(NlogN) + O(N) to return second last element,
Second Approach Time Complexity -> O(N) for traversing the array once while the space complexity is constant linear time O(1) in both the cases.,
where N is the size of the given array.

BADASSBLADE
Автор

very nice explanation, but include if secLargest element == INT_MIN and then return -1

anshulsoni
Автор

What if the array is starting with largest element in array

abhisrivastava
Автор

what is the output for 5 5 5 5 5 5 5 this input in the array

nayantripathi
Автор

This Way Is Good
I would like to share another way(sorting[Descending])
Add this after sorting
i = 0;
one:
i++;
if (a[0] > a[i])
cout << "\n2nd Largest Number:" << a[i] << " ";
else
goto one;

Same output

gamingtherapy
Автор

Sir, apako pahale thoda code likhar explan karate to vo acche se samaj ata

rutikbodkeacademy
Автор

This code will not work for arr[ ] = {10, 10, 10, 10} its correct code is
int max1 = INT_MIN, max2= INT_MIN;
vector<int> ans;
for(int i=0; i<sizeOfArray; i++){
if(arr[i]>max1){
max2 = max1;
max1 = arr[i];
}
else if(arr[i]>max2 && arr[i]<max1){
max2 = arr[i];
}
}
if(max2==INT_MIN){
max2 = -1;
}
ans.push_back(max1);

ans.push_back(max2);
return ans;

BhupendraSinghsisodiya-or
Автор

what if the largest number is at first position

muneeburrehmankhan
Автор

This Program will FAIL for a cases like: A[3]={10, 10, 10} since all the elements are same, So what we can do we can write else if instead on else and put this condition "a[i]!=largest", what this condition would do is the if condition inside the else if part would only we checked if i'th element is not equal to the largest element variable.

Aditya_
Автор

if we put { 1, 2, 3, 4, 5 } its is not giving the correct output

idkwhoiam