diff --git a/dsa-solutions/lc-solutions/0400-0499/0493-reverse-pairs.md b/dsa-solutions/lc-solutions/0400-0499/0493-reverse-pairs.md index ec1d771d6..259a6e4c1 100644 --- a/dsa-solutions/lc-solutions/0400-0499/0493-reverse-pairs.md +++ b/dsa-solutions/lc-solutions/0400-0499/0493-reverse-pairs.md @@ -1,323 +1,323 @@ ---- -id: reverse-pairs -title: Reverse Pairs -sidebar_label: 493-Reverse-Pairs -tags: - - Array - - Binary Search - - Divide and conquer - - Binary Indexed Tree - - Segment Tree - - Merge Sort - - Ordered Set -description: The problem is to reverse the pairs. -sidebar_position: 2667 ---- - -## Problem Statement - -### Problem Description - -Given an integer array `nums`, return the number of reverse pairs in the array. - -A reverse pair is a pair `(i, j)` where: - -`0 <= i < j < nums.length` and -`nums[i] > 2 * nums[j]`. - -### Examples - -**Example 1:** - -``` -Input: nums = [1,3,2,3,1] -Output: 2 -Explanation: The reverse pairs are: -(1, 4) --> nums[1] = 3, nums[4] = 1, 3 > 2 * 1 -(3, 4) --> nums[3] = 3, nums[4] = 1, 3 > 2 * 1 -``` - -**Example 2:** - -``` -Input: nums = [2,4,3,5,1] -Output: 3 -Explanation: The reverse pairs are: -(1, 4) --> nums[1] = 4, nums[4] = 1, 4 > 2 * 1 -(2, 4) --> nums[2] = 3, nums[4] = 1, 3 > 2 * 1 -(3, 4) --> nums[3] = 5, nums[4] = 1, 5 > 2 * 1 -``` - -### Constraints - -- `1 <= nums.length <= 5 * 10^4` - -## Solution of Given Problem - -### Intuition and Approach - -The intuition behind this solution is to use a modified Merge Sort algorithm to count the number of reverse pairs in the array. - - -### Approaches - -- Divide the array into smaller subarrays until each subarray has only one element. -- Merge the subarrays back together, counting the number of reverse pairs between each pair of subarrays. -- The merge step is done in a way that ensures the count of reverse pairs is accurate. - - -#### Codes in Different Languages - - - - - ```javascript - function mergeSortedArrays(nums, left, mid, right, temp) { - let i = left, j = mid + 1, k = 0; - while (i <= mid && j <= right) { - if (nums[i] <= nums[j]) temp[k++] = nums[i++]; - else temp[k++] = nums[j++]; - } - while (i <= mid) temp[k++] = nums[i++]; - while (j <= right) temp[k++] = nums[j++]; -} - -function merge(nums, left, mid, right) { - let count = 0; - let j = mid + 1; - for (let i = left; i <= mid; i++) { - while (j <= right && nums[i] > 2 * nums[j]) j++; - count += j - mid - 1; - } - let temp = new Array(right - left + 1); - mergeSortedArrays(nums, left, mid, right, temp); - for (let i = left; i <= right; i++) nums[i] = temp[i - left]; - return count; -} -function mergeSort(nums, left, right) { - if (left >= right) return 0; - let mid = left + (right - left) / 2; - let count = mergeSort(nums, left, mid) + mergeSort(nums, mid + 1, right); - count += merge(nums, left, mid, right); - return count; -} - -class Solution { - reversePairs(nums) { - return mergeSort(nums, 0, nums.length - 1); - } -} - - - ``` - - - - - ```typescript - function mergeSortedArrays(nums: number[], left: number, mid: number, right: number, temp: number[]) { - let i = left, j = mid + 1, k = 0; - while (i <= mid && j <= right) { - if (nums[i] <= nums[j]) temp[k++] = nums[i++]; - else temp[k++] = nums[j++]; - } - while (i <= mid) temp[k++] = nums[i++]; - while (j <= right) temp[k++] = nums[j++]; -} - -function merge(nums: number[], left: number, mid: number, right: number) { - let count = 0; - let j = mid + 1; - for (let i = left; i <= mid; i++) { - while (j <= right && nums[i] > 2 * nums[j]) j++; - count += j - mid - 1; - } - let temp = new Array(right - left + 1); - mergeSortedArrays(nums, left, mid, right, temp); - for (let i = left; i <= right; i++) nums[i] = temp[i - left]; - return count; -} -function mergeSort(nums: number[], left: number, right: number) { - if (left >= right) return 0; - let mid = left + (right - left) / 2; - let count = mergeSort(nums, left, mid) + mergeSort(nums, mid + 1, right); - count += merge(nums, left, mid, right); - return count; -} - -class Solution { - reversePairs(nums: number[]) { - return mergeSort(nums, 0, nums.length - 1); - } -} - - ``` - - - - - ```python - def merge_sorted_arrays(nums, left, mid, right, temp): - i, j, k = left, mid + 1, 0 - while i <= mid and j <= right: - if nums[i] <= nums[j]: - temp[k] = nums[i] - i += 1 - else: - temp[k] = nums[j] - j += 1 - k += 1 - while i <= mid: - temp[k] = nums[i] - i += 1 - k += 1 - while j <= right: - temp[k] = nums[j] - j += 1 - k += 1 - -def merge(nums, left, mid, right): - count = 0 - j = mid + 1 - for i in range(left, mid + 1): - while j <= right and nums[i] > 2 * nums[j]: - j += 1 - count += j - mid - 1 - temp = [0] * (right - left + 1) - merge_sorted_arrays(nums, left, mid, right, temp) - for i in range(left, right + 1): - nums[i] = temp[i - left] - return count - -def merge_sort(nums, left, right): - if left >= right: - return 0 - mid = left + (right - left) // 2 - count = merge_sort(nums, left, mid) + merge_sort(nums, mid + 1, right) - count += merge(nums, left, mid, right) - return count - -class Solution: - def reversePairs(self, nums): - return merge_sort(nums, 0, len(nums) - 1) - - ``` - - - - - ```java - public class Solution { - public int reversePairs(int[] nums) { - return mergeSort(nums, 0, nums.length - 1); - } - - public int mergeSort(int[] nums, int left, int right) { - if (left >= right) { - return 0; - } - int mid = left + (right - left) / 2; - int count = mergeSort(nums, left, mid) + mergeSort(nums, mid + 1, right); - count += merge(nums, left, mid, right); - return count; - } - public int merge(int[] nums, int left, int mid, int right) { - int count = 0; - int j = mid + 1; - for (int i = left; i <= mid; i++) { - while (j <= right && nums[i] > 2 * nums[j]) { - j++; - } - count += j - mid - 1; - } - int[] temp = new int[right - left + 1]; - mergeSortedArrays(nums, left, mid, right, temp); - for (int i = left; i <= right; i++) { - nums[i] = temp[i - left]; - } - return count; - } - - public void - mergeSortedArrays(int[] nums, int left, int mid, int right, int[] temp) { - int i = left, j = mid + 1, k = 0; - while (i <= mid && j <= right) { - if (nums[i] <= nums[j]) { - temp[k++] = nums[i++]; - } else { - temp[k++] = nums[j++]; - } - } - while (i <= mid) { - temp[k++] = nums[i++]; - } - while (j <= right) { - temp[k++] = nums[j++]; - } - } -} - - ``` - - - - ```cpp - void mergeSortedArrays(vector& nums, int left, int mid, int right, vector& temp) { - int i = left, j = mid + 1, k = 0; - while (i <= mid && j <= right) { - if (nums[i] <= nums[j]) temp[k++] = nums[i++]; - else temp[k++] = nums[j++]; - } - while (i <= mid) temp[k++] = nums[i++]; - while (j <= right) temp[k++] = nums[j++]; -} -int merge(vector& nums, int left, int mid, int right) { - int count = 0; - int j = mid + 1; - for (int i = left; i <= mid; i++) { - while (j <= right && nums[i] > 2 * nums[j]) j++; - count += j - mid - 1; - } - vector temp(right - left + 1); - mergeSortedArrays(nums, left, mid, right, temp); - for (int i = left; i <= right; i++) nums[i] = temp[i - left]; - return count; -} -int mergeSort(vector& nums, int left, int right) { - if (left >= right) return 0; - int mid = left + (right - left) / 2; - int count = mergeSort(nums, left, mid) + mergeSort(nums, mid + 1, right); - count += merge(nums, left, mid, right); - return count; -} - - -class Solution { -public: - int reversePairs(vector& nums) { - return mergeSort(nums, 0, nums.size() - 1); - - - } -}; - ``` - - - -### Complexity Analysis - -- **Time Complexity**: $$O(n*log(n))$$, where n is the length of the input array. This is because the solution uses a modified Merge Sort algorithm, which has a time complexity of O(n log n). - - -- **Space Complexity**: $$O(n)$$, where n is the length of the input array. This is because the solution uses a temporary array to store the merged sorted arrays. - - - ---- - -

