Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions DSA/Gautam_Shyamnani_590013078/Day_47/Question1_Leetcode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public String[] findRelativeRanks(int[] score) {
int n = score.length;

int[][] arr = new int[n][2];
for(int i = 0; i < n; i++){
arr[i][0] = score[i];
arr[i][1] = i;
}
Arrays.sort(arr,(a,b) -> b[0] - a[0]);
String[] ans = new String[n];
for(int i = 0;i < n; i++){
int idx = arr[i][1];
if(i == 0) ans[idx] = "Gold Medal";
else if(i == 1) ans[idx] = "Silver Medal";
else if(i == 2) ans[idx] = "Bronze Medal";
else ans[idx] = String.valueOf(i + 1);
}
return ans;
}
}
47 changes: 47 additions & 0 deletions DSA/Gautam_Shyamnani_590013078/Day_47/Question2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import java.util.*;

public class Question2 {

public static int kthElement(int[] A, int[] B, int k) {
int n = A.length;
int m = B.length;

if (n > m) {
return kthElement(B, A, k);
}

int low = Math.max(0, k - m);
int high = Math.min(k, n);

while (low <= high) {
int cut1 = (low + high) / 2;
int cut2 = k - cut1;

int l1 = (cut1 == 0) ? Integer.MIN_VALUE : A[cut1 - 1];
int l2 = (cut2 == 0) ? Integer.MIN_VALUE : B[cut2 - 1];

int r1 = (cut1 == n) ? Integer.MAX_VALUE : A[cut1];
int r2 = (cut2 == m) ? Integer.MAX_VALUE : B[cut2];

if (l1 <= r2 && l2 <= r1) {
return Math.max(l1, l2);
}
else if (l1 > r2) {
high = cut1 - 1;
}
else {
low = cut1 + 1;
}
}

return -1;
}

public static void main(String[] args) {
int[] A = {2, 3, 6, 7};
int[] B = {1, 4, 5, 8};
int k = 5;

System.out.println(kthElement(A, B, k));
}
}