-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Solve leetcode problem for sorting. Level easy
- Loading branch information
1 parent
fbd4824
commit 1e5c7fd
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
JavaDsaWithTest/src/main/java/org/practice/dsa/leet_code/easy/sorting/ThirdMax.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.practice.dsa.leet_code.easy.sorting; | ||
|
||
public class ThirdMax { | ||
public static void main(String[] args) { | ||
int[] arr = {2,1,3}; | ||
System.out.println(thirdMax(arr));; | ||
} | ||
|
||
public static int thirdMax(int[] nums) { | ||
|
||
sort(nums); | ||
int pointer =0; | ||
for (int i = nums.length-1; i >=0 ; i--) { | ||
if (i == nums.length-1 || nums[i] !=nums[i+1]) { | ||
pointer++; | ||
} | ||
|
||
if (pointer == 3) { | ||
return nums[i]; | ||
} | ||
} | ||
return nums[nums.length -1]; | ||
} | ||
|
||
private static void sort(int[] nums) { | ||
boolean isSwap; | ||
|
||
for (int i = 0; i < nums.length-1; i++) { | ||
isSwap = false; | ||
for (int j = 1; j < nums.length -1- i; j++) { | ||
if (nums[j] < nums[j-1]) { | ||
int temp = nums[j]; | ||
nums[j] = nums[j-1]; | ||
nums[j-1] = temp; | ||
isSwap = true; | ||
} | ||
} | ||
if (!isSwap) { | ||
break; | ||
} | ||
} | ||
} | ||
} |