From 174be6084f6ef492d32d32cc4c98e280e281d49a Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Sat, 8 Jun 2024 10:39:52 +0530 Subject: [PATCH] Added codes for 8 June --- GeeksForGeeks/June/08-6-24/GFG.java | 47 ++++++++++++++++++++++++++++ GeeksForGeeks/June/08-6-24/README.md | 2 ++ LeetCode/June/08-6-24/README.md | 2 ++ LeetCode/June/08-6-24/Solution.java | 27 ++++++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 GeeksForGeeks/June/08-6-24/GFG.java create mode 100644 GeeksForGeeks/June/08-6-24/README.md create mode 100644 LeetCode/June/08-6-24/README.md create mode 100644 LeetCode/June/08-6-24/Solution.java diff --git a/GeeksForGeeks/June/08-6-24/GFG.java b/GeeksForGeeks/June/08-6-24/GFG.java new file mode 100644 index 0000000..6e65a00 --- /dev/null +++ b/GeeksForGeeks/June/08-6-24/GFG.java @@ -0,0 +1,47 @@ +//{ Driver Code Starts +import java.util.*; + +class ExtraElement { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int t = sc.nextInt(); + while (t-- > 0) { + int n = sc.nextInt(); + int[] a = new int[n]; + int[] b = new int[n - 1]; + for (int i = 0; i < n; i++) a[i] = sc.nextInt(); + for (int i = 0; i < n - 1; i++) b[i] = sc.nextInt(); + Solution g = new Solution(); + System.out.println(g.findExtra(n, a, b)); + } + } +} +// } Driver Code Ends + + +/*Complete the function below*/ +class Solution +{ + public int findExtra(int n, int arr1[], int arr2[]) + { + // add code here. + int index = n-1; + + int left = 0, right = n - 2; + while (left <= right) + { + int mid = (left+right) / 2; + + if (arr2[mid] == arr1[mid]) + left = mid + 1; + + else + { + index = mid; + right = mid - 1; + } + } + + return index; + } +} diff --git a/GeeksForGeeks/June/08-6-24/README.md b/GeeksForGeeks/June/08-6-24/README.md new file mode 100644 index 0000000..ba09c45 --- /dev/null +++ b/GeeksForGeeks/June/08-6-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(logn) +Space complexity - O(1) diff --git a/LeetCode/June/08-6-24/README.md b/LeetCode/June/08-6-24/README.md new file mode 100644 index 0000000..7384c4b --- /dev/null +++ b/LeetCode/June/08-6-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(n) diff --git a/LeetCode/June/08-6-24/Solution.java b/LeetCode/June/08-6-24/Solution.java new file mode 100644 index 0000000..e1cfba3 --- /dev/null +++ b/LeetCode/June/08-6-24/Solution.java @@ -0,0 +1,27 @@ +class Solution +{ + public boolean checkSubarraySum(int[] nums, int k) + { + int prefix = 0; + Map prefixToIndex = new HashMap<>(); + prefixToIndex.put(0, -1); + + for (int i = 0; i < nums.length; ++i) + { + prefix += nums[i]; + if (k != 0) + prefix %= k; + if (prefixToIndex.containsKey(prefix)) + { + if (i - prefixToIndex.get(prefix) > 1) + return true; + } + else + { + prefixToIndex.put(prefix, i); + } + } + + return false; + } +}