Authors:

- -
-{['Ishitamukherjee2004'].map(username => ( - -))}
+--- +id: reverse-pairs +title: Reverse Pairs +sidebar_label: 493-Reverse-Pairs +tags: + - Array + - Binary Search + - Divide and conquer + - Binary Indexed Tree + - Segment Tree + - Merge Sort + - Ordered Set +description: The problem is to reverse the pairs. +sidebar_position: 2667 +--- + +## Problem Statement + +### Problem Description + +Given an integer array `nums`, return the number of reverse pairs in the array. + +A reverse pair is a pair `(i, j)` where: + +`0 <= i < j < nums.length` and +`nums[i] > 2 * nums[j]`. + +### Examples + +**Example 1:** + +``` +Input: nums = [1,3,2,3,1] +Output: 2 +Explanation: The reverse pairs are: +(1, 4) --> nums[1] = 3, nums[4] = 1, 3 > 2 * 1 +(3, 4) --> nums[3] = 3, nums[4] = 1, 3 > 2 * 1 +``` + +**Example 2:** + +``` +Input: nums = [2,4,3,5,1] +Output: 3 +Explanation: The reverse pairs are: +(1, 4) --> nums[1] = 4, nums[4] = 1, 4 > 2 * 1 +(2, 4) --> nums[2] = 3, nums[4] = 1, 3 > 2 * 1 +(3, 4) --> nums[3] = 5, nums[4] = 1, 5 > 2 * 1 +``` + +### Constraints + +- `1 <= nums.length <= 5 * 10^4` + +## Solution of Given Problem + +### Intuition and Approach + +The intuition behind this solution is to use a modified Merge Sort algorithm to count the number of reverse pairs in the array. + + +### Approaches + +- Divide the array into smaller subarrays until each subarray has only one element. +- Merge the subarrays back together, counting the number of reverse pairs between each pair of subarrays. +- The merge step is done in a way that ensures the count of reverse pairs is accurate. + + +#### Codes in Different Languages + + + + + ```javascript + function mergeSortedArrays(nums, left, mid, right, temp) { + let i = left, j = mid + 1, k = 0; + while (i <= mid && j <= right) { + if (nums[i] <= nums[j]) temp[k++] = nums[i++]; + else temp[k++] = nums[j++]; + } + while (i <= mid) temp[k++] = nums[i++]; + while (j <= right) temp[k++] = nums[j++]; +} + +function merge(nums, left, mid, right) { + let count = 0; + let j = mid + 1; + for (let i = left; i <= mid; i++) { + while (j <= right && nums[i] > 2 * nums[j]) j++; + count += j - mid - 1; + } + let temp = new Array(right - left + 1); + mergeSortedArrays(nums, left, mid, right, temp); + for (let i = left; i <= right; i++) nums[i] = temp[i - left]; + return count; +} +function mergeSort(nums, left, right) { + if (left >= right) return 0; + let mid = left + (right - left) / 2; + let count = mergeSort(nums, left, mid) + mergeSort(nums, mid + 1, right); + count += merge(nums, left, mid, right); + return count; +} + +class Solution { + reversePairs(nums) { + return mergeSort(nums, 0, nums.length - 1); + } +} + + + ``` + + + + + ```typescript + function mergeSortedArrays(nums: number[], left: number, mid: number, right: number, temp: number[]) { + let i = left, j = mid + 1, k = 0; + while (i <= mid && j <= right) { + if (nums[i] <= nums[j]) temp[k++] = nums[i++]; + else temp[k++] = nums[j++]; + } + while (i <= mid) temp[k++] = nums[i++]; + while (j <= right) temp[k++] = nums[j++]; +} + +function merge(nums: number[], left: number, mid: number, right: number) { + let count = 0; + let j = mid + 1; + for (let i = left; i <= mid; i++) { + while (j <= right && nums[i] > 2 * nums[j]) j++; + count += j - mid - 1; + } + let temp = new Array(right - left + 1); + mergeSortedArrays(nums, left, mid, right, temp); + for (let i = left; i <= right; i++) nums[i] = temp[i - left]; + return count; +} +function mergeSort(nums: number[], left: number, right: number) { + if (left >= right) return 0; + let mid = left + (right - left) / 2; + let count = mergeSort(nums, left, mid) + mergeSort(nums, mid + 1, right); + count += merge(nums, left, mid, right); + return count; +} + +class Solution { + reversePairs(nums: number[]) { + return mergeSort(nums, 0, nums.length - 1); + } +} + + ``` + + + + + ```python + def merge_sorted_arrays(nums, left, mid, right, temp): + i, j, k = left, mid + 1, 0 + while i <= mid and j <= right: + if nums[i] <= nums[j]: + temp[k] = nums[i] + i += 1 + else: + temp[k] = nums[j] + j += 1 + k += 1 + while i <= mid: + temp[k] = nums[i] + i += 1 + k += 1 + while j <= right: + temp[k] = nums[j] + j += 1 + k += 1 + +def merge(nums, left, mid, right): + count = 0 + j = mid + 1 + for i in range(left, mid + 1): + while j <= right and nums[i] > 2 * nums[j]: + j += 1 + count += j - mid - 1 + temp = [0] * (right - left + 1) + merge_sorted_arrays(nums, left, mid, right, temp) + for i in range(left, right + 1): + nums[i] = temp[i - left] + return count + +def merge_sort(nums, left, right): + if left >= right: + return 0 + mid = left + (right - left) // 2 + count = merge_sort(nums, left, mid) + merge_sort(nums, mid + 1, right) + count += merge(nums, left, mid, right) + return count + +class Solution: + def reversePairs(self, nums): + return merge_sort(nums, 0, len(nums) - 1) + + ``` + + + + + ```java + public class Solution { + public int reversePairs(int[] nums) { + return mergeSort(nums, 0, nums.length - 1); + } + + public int mergeSort(int[] nums, int left, int right) { + if (left >= right) { + return 0; + } + int mid = left + (right - left) / 2; + int count = mergeSort(nums, left, mid) + mergeSort(nums, mid + 1, right); + count += merge(nums, left, mid, right); + return count; + } + public int merge(int[] nums, int left, int mid, int right) { + int count = 0; + int j = mid + 1; + for (int i = left; i <= mid; i++) { + while (j <= right && nums[i] > 2 * nums[j]) { + j++; + } + count += j - mid - 1; + } + int[] temp = new int[right - left + 1]; + mergeSortedArrays(nums, left, mid, right, temp); + for (int i = left; i <= right; i++) { + nums[i] = temp[i - left]; + } + return count; + } + + public void + mergeSortedArrays(int[] nums, int left, int mid, int right, int[] temp) { + int i = left, j = mid + 1, k = 0; + while (i <= mid && j <= right) { + if (nums[i] <= nums[j]) { + temp[k++] = nums[i++]; + } else { + temp[k++] = nums[j++]; + } + } + while (i <= mid) { + temp[k++] = nums[i++]; + } + while (j <= right) { + temp[k++] = nums[j++]; + } + } +} + + ``` + + + + ```cpp + void mergeSortedArrays(vector& nums, int left, int mid, int right, vector& temp) { + int i = left, j = mid + 1, k = 0; + while (i <= mid && j <= right) { + if (nums[i] <= nums[j]) temp[k++] = nums[i++]; + else temp[k++] = nums[j++]; + } + while (i <= mid) temp[k++] = nums[i++]; + while (j <= right) temp[k++] = nums[j++]; +} +int merge(vector& nums, int left, int mid, int right) { + int count = 0; + int j = mid + 1; + for (int i = left; i <= mid; i++) { + while (j <= right && nums[i] > 2 * nums[j]) j++; + count += j - mid - 1; + } + vector temp(right - left + 1); + mergeSortedArrays(nums, left, mid, right, temp); + for (int i = left; i <= right; i++) nums[i] = temp[i - left]; + return count; +} +int mergeSort(vector& nums, int left, int right) { + if (left >= right) return 0; + int mid = left + (right - left) / 2; + int count = mergeSort(nums, left, mid) + mergeSort(nums, mid + 1, right); + count += merge(nums, left, mid, right); + return count; +} + + +class Solution { +public: + int reversePairs(vector& nums) { + return mergeSort(nums, 0, nums.size() - 1); + + + } +}; + ``` + + + +### Complexity Analysis + +- **Time Complexity**: $$O(n*log(n))$$, where n is the length of the input array. This is because the solution uses a modified Merge Sort algorithm, which has a time complexity of O(n log n). + + +- **Space Complexity**: $$O(n)$$, where n is the length of the input array. This is because the solution uses a temporary array to store the merged sorted arrays. + + + +--- + +

