Skip to content

Commit

Permalink
Added codes for 17 May
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed May 17, 2024
1 parent 3041691 commit a76dca1
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
68 changes: 68 additions & 0 deletions GeeksForGeeks/May/17-5-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//{ Driver Code Starts
import java.io.*;
import java.util.*;

class IntArray {
public static int[] input(BufferedReader br, int n) throws IOException {
String[] s = br.readLine().trim().split(" ");
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = Integer.parseInt(s[i]);

return a;
}

public static void print(int[] a) {
for (int e : a) System.out.print(e + " ");
System.out.println();
}

public static void print(ArrayList<Integer> a) {
for (int e : a) System.out.print(e + " ");
System.out.println();
}
}

class GFG {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t;
t = Integer.parseInt(br.readLine());
while (t-- > 0) {

int n;
n = Integer.parseInt(br.readLine());

int x;
x = Integer.parseInt(br.readLine());

int[] arr = IntArray.input(br, n);

Solution obj = new Solution();
int res = obj.findPair(n, x, arr);

System.out.println(res);
}
}
}

// } Driver Code Ends



class Solution
{
public int findPair(int n, int x, int[] arr)
{
// code here
HashSet<Integer> set = new HashSet<>();
for(int i=0; i<n; i++)
{
if(set.contains(arr[i]-x) || set.contains(arr[i]+x))
return 1; //true

set.add(arr[i]);
}

return -1; //false
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/May/17-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(n)
2 changes: 2 additions & 0 deletions LeetCode/May/17-5-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(h)
33 changes: 33 additions & 0 deletions LeetCode/May/17-5-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution
{
public TreeNode removeLeafNodes(TreeNode root, int target)
{
if (root == null)
return null;

root.left = removeLeafNodes(root.left, target);
root.right = removeLeafNodes(root.right, target);

return isLeaf(root) && root.val == target ? null : root;
}

private boolean isLeaf(TreeNode root)
{
return root.left == null && root.right == null;
}
}

0 comments on commit a76dca1

Please sign in to comment.