-
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.
- Loading branch information
1 parent
066f151
commit 2200dd6
Showing
4 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
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 @@ | ||
//{ Driver Code Starts | ||
import java.util.*; | ||
import java.io.*; | ||
import java.lang.*; | ||
|
||
public class GfG { | ||
public static void main (String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
int t = Integer.parseInt(br.readLine().trim()); | ||
while(t-->0){ | ||
String inputLine[] = br.readLine().trim().split(" "); | ||
int n = Integer.parseInt(inputLine[0]); | ||
int k = Integer.parseInt(inputLine[1]); | ||
int[] arr = new int[n]; | ||
inputLine = br.readLine().trim().split(" "); | ||
for(int i=0; i<n; i++)arr[i] = Integer.parseInt(inputLine[i]); | ||
|
||
Solution obj = new Solution(); | ||
System.out.println(obj.firstElementKTime(n, k, arr)); | ||
} | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
class Solution | ||
{ | ||
public int firstElementKTime(int n, int k, int[] a) | ||
{ | ||
HashMap<Integer, Integer> count_map = new HashMap<>(); | ||
for (int i = 0; i < n; i++) | ||
{ | ||
count_map.put(a[i], count_map.getOrDefault(a[i], 0) + 1); | ||
if (count_map.get(a[i]) == k) | ||
{ | ||
return a[i]; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
} |
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,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(n) |
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,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(n) |
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,17 @@ | ||
class Solution | ||
{ | ||
public int[] sortedSquares(int[] nums) | ||
{ | ||
int n = nums.length; | ||
int[] ans = new int[n]; | ||
int i = n - 1; | ||
|
||
for (int l = 0, r = n - 1; l <= r;) | ||
if (Math.abs(nums[l]) > Math.abs(nums[r])) | ||
ans[i--] = nums[l] * nums[l++]; | ||
else | ||
ans[i--] = nums[r] * nums[r--]; | ||
|
||
return ans; | ||
} | ||
} |