-
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
c08108f
commit 3d4c616
Showing
4 changed files
with
118 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,83 @@ | ||
//{ Driver Code Starts | ||
//Initial Template for Java | ||
|
||
|
||
|
||
|
||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
|
||
// } Driver Code Ends | ||
//User function Template for Java | ||
|
||
|
||
|
||
class Solution | ||
{ | ||
ArrayList<Integer> findMissing(int a[], int b[], int n, int m) | ||
{ | ||
Set<Integer> visited = new HashSet<>(); | ||
for(int val: b) | ||
{ | ||
visited.add(val); | ||
} | ||
ArrayList<Integer> result = new ArrayList<>(); | ||
for(int val: a) | ||
{ | ||
if(!visited.contains(val)) | ||
{ | ||
result.add(val); | ||
} | ||
} | ||
return result; | ||
} | ||
} | ||
|
||
|
||
//{ 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) { | ||
// int n = Integer.parseInt(br.readLine()); | ||
String line = br.readLine(); | ||
String[] q = line.trim().split("\\s+"); | ||
int n =Integer.parseInt(q[0]); | ||
int m =Integer.parseInt(q[1]); | ||
//int y =Integer.parseInt(q[2]); | ||
String line1 = br.readLine(); | ||
String[] a1 = line1.trim().split("\\s+"); | ||
int a[] = new int[n]; | ||
for (int i = 0; i < n; i++) { | ||
a[i] = Integer.parseInt(a1[i]); | ||
} | ||
String line2 = br.readLine(); | ||
String[] a2 = line2.trim().split("\\s+"); | ||
int b[] = new int[m]; | ||
for (int i = 0; i < m; i++) { | ||
b[i] = Integer.parseInt(a2[i]); | ||
} | ||
Solution ob = new Solution(); | ||
ArrayList<Integer> ans=ob.findMissing(a,b,n,m); | ||
for (int i = 0; i < ans.size(); i++) { | ||
System.out.print(ans.get(i)+" "); | ||
} | ||
System.out.println(); | ||
|
||
} | ||
} | ||
} | ||
|
||
|
||
|
||
// } 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(n+m) | ||
Space complexity - O(n) |
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(n*m) |
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,31 @@ | ||
class Solution | ||
{ | ||
public int numIslands(char[][] grid) | ||
{ | ||
int ans = 0; | ||
|
||
for (int i = 0; i < grid.length; ++i) | ||
for (int j = 0; j < grid[0].length; ++j) | ||
if (grid[i][j] == '1') | ||
{ | ||
dfs(grid, i, j); | ||
++ans; | ||
} | ||
|
||
return ans; | ||
} | ||
|
||
private void dfs(char[][] grid, int i, int j) | ||
{ | ||
if (i < 0 || i == grid.length || j < 0 || j == grid[0].length) | ||
return; | ||
if (grid[i][j] != '1') | ||
return; | ||
|
||
grid[i][j] = '2'; | ||
dfs(grid, i + 1, j); | ||
dfs(grid, i - 1, j); | ||
dfs(grid, i, j + 1); | ||
dfs(grid, i, j - 1); | ||
} | ||
} |