From faf5d53b71d9a5ee25425a651ef451553b39ad82 Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Mon, 22 Jan 2024 11:16:00 +0530 Subject: [PATCH] Added codes for 22 Jan --- GeeksForGeeks/.README.md.swp | Bin 0 -> 1024 bytes GeeksForGeeks/22-1-24/GFG.java | 170 ++++++++++++++++++++++++++++++++ GeeksForGeeks/22-1-24/README.md | 2 + LeetCode/22-1-24/README.md | 2 + LeetCode/22-1-24/Solution.java | 30 ++++++ 5 files changed, 204 insertions(+) create mode 100644 GeeksForGeeks/.README.md.swp create mode 100644 GeeksForGeeks/22-1-24/GFG.java create mode 100644 GeeksForGeeks/22-1-24/README.md create mode 100644 LeetCode/22-1-24/README.md create mode 100644 LeetCode/22-1-24/Solution.java diff --git a/GeeksForGeeks/.README.md.swp b/GeeksForGeeks/.README.md.swp new file mode 100644 index 0000000000000000000000000000000000000000..e3c217b086e09c12bdcc0495b5b2913b8e874d35 GIT binary patch literal 1024 zcmYc?$V<%2S1{8vVn6{PZb@QZZepcwR$^wJZcZtRGzupKqRtDb4n-WD6XfdX;_IrH Sn}RMe${!7Z(GVDr5C8x&IS*6- literal 0 HcmV?d00001 diff --git a/GeeksForGeeks/22-1-24/GFG.java b/GeeksForGeeks/22-1-24/GFG.java new file mode 100644 index 0000000..a01eecf --- /dev/null +++ b/GeeksForGeeks/22-1-24/GFG.java @@ -0,0 +1,170 @@ +//{ Driver Code Starts +import java.util.*; +import java.io.*; + +class Node { + int data; + Node left; + Node right; + Node(int data) { + this.data = data; + left = null; + right = null; + } +} + +public class GfG { + public static Node buildTree(String str) { + + if (str.length() == 0 || str.charAt(0) == 'N') { + return null; + } + + String ip[] = str.split(" "); + // Create the root of the tree + Node root = new Node(Integer.parseInt(ip[0])); + // Push the root to the queue + + Queue queue = new LinkedList<>(); + + queue.add(root); + // Starting from the second element + + int i = 1; + while (queue.size() > 0 && i < ip.length) { + + // Get and remove the front of the queue + Node currNode = queue.peek(); + queue.remove(); + + // Get the current node's value from the string + String currVal = ip[i]; + + // If the left child is not null + if (!currVal.equals("N")) { + + // Create the left child for the current node + currNode.left = new Node(Integer.parseInt(currVal)); + // Push it to the queue + queue.add(currNode.left); + } + + // For the right child + i++; + if (i >= ip.length) break; + + currVal = ip[i]; + + // If the right child is not null + if (!currVal.equals("N")) { + + // Create the right child for the current node + currNode.right = new Node(Integer.parseInt(currVal)); + + // Push it to the queue + queue.add(currNode.right); + } + i++; + } + + return root; + } + + + public static void main(String[] args) + { + Scanner scn=new Scanner(System.in); + + int t = Integer.parseInt(scn.nextLine()); + + while (t-- > 0) + { + int s = Integer.parseInt(scn.nextLine()); + // sc.nextLine(); + String res = scn.nextLine(); + // sc.nextLine(); + Node root = buildTree(res); + + Solution ob=new Solution(); + ArrayList> ans= ob.printPaths(root, s); + + Collections.sort(ans, new ElementWiseComparator()); + + for(int i=0;i> { + @Override + public int compare(ArrayList list1, ArrayList list2) { + int minLength = Math.min(list1.size(), list2.size()); + + for (int i = 0; i < minLength; i++) { + int comparison = Integer.compare(list1.get(i), list2.get(i)); + if (comparison != 0) { + return comparison; + } + } + + return Integer.compare(list1.size(), list2.size()); + } + } +} +// } Driver Code Ends + + +//User function Template for Java + +/*Tree Node +class Node { + int data; + Node left; + Node right; + Node(int data) { + this.data = data; + left = null; + right = null; + } +} +*/ + +class Solution +{ + public static ArrayList> printPaths(Node root, int sum) + { + // code here + ArrayList> ans =new ArrayList<>(); + solve(root, 0, sum, new ArrayList(), ans); + return ans; + } + + public static void solve(Node rt, int cur, int sum, ArrayList a, ArrayList> ans){ + if(rt==null) + return; + + cur+=rt.data; + a.add(rt.data); + + if(cur==sum) + { + ArrayList res = new ArrayList<>(); + + for(int k: a) + res.add(k); + + ans.add(res); + } + + solve(rt.left, cur, sum, a, ans); + solve(rt.right, cur,sum,a,ans); + + a.remove(a.size()-1); + } +} diff --git a/GeeksForGeeks/22-1-24/README.md b/GeeksForGeeks/22-1-24/README.md new file mode 100644 index 0000000..b0ea7a8 --- /dev/null +++ b/GeeksForGeeks/22-1-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n^2) +Space complexity - O(n) diff --git a/LeetCode/22-1-24/README.md b/LeetCode/22-1-24/README.md new file mode 100644 index 0000000..9567f26 --- /dev/null +++ b/LeetCode/22-1-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(1) diff --git a/LeetCode/22-1-24/Solution.java b/LeetCode/22-1-24/Solution.java new file mode 100644 index 0000000..c23946a --- /dev/null +++ b/LeetCode/22-1-24/Solution.java @@ -0,0 +1,30 @@ +class Solution { + public int[] findErrorNums(int[] nums) + { + int i=0; + while(i