diff --git a/DSA/AnushBohra_590017415/Day_47/Q1_K-th_Smallest.c b/DSA/AnushBohra_590017415/Day_47/Q1_K-th_Smallest.c new file mode 100644 index 000000000..4d58e408f --- /dev/null +++ b/DSA/AnushBohra_590017415/Day_47/Q1_K-th_Smallest.c @@ -0,0 +1,58 @@ +#include +#include + +// Helper macro for maximum +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) + +int kthSmallest(const int* A, int m, const int* B, int n, int k) { + // Ensure A is the smaller array to minimize the binary search range + if (m > n) { + return kthSmallest(B, n, A, m, k); + } + + // Binary search range for how many elements to pick from A + int low = MAX(0, k - n); + int high = MIN(k, m); + + while (low <= high) { + int cut1 = low + (high - low) / 2; + int cut2 = k - cut1; + + // Elements around the cut in array A + int l1 = (cut1 == 0) ? INT_MIN : A[cut1 - 1]; + int r1 = (cut1 == m) ? INT_MAX : A[cut1]; + + // Elements around the cut in array B + int l2 = (cut2 == 0) ? INT_MIN : B[cut2 - 1]; + int r2 = (cut2 == n) ? INT_MAX : B[cut2]; + + // Valid partition found + if (l1 <= r2 && l2 <= r1) { + return MAX(l1, l2); + } + // Need to take fewer elements from A + else if (l1 > r2) { + high = cut1 - 1; + } + // Need to take more elements from A + else { + low = cut1 + 1; + } + } + + return -1; // Should not be reached if k is valid +} + +int main(void) { + int A[] = {2, 3, 6, 7}; + int B[] = {1, 4, 5, 8}; + int m = sizeof(A) / sizeof(A[0]); + int n = sizeof(B) / sizeof(B[0]); + int k = 5; + + int result = kthSmallest(A, m, B, n, k); + printf("The %d-th smallest element is: %d\n", k, result); // Output: 5 + + return 0; +} \ No newline at end of file diff --git a/DSA/AnushBohra_590017415/Day_47/Q2_leetcode.c b/DSA/AnushBohra_590017415/Day_47/Q2_leetcode.c new file mode 100644 index 000000000..6cd5ee677 --- /dev/null +++ b/DSA/AnushBohra_590017415/Day_47/Q2_leetcode.c @@ -0,0 +1,77 @@ +#include +#include +#include + +typedef struct { + int score; + int original_index; +} Athlete; + +int compare(const void* a, const void* b) { + Athlete* athleteA = (Athlete*)a; + Athlete* athleteB = (Athlete*)b; + return athleteB->score - athleteA->score; +} + +char** findRelativeRanks(int* score, int scoreSize, int* returnSize) { + *returnSize = scoreSize; + + char** result = (char**)malloc(scoreSize * sizeof(char*)); + Athlete* athletes = (Athlete*)malloc(scoreSize * sizeof(Athlete)); + + for (int i = 0; i < scoreSize; i++) { + athletes[i].score = score[i]; + athletes[i].original_index = i; + } + + qsort(athletes, scoreSize, sizeof(Athlete), compare); + + for (int rank = 0; rank < scoreSize; rank++) { + int orig_idx = athletes[rank].original_index; + result[orig_idx] = (char*)malloc(15 * sizeof(char)); + + if (rank == 0) { + strcpy(result[orig_idx], "Gold Medal"); + } else if (rank == 1) { + strcpy(result[orig_idx], "Silver Medal"); + } else if (rank == 2) { + strcpy(result[orig_idx], "Bronze Medal"); + } else { + sprintf(result[orig_idx], "%d", rank + 1); + } + } + + free(athletes); + return result; +} + +// Helper function to print results and clean up memory +void printAndFreeResult(char** result, int size) { + printf("["); + for (int i = 0; i < size; i++) { + printf("\"%s\"%s", result[i], (i == size - 1) ? "" : ", "); + free(result[i]); // Free individual string allocation + } + printf("]\n"); + free(result); // Free array of string pointers +} + +int main() { + int returnSize; + + // Test Case 1 + int score1[] = {5, 4, 3, 2, 1}; + int size1 = sizeof(score1) / sizeof(score1[0]); + char** result1 = findRelativeRanks(score1, size1, &returnSize); + printf("Test Case 1 Output:\n"); + printAndFreeResult(result1, returnSize); + + // Test Case 2 + int score2[] = {10, 3, 8, 9, 4}; + int size2 = sizeof(score2) / sizeof(score2[0]); + char** result2 = findRelativeRanks(score2, size2, &returnSize); + printf("\nTest Case 2 Output:\n"); + printAndFreeResult(result2, returnSize); + + return 0; +} \ No newline at end of file