-
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
5717a9e
commit 8fb611a
Showing
4 changed files
with
165 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,45 @@ | ||
long long int res = 0; | ||
|
||
void merge(long long *arr, long long l, long long m, long long r) { | ||
long long n1 = m - l + 1; | ||
long long n2 = r - m; | ||
|
||
long long left[n1], right[n2]; | ||
|
||
for(long long i = 0; i < n1; i++) { | ||
left[i] = arr[l + i]; | ||
} | ||
for(long long i = 0; i < n2; i++) { | ||
right[i] = arr[m + i + 1]; | ||
} | ||
|
||
long long i = 0, j = 0, k = l; | ||
while(i < n1 and j < n2) { | ||
if(left[i] <= right[j]) { | ||
arr[k++] = left[i++]; | ||
} else { | ||
arr[k++] = right[j++]; | ||
res += (n1 - i); | ||
} | ||
} | ||
while(i < n1) { | ||
arr[k++] = left[i++]; | ||
} | ||
while(j < n2) { | ||
arr[k++] = right[j++]; | ||
} | ||
} | ||
|
||
void merge_sort(long long *arr, long long l, long long r) { | ||
if(l >= r) return; | ||
long long mid = (l + r) / 2; | ||
merge_sort(arr, l, mid); | ||
merge_sort(arr, mid + 1, r); | ||
merge(arr, l, mid, r); | ||
} | ||
|
||
long long getInversions(long long *arr, int n){ | ||
// Write your code here. | ||
merge_sort(arr, 0, n - 1); | ||
return res; | ||
} |
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,13 @@ | ||
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { | ||
int i = m - 1, j = n - 1, k = m + n - 1; | ||
while(i >= 0 and j >= 0) { | ||
if(nums1[i] < nums2[j]) { | ||
nums1[k--] = nums2[j--]; | ||
} else { | ||
nums1[k--] = nums1[i--]; | ||
} | ||
} | ||
while(j >= 0) { | ||
nums1[k--] = nums2[j--]; | ||
} | ||
} |
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,84 @@ | ||
# Day2 | ||
|
||
## Rotate Matrix | ||
|
||
Link: [https://leetcode.com/problems/rotate-image/] | ||
|
||
- Find the transpose of the matrix first. Can be done with following: | ||
``` | ||
for(i = 0; i < n; i++) { | ||
for(j = 0; j < i; j++) { | ||
swap(matrix[i][j], matrix[j][i]) | ||
} | ||
} | ||
``` | ||
- Reverse each row of the matrix and the answer is obtained. | ||
|
||
Time Complexity: O(n) | ||
|
||
## Merge OverLapping Intervals | ||
|
||
Link: [https://leetcode.com/problems/merge-intervals/] | ||
|
||
- Sort the intervals on the basis of the starting time. | ||
- If the answer vector is empty, push the entire interval in the answer vector. | ||
- If the current interval's ending time is greater than the answer vector's last interval's ending time, then push the entire inerval in the vector. | ||
- Else, check for the maximum ending time between the current interval and the last interval in the answer vector. | ||
|
||
Time Complexity: O(nlogn) | ||
|
||
## Find the Duplicate Number | ||
|
||
Link: [https://leetcode.com/problems/find-the-duplicate-number] | ||
|
||
_Similar to the Find the starting point of cycle in Linked List_ | ||
|
||
- Initialize slow and fast as the first element of the array. | ||
- Traverse the array with the two pointers, fast travels 2x of the slow. | ||
``` | ||
slow = nums[slow] | ||
fast = nums[nums[fast]] | ||
``` | ||
- Run the loop till they are similar, that is, fast == slow. | ||
- Re-initialize slow as the first element. | ||
- Re-traverse the array with both slow and fast until they are equal. | ||
- Slow and fast travel at the same speed. | ||
|
||
## Merge Sorted Array | ||
|
||
Link: [https://leetcode.com/problems/merge-sorted-array/] | ||
|
||
- Initialize two pointers i and j pointing to the end of the array and a size k = m + n - 1. | ||
- While i and j are greater than zero, check if the element at i is smaller than the element at j and swap the elements at k and j. | ||
- Else, swap the elements at the position k and i | ||
- While j is greater than zero, swap all the remaining elements with the position k. | ||
|
||
Time Complexity: O(n + m) | ||
|
||
## Repeat and Missing Number Array | ||
|
||
Link: [https://www.interviewbit.com/problems/repeat-and-missing-number-array/] | ||
|
||
- Use two mathematical equations of sum of numbers from 1 to n and sum of sqaure of numbers from 1 to n. | ||
- To find the missing number, use the equation: | ||
``` | ||
missing = (sum + square_sum/sum) / 2 | ||
``` | ||
- To find the repeating number, use the equation: | ||
``` | ||
repeating = sum - missing | ||
``` | ||
|
||
Time Complexity: O(n) | ||
|
||
Link: [https://www.codingninjas.com/codestudio/problems/count-inversions_615] | ||
|
||
- Use merge sort to find the number of inversions. | ||
- Write a logic that counts the number of inversions when the arrays, namely, left sub-array and right sub-array are merged. The idea is to have two indices or pointers. One of the pointers will refer to the left sub-array and the other one will point to the right sub-array. Let’s call them ‘LEFTINDEX’ and ‘RIGHTINDEX’ such that: | ||
|
||
- ‘LEFTINDEX' < ‘RIGHTINDEX’ and | ||
- 'LEFTSUBARRAY[LEFTINDEX]' > 'RIGHTSUBARRAY[RIGHTINDEX]' | ||
- We can deduce major information from this configuration. We can say, there would be ('MID' - ‘LEFTINDEX’) inversions, where ‘MID’ is the index from where the array has been split into two. (We can say so because all the remaining elements in the left-subarray ('LEFTSUBARRAY[LEFTINDEX+ 1]', ‘LEFTSUBARRAY[LEFTINDEX+ 2]’ ….. ‘LEFTSUBARRAY[MID]’) will be greater than ‘RIGHTSUBARRAY[RIGHTINDEX]’) | ||
- Follow the above approach to find the number of inversions. | ||
|
||
Time Complexity: O(nlogn) |
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,23 @@ | ||
vector<int> Solution::repeatedNumber(const vector<int> &A) { | ||
long long int len = A.size(); | ||
|
||
long long int S = (len * (len+1) ) /2; | ||
long long int P = (len * (len +1) *(2*len +1) )/6; | ||
long long int missingNumber=0, repeating=0; | ||
|
||
for(int i=0;i<A.size(); i++){ | ||
S -= (long long int)A[i]; | ||
P -= (long long int)A[i]*(long long int)A[i]; | ||
} | ||
|
||
missingNumber = (S + P/S)/2; | ||
|
||
repeating = missingNumber - S; | ||
|
||
vector <int> ans; | ||
|
||
ans.push_back(repeating); | ||
ans.push_back(missingNumber); | ||
|
||
return ans; | ||
} |