diff --git a/GeeksForGeeks/April/19-4-24/GFG.java b/GeeksForGeeks/April/19-4-24/GFG.java new file mode 100644 index 0000000..4c7974e --- /dev/null +++ b/GeeksForGeeks/April/19-4-24/GFG.java @@ -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 findMissing(int a[], int b[], int n, int m) + { + Set visited = new HashSet<>(); + for(int val: b) + { + visited.add(val); + } + ArrayList 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 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 diff --git a/GeeksForGeeks/April/19-4-24/README.md b/GeeksForGeeks/April/19-4-24/README.md new file mode 100644 index 0000000..e95b48c --- /dev/null +++ b/GeeksForGeeks/April/19-4-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n+m) +Space complexity - O(n) diff --git a/LeetCode/April/19-4-24/README.md b/LeetCode/April/19-4-24/README.md new file mode 100644 index 0000000..8faf6d9 --- /dev/null +++ b/LeetCode/April/19-4-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n*m) +Space complexity - O(n*m) diff --git a/LeetCode/April/19-4-24/Solution.java b/LeetCode/April/19-4-24/Solution.java new file mode 100644 index 0000000..e13441c --- /dev/null +++ b/LeetCode/April/19-4-24/Solution.java @@ -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); + } +}