-
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
3d4c616
commit 9ca5ee7
Showing
4 changed files
with
110 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,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; | ||
} | ||
} |
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,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,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); | ||
} | ||
} |