From 9c11565b2d57ca63282d86bc9bfa134d54a69605 Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Wed, 5 Jun 2024 12:53:42 +0530 Subject: [PATCH] Added codes for 5 June --- GeeksForGeeks/June/05-6-24/GFG.java | 76 ++++++++++++++++++++++++++++ GeeksForGeeks/June/05-6-24/README.md | 2 + LeetCode/June/05-6-24/README.md | 2 + LeetCode/June/05-6-24/Solution.java | 24 +++++++++ 4 files changed, 104 insertions(+) create mode 100644 GeeksForGeeks/June/05-6-24/GFG.java create mode 100644 GeeksForGeeks/June/05-6-24/README.md create mode 100644 LeetCode/June/05-6-24/README.md create mode 100644 LeetCode/June/05-6-24/Solution.java diff --git a/GeeksForGeeks/June/05-6-24/GFG.java b/GeeksForGeeks/June/05-6-24/GFG.java new file mode 100644 index 0000000..e8f4325 --- /dev/null +++ b/GeeksForGeeks/June/05-6-24/GFG.java @@ -0,0 +1,76 @@ +//{ Driver Code Starts +// Initial Template for Java + +import java.io.*; +import java.util.*; + + +// } Driver Code Ends +// User function Template for Java + +class Solution +{ + long findSwapValues(long a[], int n, long b[], int m) + { + // Your code goes here + Set set = new HashSet<>(); + + long aSum = Arrays.stream(a).sum(); + long bSum = Arrays.stream(b).sum(); + long diff = Math.abs(aSum - bSum); + + if (diff == 0) + return 1; + + if ((aSum + bSum) % 2 == 1) + return -1; + + for (int i=0; i 0) { + String line = br.readLine(); + String[] q = line.trim().split("\\s+"); + int n = Integer.parseInt(q[0]); + int m = Integer.parseInt(q[1]); + String line1 = br.readLine(); + String[] a1 = line1.trim().split("\\s+"); + long a[] = new long[n]; + for (int i = 0; i < n; i++) { + a[i] = Long.parseLong(a1[i]); + } + String line2 = br.readLine(); + String[] a2 = line2.trim().split("\\s+"); + long b[] = new long[m]; + for (int i = 0; i < m; i++) { + b[i] = Long.parseLong(a2[i]); + } + Solution ob = new Solution(); + long ans = ob.findSwapValues(a, n, b, m); + System.out.println(ans); + } + } +} + +// } Driver Code Ends diff --git a/GeeksForGeeks/June/05-6-24/README.md b/GeeksForGeeks/June/05-6-24/README.md new file mode 100644 index 0000000..6d90d82 --- /dev/null +++ b/GeeksForGeeks/June/05-6-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(mlogm + nlogn) +Space complexity - O(1) diff --git a/LeetCode/June/05-6-24/README.md b/LeetCode/June/05-6-24/README.md new file mode 100644 index 0000000..f988889 --- /dev/null +++ b/LeetCode/June/05-6-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n*m) +Space complexity - O(1) diff --git a/LeetCode/June/05-6-24/Solution.java b/LeetCode/June/05-6-24/Solution.java new file mode 100644 index 0000000..9f897f8 --- /dev/null +++ b/LeetCode/June/05-6-24/Solution.java @@ -0,0 +1,24 @@ +class Solution +{ + public List commonChars(String[] words) + { + List ans = new ArrayList<>(); + int[] commonCount = new int[26]; + Arrays.fill(commonCount, Integer.MAX_VALUE); + + for (String a : words) + { + int[] count = new int[26]; + for (char c : a.toCharArray()) + ++count[c - 'a']; + for (int i = 0; i < 26; ++i) + commonCount[i] = Math.min(commonCount[i], count[i]); + } + + for (char c = 'a'; c <= 'z'; ++c) + for (int i = 0; i < commonCount[c - 'a']; ++i) + ans.add(String.valueOf(c)); + + return ans; + } +}