diff --git a/GeeksForGeeks/May/10-5-24/GFG.java b/GeeksForGeeks/May/10-5-24/GFG.java new file mode 100644 index 0000000..f5a84eb --- /dev/null +++ b/GeeksForGeeks/May/10-5-24/GFG.java @@ -0,0 +1,68 @@ +//{ Driver Code Starts +// Initial Template for Java + +import java.io.*; +import java.lang.*; +import java.util.*; + +public class Main { + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + PrintWriter ot = new PrintWriter(System.out); + int t = Integer.parseInt(br.readLine().trim()); + while (t-- > 0) { + String s[] = br.readLine().trim().split(" "); + int n = Integer.parseInt(s[0]); + int k = Integer.parseInt(s[1]); + int a[] = new int[n]; + s = br.readLine().trim().split(" "); + for (int i = 0; i < n; i++) a[i] = Integer.parseInt(s[i]); + List> ans = new Solution().CombinationSum2(a, n, k); + for (List list : ans) { + for (int x : list) ot.print(x + " "); + ot.println(); + } + if (ans.size() == 0) ot.println(); + } + ot.close(); + } +} +// } Driver Code Ends + + +// User function Template for Java + +class Solution +{ + public List> CombinationSum2(int arr[], int n, int k) + { + // Code Here. + List> result = new ArrayList<>(); + Arrays.sort(arr); + subset(arr, n, k, new ArrayList<>(), result, 0, 0); + return result; + } + + private void subset(int arr[], int n, int k, List current, List> result, int sum, int index) + { + if (sum > k) + return; + + if (sum == k) + { + result.add(new ArrayList<>(current)); + return; + } + + for (int i = index; i < n; i++) + { + if (i > index && arr[i] == arr[i - 1]) + continue; // Skip duplicates + + current.add(arr[i]); + subset(arr, n, k, current, result, sum + arr[i], i + 1); + current.remove(current.size() - 1); + } + } +} diff --git a/GeeksForGeeks/May/10-5-24/README.md b/GeeksForGeeks/May/10-5-24/README.md new file mode 100644 index 0000000..80564cd --- /dev/null +++ b/GeeksForGeeks/May/10-5-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(2^min(n,p)) +Space complexity - O(n) diff --git a/LeetCode/May/10-5-24/README.md b/LeetCode/May/10-5-24/README.md new file mode 100644 index 0000000..7b1166a --- /dev/null +++ b/LeetCode/May/10-5-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n * logn) +Space complexity - O(1) diff --git a/LeetCode/May/10-5-24/Solution.java b/LeetCode/May/10-5-24/Solution.java new file mode 100644 index 0000000..70da23b --- /dev/null +++ b/LeetCode/May/10-5-24/Solution.java @@ -0,0 +1,45 @@ +class Solution +{ + public int[] kthSmallestPrimeFraction(int[] arr, int k) + { + int n = arr.length; + double left = 0, right = 1, mid; + int[] ans = new int[2]; + + while (left <= right) + { + mid = left + (right - left) / 2; + int j = 1, total = 0, num = 0, index = 0; + double maxFraction = 0; + + for (int i = 0; i < n; ++i) + { + while (j < n && arr[i] >= arr[j] * mid) + ++j; + + total += n - j; + + if (j < n && maxFraction < arr[i] * 1.0 / arr[j]) + { + maxFraction = arr[i] * 1.0 / arr[j]; + num = i; + index = j; + } + } + + if (total == k) + { + ans[0] = arr[num]; + ans[1] = arr[index]; + break; + } + + if (total > k) + right = mid; + else + left = mid; + } + + return ans; + } +}