-
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
ae9ec1b
commit 59de072
Showing
4 changed files
with
117 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,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); | ||
} | ||
} | ||
} |
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(2^min(n,p)) | ||
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 * logn) | ||
Space complexity - O(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,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; | ||
} | ||
} |