From 2ab7181e709517f1613f66f95dfc3c1fb5db173f Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Sat, 18 May 2024 18:42:16 +0530 Subject: [PATCH] Added codes for 18 May --- GeeksForGeeks/May/18-5-24/GFG.java | 51 +++++++++++++++++++++++++++++ GeeksForGeeks/May/18-5-24/README.md | 2 ++ LeetCode/May/18-5-24/README.md | 2 ++ LeetCode/May/18-5-24/Solution.java | 38 +++++++++++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 GeeksForGeeks/May/18-5-24/GFG.java create mode 100644 GeeksForGeeks/May/18-5-24/README.md create mode 100644 LeetCode/May/18-5-24/README.md create mode 100644 LeetCode/May/18-5-24/Solution.java diff --git a/GeeksForGeeks/May/18-5-24/GFG.java b/GeeksForGeeks/May/18-5-24/GFG.java new file mode 100644 index 0000000..e342967 --- /dev/null +++ b/GeeksForGeeks/May/18-5-24/GFG.java @@ -0,0 +1,51 @@ +//{ Driver Code Starts +import java.io.*; +import java.lang.*; +import java.util.*; + +class GFG { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int T = Integer.parseInt(br.readLine().trim()); + while (T-- > 0) { + String s = br.readLine().trim(); + int n = Integer.parseInt(s); + String S = br.readLine(); + String[] s1 = S.split(" "); + List a = new ArrayList(); + for (int i = 0; i < n; i++) { + a.add(Integer.parseInt(s1[i])); + } + Solution ob = new Solution(); + int ans = ob.findPeakElement(a); + System.out.println(ans); + } + } +} + +// } Driver Code Ends + + +class Solution { + public int findPeakElement(List a) { + // Code here + + // Collections.sort(a); + // return a.get(a.size()-1); + + int left = 0; + int right = a.size() - 1; + + while (left < right) + { + int mid = left + (right - left) / 2; + + if (a.get(mid) > a.get(right)) + right = mid; + else + left = mid + 1; + } + + return a.get(left); + } +} diff --git a/GeeksForGeeks/May/18-5-24/README.md b/GeeksForGeeks/May/18-5-24/README.md new file mode 100644 index 0000000..ba09c45 --- /dev/null +++ b/GeeksForGeeks/May/18-5-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(logn) +Space complexity - O(1) diff --git a/LeetCode/May/18-5-24/README.md b/LeetCode/May/18-5-24/README.md new file mode 100644 index 0000000..722dfdc --- /dev/null +++ b/LeetCode/May/18-5-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(h) diff --git a/LeetCode/May/18-5-24/Solution.java b/LeetCode/May/18-5-24/Solution.java new file mode 100644 index 0000000..fb7cc2e --- /dev/null +++ b/LeetCode/May/18-5-24/Solution.java @@ -0,0 +1,38 @@ +/** + * 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 int distributeCoins(TreeNode root) + { + dfs(root); + return ans; + } + + private int ans = 0; + + // Returns the number of coins I can give (positive) / take (negative). + private int dfs(TreeNode root) + { + if (root == null) + return 0; + + int l = dfs(root.left); + int r = dfs(root.right); + ans += Math.abs(l) + Math.abs(r); + + return (root.val - 1) + l + r; + } +}