-
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
3b7f93f
commit 174be60
Showing
4 changed files
with
78 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,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; | ||
} | ||
} |
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(logn) | ||
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(n) | ||
Space complexity - O(n) |
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,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; | ||
} | ||
} |