Skip to content

Commit

Permalink
Added codes for 10 May
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed May 10, 2024
1 parent ae9ec1b commit 59de072
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
68 changes: 68 additions & 0 deletions GeeksForGeeks/May/10-5-24/GFG.java
Original file line number Diff line number Diff line change
@@ -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<List<Integer>> ans = new Solution().CombinationSum2(a, n, k);
for (List<Integer> 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<List<Integer>> CombinationSum2(int arr[], int n, int k)
{
// Code Here.
List<List<Integer>> 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<Integer> current, List<List<Integer>> 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);
}
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/May/10-5-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(2^min(n,p))
Space complexity - O(n)
2 changes: 2 additions & 0 deletions LeetCode/May/10-5-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n * logn)
Space complexity - O(1)
45 changes: 45 additions & 0 deletions LeetCode/May/10-5-24/Solution.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 59de072

Please sign in to comment.