-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a39cdea
commit 9c11565
Showing
4 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(mlogm + nlogn) | ||
Space complexity - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n*m) | ||
Space complexity - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |