-
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
3c0a79f
commit c08108f
Showing
4 changed files
with
91 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,62 @@ | ||
//{ Driver Code Starts | ||
//Initial template for JAVA | ||
|
||
import java.util.*; | ||
import java.lang.*; | ||
import java.io.*; | ||
|
||
class GFG | ||
{ | ||
public static void main (String[] args) throws IOException | ||
{ | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
int t = Integer.parseInt(br.readLine()); | ||
for(int i=0;i<t;i++){ | ||
int n = Integer.parseInt(br.readLine()); | ||
String l = br.readLine(); | ||
String[] sarr = l.split(" "); | ||
int[] arr = new int[sarr.length]; | ||
for(int i1=0;i1<arr.length;i1++){ | ||
arr[i1] = Integer.parseInt(sarr[i1]); | ||
} | ||
|
||
Solution obj = new Solution(); | ||
|
||
int[] res = obj.twoRepeated(arr, n); | ||
System.out.println(res[0] + " " + res[1]); | ||
} | ||
} | ||
} | ||
// } Driver Code Ends | ||
|
||
|
||
//User function template for JAVA | ||
|
||
class Solution | ||
{ | ||
//Function to find two repeated elements. | ||
public int[] twoRepeated(int arr[], int n) | ||
{ | ||
// Your code here | ||
Set<Integer> set = new HashSet<>(); | ||
|
||
int[] ans = new int[2]; | ||
int j=0; | ||
for(int i=0;i<n+2;i++) | ||
{ | ||
if(set.contains(arr[i]) && j!=2) | ||
{ | ||
ans[j]= arr[i]; | ||
j++; | ||
|
||
} | ||
|
||
else | ||
{ | ||
set.add(arr[i]); | ||
} | ||
} | ||
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) | ||
Space complexity - O(1) |
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(m*n) | ||
Space complexity - O(1) |
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,25 @@ | ||
class Solution | ||
{ | ||
public int islandPerimeter(int[][] grid) | ||
{ | ||
int islands = 0; | ||
int neighbors = 0; | ||
|
||
for (int i = 0; i < grid.length; ++i) | ||
{ | ||
for (int j = 0; j < grid[0].length; ++j) | ||
{ | ||
if (grid[i][j] == 1) | ||
{ | ||
++islands; | ||
if (i - 1 >= 0 && grid[i - 1][j] == 1) | ||
++neighbors; | ||
if (j - 1 >= 0 && grid[i][j - 1] == 1) | ||
++neighbors; | ||
} | ||
} | ||
} | ||
|
||
return islands * 4 - neighbors * 2; | ||
} | ||
} |