Skip to content

Commit

Permalink
Added codes for 8 June
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Jun 8, 2024
1 parent 3b7f93f commit 174be60
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
47 changes: 47 additions & 0 deletions GeeksForGeeks/June/08-6-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//{ Driver Code Starts
import java.util.*;

class ExtraElement {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int[] a = new int[n];
int[] b = new int[n - 1];
for (int i = 0; i < n; i++) a[i] = sc.nextInt();
for (int i = 0; i < n - 1; i++) b[i] = sc.nextInt();
Solution g = new Solution();
System.out.println(g.findExtra(n, a, b));
}
}
}
// } Driver Code Ends


/*Complete the function below*/
class Solution
{
public int findExtra(int n, int arr1[], int arr2[])
{
// add code here.
int index = n-1;

int left = 0, right = n - 2;
while (left <= right)
{
int mid = (left+right) / 2;

if (arr2[mid] == arr1[mid])
left = mid + 1;

else
{
index = mid;
right = mid - 1;
}
}

return index;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/June/08-6-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(logn)
Space complexity - O(1)
2 changes: 2 additions & 0 deletions LeetCode/June/08-6-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(n)
27 changes: 27 additions & 0 deletions LeetCode/June/08-6-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution
{
public boolean checkSubarraySum(int[] nums, int k)
{
int prefix = 0;
Map<Integer, Integer> prefixToIndex = new HashMap<>();
prefixToIndex.put(0, -1);

for (int i = 0; i < nums.length; ++i)
{
prefix += nums[i];
if (k != 0)
prefix %= k;
if (prefixToIndex.containsKey(prefix))
{
if (i - prefixToIndex.get(prefix) > 1)
return true;
}
else
{
prefixToIndex.put(prefix, i);
}
}

return false;
}
}

0 comments on commit 174be60

Please sign in to comment.