From 8fb611a1a82e57425680daf9b2404d40d5bec385 Mon Sep 17 00:00:00 2001 From: Mudit Mahajan Date: Thu, 28 Apr 2022 20:11:12 +0000 Subject: [PATCH] Day2 complete --- Day02/Inversions of Array.cpp | 45 ++++++++++++++++ Day02/Merge Sorted Array.cpp | 13 +++++ Day02/README.md | 84 +++++++++++++++++++++++++++++ Day02/Repeat and Missing Number.cpp | 23 ++++++++ 4 files changed, 165 insertions(+) create mode 100644 Day02/Inversions of Array.cpp create mode 100644 Day02/Merge Sorted Array.cpp create mode 100644 Day02/README.md create mode 100644 Day02/Repeat and Missing Number.cpp diff --git a/Day02/Inversions of Array.cpp b/Day02/Inversions of Array.cpp new file mode 100644 index 0000000..285ecd0 --- /dev/null +++ b/Day02/Inversions of Array.cpp @@ -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; +} \ No newline at end of file diff --git a/Day02/Merge Sorted Array.cpp b/Day02/Merge Sorted Array.cpp new file mode 100644 index 0000000..9eb7277 --- /dev/null +++ b/Day02/Merge Sorted Array.cpp @@ -0,0 +1,13 @@ +void merge(vector& nums1, int m, vector& 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--]; + } + } \ No newline at end of file diff --git a/Day02/README.md b/Day02/README.md new file mode 100644 index 0000000..ff88bd5 --- /dev/null +++ b/Day02/README.md @@ -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) diff --git a/Day02/Repeat and Missing Number.cpp b/Day02/Repeat and Missing Number.cpp new file mode 100644 index 0000000..8dbc9a6 --- /dev/null +++ b/Day02/Repeat and Missing Number.cpp @@ -0,0 +1,23 @@ +vector Solution::repeatedNumber(const vector &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 ans; + + ans.push_back(repeating); + ans.push_back(missingNumber); + + return ans; +}