Skip to content

Commit

Permalink
Added codes for 21 May
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed May 21, 2024
1 parent c6bcd8a commit a94b9e0
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
103 changes: 103 additions & 0 deletions GeeksForGeeks/May/21-5-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//{ Driver Code Starts
// Initial Template for Java

import java.io.*;
import java.util.*;

public class GFG {

public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int tc = Integer.parseInt(br.readLine().trim());
while (tc-- > 0) {
String[] inputLine;
inputLine = br.readLine().trim().split(" ");
int n = Integer.parseInt(inputLine[0]);
int[] arr = new int[n];
inputLine = br.readLine().trim().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(inputLine[i]);
}
inputLine = br.readLine().trim().split(" ");
int k = Integer.parseInt(inputLine[0]);
int x = Integer.parseInt(inputLine[1]);

int[] ans = new Solution().printKClosest(arr, n, k, x);
for (int xx : ans) {
System.out.print(xx + " ");
}
System.out.println();
}
}
}

// } Driver Code Ends


// User function Template for Java

class Solution
{
int[] printKClosest(int[] arr, int n, int k, int x)
{
// code here
int crossIdx = findCrossOver(arr, 0, n - 1, x);
int rightIdx = crossIdx + 1;

if (arr[crossIdx] == x)
crossIdx--;

int ans[] = new int[k];

for (int i = 0; i < k; i++)
{
if (crossIdx >= 0 && rightIdx < n)
{
int leftDiff = x - arr[crossIdx];
int rightDiff = arr[rightIdx] - x;

if (leftDiff < rightDiff)
{
ans[i] = arr[crossIdx];
crossIdx--;
}
else
{
ans[i] = arr[rightIdx];
rightIdx++;
}
}
else if (crossIdx >= 0)
{
ans[i] = arr[crossIdx];
crossIdx--;
}
else
{
ans[i] = arr[rightIdx];
rightIdx++;
}
}

return ans;
}

private int findCrossOver(int[] arr, int low, int high, int x)
{
if (arr[high] <= x)
return high;

if (arr[low] > x)
return low;

int mid = (low + high) / 2;

if (arr[mid] <= x && arr[mid + 1] > x)
return mid;

else if (arr[mid] < x)
return findCrossOver(arr, mid + 1, high, x);

return findCrossOver(arr, low, mid - 1, x);
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/May/21-5-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(logn+k)
Space complexity - O(k)
2 changes: 2 additions & 0 deletions LeetCode/May/21-5-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(2^n)
Space complexity - O(n*2^n)
21 changes: 21 additions & 0 deletions LeetCode/May/21-5-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution
{
public List<List<Integer>> subsets(int[] nums)
{
List<List<Integer>> ans = new ArrayList<>();
dfs(nums, 0, new ArrayList<>(), ans);
return ans;
}

private void dfs(int[] nums, int s, List<Integer> path, List<List<Integer>> ans)
{
ans.add(new ArrayList<>(path));

for (int i = s; i < nums.length; ++i)
{
path.add(nums[i]);
dfs(nums, i + 1, path, ans);
path.remove(path.size() - 1);
}
}
}

0 comments on commit a94b9e0

Please sign in to comment.