From 9ca5ee734326b2236a1d88f14bc087c2f0710ac9 Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Sat, 20 Apr 2024 07:27:58 +0530 Subject: [PATCH] Added codes for 20 April --- GeeksForGeeks/April/20-4-24/GFG.java | 74 +++++++++++++++++++++++++++ GeeksForGeeks/April/20-4-24/README.md | 2 + LeetCode/April/20-4-24/README.md | 2 + LeetCode/April/20-4-24/Solution.java | 32 ++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 GeeksForGeeks/April/20-4-24/GFG.java create mode 100644 GeeksForGeeks/April/20-4-24/README.md create mode 100644 LeetCode/April/20-4-24/README.md create mode 100644 LeetCode/April/20-4-24/Solution.java diff --git a/GeeksForGeeks/April/20-4-24/GFG.java b/GeeksForGeeks/April/20-4-24/GFG.java new file mode 100644 index 0000000..95db6af --- /dev/null +++ b/GeeksForGeeks/April/20-4-24/GFG.java @@ -0,0 +1,74 @@ +//{ Driver Code Starts +//Initial Template for Java + + +import java.util.*; +import java.io.*; +import java.lang.*; + +class Main +{ + public static void main(String args[])throws IOException + { + BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); + int t =Integer.parseInt(read.readLine()); + + while(t-- > 0) + { + String st[] = read.readLine().trim().split(" "); + int N = Integer.parseInt(st[0]); + int M = Integer.parseInt(st[1]); + + int arr1[] = new int[N]; + int arr2[] = new int[M]; + + st = read.readLine().trim().split(" "); + for(int i = 0; i < N; i++) + arr1[i] = Integer.parseInt(st[i]); + + st = read.readLine().trim().split(" "); + for(int i = 0; i< M; i++) + arr2[i] = Integer.parseInt(st[i]); + + Solution obj = new Solution(); + ArrayList res = new ArrayList(); + res = obj.findUnion(arr1, arr2, N, M); + for(int i = 0;i findUnion(int arr1[], int arr2[], int n, int m) + { + // add your code here + ArrayList ans= new ArrayList(); + + Set set= new HashSet<>(); + + for(int a:arr1) + set.add(a); + + for(int b:arr2) + set.add(b); + + for(int i:set) + ans.add(i); + + Collections.sort(ans); + return ans; + } +} diff --git a/GeeksForGeeks/April/20-4-24/README.md b/GeeksForGeeks/April/20-4-24/README.md new file mode 100644 index 0000000..157d70d --- /dev/null +++ b/GeeksForGeeks/April/20-4-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n+m) +Space complexity - O(n+m) diff --git a/LeetCode/April/20-4-24/README.md b/LeetCode/April/20-4-24/README.md new file mode 100644 index 0000000..8faf6d9 --- /dev/null +++ b/LeetCode/April/20-4-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n*m) +Space complexity - O(n*m) diff --git a/LeetCode/April/20-4-24/Solution.java b/LeetCode/April/20-4-24/Solution.java new file mode 100644 index 0000000..f4f2323 --- /dev/null +++ b/LeetCode/April/20-4-24/Solution.java @@ -0,0 +1,32 @@ +class Solution +{ + public int[][] findFarmland(int[][] land) + { + List ans = new ArrayList<>(); + + for (int i = 0; i < land.length; ++i) + for (int j = 0; j < land[0].length; ++j) + if (land[i][j] == 1) + { + int[] cell = new int[] {i, j}; + dfs(land, i, j, cell); + ans.add(new int[] {i, j, cell[0], cell[1]}); + } + + return ans.stream().toArray(int[][] ::new); + } + + private void dfs(int[][] land, int i, int j, int[] cell) + { + if (i < 0 || i == land.length || j < 0 || j == land[0].length) + return; + if (land[i][j] != 1) + return; + + land[i][j] = 2; + cell[0] = Math.max(cell[0], i); + cell[1] = Math.max(cell[1], j); + dfs(land, i + 1, j, cell); + dfs(land, i, j + 1, cell); + } +}