Skip to content

Commit

Permalink
Added codes for 5 June
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Jun 5, 2024
1 parent a39cdea commit 9c11565
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
76 changes: 76 additions & 0 deletions GeeksForGeeks/June/05-6-24/GFG.java
Original file line number Diff line number Diff line change
@@ -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<Long> 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<a.length; i++)
set.add(a[i]);

for (int i=0; i<b.length; i++)
if (set.contains(diff-b[i]))
return 1;

return -1;
}
}


//{ Driver Code Starts.

// Driver class
class Array {

// Driver code
public static void main(String[] args) throws IOException {
// Taking input using buffered reader
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int testcases = Integer.parseInt(br.readLine());
// looping through all testcases
while (testcases-- > 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
2 changes: 2 additions & 0 deletions GeeksForGeeks/June/05-6-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(mlogm + nlogn)
Space complexity - O(1)
2 changes: 2 additions & 0 deletions LeetCode/June/05-6-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n*m)
Space complexity - O(1)
24 changes: 24 additions & 0 deletions LeetCode/June/05-6-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution
{
public List<String> commonChars(String[] words)
{
List<String> 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;
}
}

0 comments on commit 9c11565

Please sign in to comment.