Skip to content

Commit

Permalink
Added codes for 20 April
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Apr 20, 2024
1 parent 3d4c616 commit 9ca5ee7
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
74 changes: 74 additions & 0 deletions GeeksForGeeks/April/20-4-24/GFG.java
Original file line number Diff line number Diff line change
@@ -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<Integer> res = new ArrayList<Integer>();
res = obj.findUnion(arr1, arr2, N, M);
for(int i = 0;i<res.size();i++)
System.out.print(res.get(i) + " ");
System.out.println();
}
}
}



// } Driver Code Ends


//User function Template for Java

//arr1,arr2 : the arrays
// n, m: size of arrays
class Solution
{
//Function to return a list containing the union of the two arrays.
public static ArrayList<Integer> findUnion(int arr1[], int arr2[], int n, int m)
{
// add your code here
ArrayList<Integer> ans= new ArrayList<Integer>();

Set<Integer> 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;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/April/20-4-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(n+m)
2 changes: 2 additions & 0 deletions LeetCode/April/20-4-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(n*m)
32 changes: 32 additions & 0 deletions LeetCode/April/20-4-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution
{
public int[][] findFarmland(int[][] land)
{
List<int[]> 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);
}
}

0 comments on commit 9ca5ee7

Please sign in to comment.