No video

Find Second Largest Number From Array | Java Interview Questions and Answers

  Рет қаралды 39,765

CloudTech

CloudTech

Күн бұрын

In this video, we showed you how to find the second highest number in Java.
#javainterviewquestions
#javacodinginterviewquestions
#javaprogramming

Пікірлер: 43
@pradeepkundekar4376
@pradeepkundekar4376 Жыл бұрын
Answer: Yes it will definitely work. I have few approaches to solve this problem: Approach 1: Step 1: Add all the elements in a Set. Step 2: Now add all elements from set into a new ArrayList and Sort. Step 3 : Get size -2 th element Time Complexity O(nlogn) Space Complexity O(n) Approach 2: (Using Heap or PriorityQueue) Step 1: Add All elements into a set to remove duplicates Step 2: Iterate through each elements of the set and add it into the Min Heap. Step 3: Check if the size of Heap >2 then pop the top elements. Step 4: Return Top element Time Complexity: O(nlog2) Space Complexity:O(n) If there were no Duplicates then Space Complexity can Be constant.
@bheemshankar_pk
@bheemshankar_pk 4 ай бұрын
Good Logic Thanks,what if there are null values in array ,then how to find 2nd largest??
@sandeshkulkarni6941
@sandeshkulkarni6941 Жыл бұрын
I think first we have to remove duplicate elements from array and then we sort, After that there no problem if we give any updated array
@unknownplayer0383
@unknownplayer0383 Жыл бұрын
program will continue to work as expected. thank you for the video
@stayvloging
@stayvloging Жыл бұрын
public class SecondLargestNumberFromTheArray { public static void main(String[] args) { int arr[] = { 1, 4, 35, 34, 35, 35 }; int largest = arr[0]; int secondLargest = arr[0]; for (int i = 0; i < arr.length; i++) { if (largest < arr[i]) { secondLargest = largest; largest = arr[i]; } else if (secondLargest < arr[i] && largest > arr[i]) { secondLargest = arr[i]; } } System.out.println(secondLargest); } }
@trendinguniverse6113
@trendinguniverse6113 Жыл бұрын
I am thinking about same but it will work if there are duplicate elements in array like that in pprogram
@stayvloging
@stayvloging Жыл бұрын
@@trendinguniverse6113 yes it will work
@sravan77751
@sravan77751 Жыл бұрын
if the first element is largest it will not work { 35, 4, 1, 34, 35, 35 };
@harikishangrandhe3381
@harikishangrandhe3381 8 ай бұрын
@@sravan77751I guess it will work
@prashanthreddy-sd9ju
@prashanthreddy-sd9ju 6 күн бұрын
In java 8 you would write omething like Arrays.stream(are).sorted(comparator.reverseorder()).limit(2).skip(1). findfirsf().get();
@sathiskumarp
@sathiskumarp Жыл бұрын
It will work as expected, since we are checking the highest number logic!!!
@omkar_gopchade143
@omkar_gopchade143 Жыл бұрын
I think it will definitely work
@dreamplaying-wg1jn
@dreamplaying-wg1jn Жыл бұрын
Yes bro it will work
@stayvloging
@stayvloging Жыл бұрын
public class SecondLargestNumberFromTheArray { public static void main(String[] args) { int arr[] = { 1, 4, 35, 34, 35, 35 }; int largest = arr[0]; int secondLargest = arr[0]; for (int i = 0; i < arr.length; i++) { if (largest < arr[i]) { secondLargest = largest; largest = arr[i]; } else if (secondLargest < arr[i] && largest > arr[i]) { secondLargest = arr[i]; } } System.out.println(secondLargest); } }
@newanurag
@newanurag Жыл бұрын
Just using the builtin functions. where is the brain applied?
@akhilraj5268
@akhilraj5268 Жыл бұрын
I don't think last code is suitable for all testcases
@cloudtech5260
@cloudtech5260 Жыл бұрын
Any idea on what test case moght not pass.
@dipalialone1298
@dipalialone1298 Жыл бұрын
When all elements are same then it will fail
@pradeepkundekar4376
@pradeepkundekar4376 Жыл бұрын
@@dipalialone1298They Have Handle it. Watch again
@bhargavmehta97
@bhargavmehta97 Жыл бұрын
What about array of negative numbers?
@siddumaradi7045
@siddumaradi7045 Жыл бұрын
yes it'll work sir
@rmcf3972
@rmcf3972 4 ай бұрын
Will this work in an array of 100,000 integers?
@princemohammed921
@princemohammed921 Жыл бұрын
For bigger test cases it is not working
@manukarthikeya4560
@manukarthikeya4560 3 ай бұрын
Is this code from gfg ??
@trendinguniverse6113
@trendinguniverse6113 Жыл бұрын
Bro pls make remove duplicate elements from array
@girijasankardas1189
@girijasankardas1189 Жыл бұрын
you can easily remove duplicate element by using HashSet brother.
@jeckrazi7691
@jeckrazi7691 Жыл бұрын
Hi @CloudTech, Stream.of(1,4,5,35,34,35) .sorted((n1,n2)-> Integer.compare(n2,n1)) .distinct() .limit(2) .skip(1) .forEach(System.out::println);
@coolraviraj24
@coolraviraj24 Жыл бұрын
yes it will
@nitheeshreddy6863
@nitheeshreddy6863 Жыл бұрын
hi bro i have used this logic can u please check this.. int array[]=new int [] {1,4,5,35,35,35,34}; Arrays.sort(array); //1,2,4,34,35,35; int highest=array[array.length-1]; for(int i=array.length-2;i>=0;i--) { if(highest>array[i]) { System.out.println(array[i]); break; } }
@iceberg5956
@iceberg5956 Жыл бұрын
Awsm
@venkateshvardhineedi9571
@venkateshvardhineedi9571 Жыл бұрын
Super
@gautamnegi6868
@gautamnegi6868 Жыл бұрын
Yes
@chaitanaywaskar6008
@chaitanaywaskar6008 Жыл бұрын
at some cases of gfg , this code don't work .
@user-we5wg4ki6c
@user-we5wg4ki6c 5 ай бұрын
If I add second largest element two time Then this logic will give error
@yogeshganpule2695
@yogeshganpule2695 Жыл бұрын
This will fail in case you have 2 duplicate elements as smallest . for ex int[] arr = {1, 2, 5, 7, 8, 9, -14, 56, 98, -3, -14, 2};
@ChennaiCineCuts
@ChennaiCineCuts Жыл бұрын
Better to make it as Hashset to remove duplicates
@cloudtech5260
@cloudtech5260 Жыл бұрын
Yes that can be done. 👍
@shaikhkhizar3167
@shaikhkhizar3167 Жыл бұрын
This logic won't work, if we take the array elements as a input from user.
@hariprasadmutalikdesai6504
@hariprasadmutalikdesai6504 Жыл бұрын
But code working with user input with me. and code is import java.util.Arrays; import java.util.Scanner; public class MainClass { public static void main(String[] args) { printsecondlargest(); } public static void printsecondlargest() { int n; Scanner sc = new Scanner(System.in); System.out.println("Enter the No of Elements to store in array : "); n = sc.nextInt(); int[] array = new int[n]; System.out.println("Enter the array Elements : "); for (int i=0; i 0; i--) { if(array[i] != array[size-1]) { System.out.println("Second Largest Element is "+array[i]); return; } } System.out.println("No such Element Present"); } } Output: Enter the No of Elements to store in array : 6 Enter the array Elements : 13 35 35 23 34 13 Second Largest Element is 34
@bhushantaywadevlogs9552
@bhushantaywadevlogs9552 Жыл бұрын
Very bad logic...
Java Program to Find the Second Highest Number in an Array
9:26
Programming Tutorials
Рет қаралды 79 М.
Fortunately, Ultraman protects me  #shorts #ultraman #ultramantiga #liveaction
00:10
КТО ЛЮБИТ ГРИБЫ?? #shorts
00:24
Паша Осадчий
Рет қаралды 3,6 МЛН
SPILLED CHOCKY MILK PRANK ON BROTHER 😂 #shorts
00:12
Savage Vlogs
Рет қаралды 49 МЛН
Find Second Largest Number in an Array in Java (Hindi)
36:29
Smart Programming
Рет қаралды 200 М.
Top 50 Most Asked JavaScript Logical Interview Questions || Must Watch🤯😱
1:09:02
Infosys java interview questions | 3 years of experience | 12+ LPA
30:54
Drunken Engineer
Рет қаралды 37 М.
01. Internal Working of HashMap & Java-8 Enhancement
19:11
WebEncyclop Tutorials
Рет қаралды 111 М.
Learn Java in 14 Minutes (seriously)
14:00
Alex Lee
Рет қаралды 4,7 МЛН