Authors:

+ +
+{['Ishitamukherjee2004'].map(username => ( + +))}
\ No newline at end of file diff --git a/dsa/Advance/binaryTree-mirror-rotation.md b/dsa/Advance/binaryTree-mirror-rotation.md new file mode 100644 index 000000000..76b184853 --- /dev/null +++ b/dsa/Advance/binaryTree-mirror-rotation.md @@ -0,0 +1,216 @@ +--- +id: binary-tree-mirror-rotate +title: Binary Tree Mirror and Rotation Solution +sidebar_label: 0005 - Binary Tree Mirror and Rotation +tags: [Binary Tree, Mirror Image, Tree Rotation, Algorithm, C++, Problem Solving] +description: This is a solution to create the mirror image and perform rotations (left and right) on a binary tree. +--- + +## Problem Statement + +### Problem Description + +Implement a feature to create the mirror image and perform rotations (left and right) on a binary tree. This functionality will allow users to easily transform binary trees for various algorithmic and visualization purposes. + +### Examples + +**Example 1:** + +```plaintext +Input: Binary Tree = [4, 2, 7, 1, 3, 6, 9] + + 4 + / \ + 2 7 + / \ / \ + 1 3 6 9 + +Mirror Image Output: + + 4 + / \ + 7 2 + / \ / \ + 9 6 3 1 + +``` +**Example 2:** + +```plaintext +Input: Binary Tree = [4, 2, 7, 1, 3, 6, 9] +Rotation (Left) Output: + + 2 + / \ + 1 4 + \ + 7 + / \ + 6 9 + +Rotation (Right) Output: + + 7 + / \ + 4 9 + / \ + 2 6 + / \ + 1 3 + + +``` +### Constraints + +- The input is a binary tree. +- The tree can have up to 10^5 nodes. + +## Solution of Given Problem + +### Intuition and Approach + +To solve the problem, we can break it down into three parts: + +1. Mirror Image: +- Swap the left and right children of each node recursively. + +2. Left Rotation: +- Rotate the tree around a specific node such that the node's right child becomes its parent. + +3. Right Rotation: +- Rotate the tree around a specific node such that the node's left child becomes its parent. + +### Approaches + +#### Codes in Different Languages + + + + + ```cpp +#include +using namespace std; + +struct TreeNode { + int val; + TreeNode* left; + TreeNode* right; + TreeNode(int x) : val(x), left(NULL), right(NULL) {} +}; + +void mirror(TreeNode* node) { + if (node == NULL) + return; + + swap(node->left, node->right); + mirror(node->left); + mirror(node->right); +} + +TreeNode* leftRotate(TreeNode* root) { + if (root == NULL || root->right == NULL) + return root; + + TreeNode* newRoot = root->right; + root->right = newRoot->left; + newRoot->left = root; + + return newRoot; +} + +TreeNode* rightRotate(TreeNode* root) { + if (root == NULL || root->left == NULL) + return root; + + TreeNode* newRoot = root->left; + root->left = newRoot->right; + newRoot->right = root; + + return newRoot; +} + +void inorder(TreeNode* root) { + if (root == NULL) + return; + inorder(root->left); + cout << root->val << " "; + inorder(root->right); +} + +int main() { + int n, val; + cout << "Enter the number of nodes in the tree: "; + cin >> n; + + if (n == 0) { + cout << "The tree is empty." << endl; + return 0; + } + + cout << "Enter the values of the nodes: "; + vector nodes(n); + for (int i = 0; i < n; ++i) { + cin >> val; + nodes[i] = new TreeNode(val); + } + + for (int i = 0; i < n; ++i) { + int leftIndex, rightIndex; + cout << "Enter the left and right child indices for node with value " << nodes[i]->val << " (use -1 for no child): "; + cin >> leftIndex >> rightIndex; + if (leftIndex != -1) nodes[i]->left = nodes[leftIndex]; + if (rightIndex != -1) nodes[i]->right = nodes[rightIndex]; + } + + TreeNode* root = nodes[0]; + + cout << "Original Tree: "; + inorder(root); + cout << "\n"; + + mirror(root); + cout << "Mirror Image: "; + inorder(root); + cout << "\n"; + + root = leftRotate(root); + cout << "Left Rotation: "; + inorder(root); + cout << "\n"; + + root = rightRotate(root); + cout << "Right Rotation: "; + inorder(root); + cout << "\n"; + + return 0; +} + ``` + + + +### Complexity Analysis + +- **Time Complexity:** $O(n)$ +- **Space Complexity:** $O(h)$, where `h` is the height of the tree (due to the recursion stack). + +The time complexity is linear because each node is visited once. The space complexity is determined by the depth of the recursion stack. + +## Video Explanation of Given Problem + + +--- + +

Authors:

+ +
+{['sjain1909'].map(username => ( + +))} +