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.
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); } }
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); } }
int max = 0; int smax = 0; if(arr.length < 2) return -1; for(int i = 0; i max){ smax = max; max = arr[i]; } else if ((arr[i]>smax) && (arr[i]!=max)) { smax = arr[i]; } } if(smax == 0) return -1; return smax; smax will hold the value of second largest, this code has only one iteration of the array. Complexity is O(n).
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; } }
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
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.
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
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);
}
}
I am thinking about same but it will work if there are duplicate elements in array like that in pprogram
@@trendinguniverse6113 yes it will work
if the first element is largest it will not work { 35, 4, 1, 34, 35, 35 };
@@sravan77751I guess it will work
program will continue to work as expected. thank you for the video
It will work as expected, since we are checking the highest number logic!!!
Good Logic Thanks,what if there are null values in array ,then how to find 2nd largest??
I think it will definitely work
Yes bro it will work
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);
}
}
Just using the builtin functions.
where is the brain applied?
What about array of negative numbers?
I don't think last code is suitable for all testcases
Any idea on what test case moght not pass.
When all elements are same then it will fail
@@dipalialone1298They Have Handle it. Watch again
int max = 0;
int smax = 0;
if(arr.length < 2)
return -1;
for(int i = 0; i max){
smax = max;
max = arr[i];
} else if ((arr[i]>smax) && (arr[i]!=max)) {
smax = arr[i];
}
}
if(smax == 0)
return -1;
return smax;
smax will hold the value of second largest, this code has only one iteration of the array. Complexity is O(n).
Will this work in an array of 100,000 integers?
Nope
Is this code from gfg ??
yes it'll work sir
For bigger test cases it is not working
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);
yes it will work surr**
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;
}
}
Awsm
Super
Bro pls make remove duplicate elements from array
you can easily remove duplicate element by using HashSet brother.
yes it will
Yes
at some cases of gfg , this code don't work .
If I add second largest element two time
Then this logic will give error
Better to make it as Hashset to remove duplicates
Yes that can be done. 👍
This logic won't work, if we take the array elements as a input from user.
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
Very bad logic...
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};