Skip to content

Commit

Permalink
Added codes for 18 April
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Apr 18, 2024
1 parent 3c0a79f commit c08108f
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
62 changes: 62 additions & 0 deletions GeeksForGeeks/April/18-4-24/GFG.java
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;
}

}
2 changes: 2 additions & 0 deletions GeeksForGeeks/April/18-4-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(1)
2 changes: 2 additions & 0 deletions LeetCode/April/18-4-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(m*n)
Space complexity - O(1)
25 changes: 25 additions & 0 deletions LeetCode/April/18-4-24/Solution.java
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;
}
}

0 comments on commit c08108f

Please sign in to comment.