Skip to content

Commit 953ffa6

Browse files
Merge branch 'CodeHarborHub:main' into lcsol-3024
2 parents ec30bab + ef5aae5 commit 953ffa6

File tree

12 files changed

+1315
-3
lines changed

12 files changed

+1315
-3
lines changed

assets/Avl-Tree.png

26.7 KB
Loading

dsa-problems/leetcode-problems/0600-0699.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ export const problems = [
573573
"problemName": "693. Binary Number with Alternating Bits",
574574
"difficulty": "Easy",
575575
"leetCodeLink": "https://leetcode.com/problems/binary-number-with-alternating-bits",
576-
"solutionLink": "#"
576+
"solutionLink": "/dsa-solutions/lc-solutions/0600-0699/binary-number-with-alternating-bits"
577577
},
578578
{
579579
"problemName": "694. Number of Distinct Islands",
@@ -591,13 +591,13 @@ export const problems = [
591591
"problemName": "696. Count Binary Substrings",
592592
"difficulty": "Easy",
593593
"leetCodeLink": "https://leetcode.com/problems/count-binary-substrings",
594-
"solutionLink": "#"
594+
"solutionLink": "/dsa-solutions/lc-solutions/0600-0699/count-binary-substrings"
595595
},
596596
{
597597
"problemName": "697. Degree of an Array",
598598
"difficulty": "Easy",
599599
"leetCodeLink": "https://leetcode.com/problems/degree-of-an-array",
600-
"solutionLink": "#"
600+
"solutionLink": "/dsa-solutions/lc-solutions/0600-0699/degree-of-an-array"
601601
},
602602
{
603603
"problemName": "698. Partition to K Equal Sum Subsets",
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
---
2+
id: avl-tree-search
3+
title: AVL Tree Search (Geeks for Geeks)
4+
sidebar_label: AVL Tree Search
5+
tags:
6+
- Beginner
7+
- Search Algorithms
8+
- Geeks for Geeks
9+
- CPP
10+
- Python
11+
- Java
12+
- JavaScript
13+
- DSA
14+
description: "This is a solution to the AVL Tree Search problem on Geeks for Geeks."
15+
---
16+
17+
## What is AVL Tree Search?
18+
19+
AVL Tree Search is a search operation performed on an AVL tree, a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by no more than one, ensuring O(log N) time complexity for search operations.
20+
21+
## Algorithm for AVL Tree Search
22+
23+
1. Start at the root node of the AVL tree.
24+
2. Compare the target value with the value of the current node:
25+
- If the target value equals the current node's value, return the node.
26+
- If the target value is less than the current node's value, move to the left child.
27+
- If the target value is greater than the current node's value, move to the right child.
28+
3. Repeat step 2 until the target value is found or the current node becomes null.
29+
4. If the target value is not found, return null.
30+
31+
## How does AVL Tree Search work?
32+
33+
- It begins by comparing the target value to the value of the root node.
34+
- If the target value matches the root node's value, the search is complete.
35+
- If the target value is less than the root node's value, the search continues in the left subtree.
36+
- If the target value is greater than the root node's value, the search continues in the right subtree.
37+
- This process continues until the target value is found or a leaf node is reached without finding the target value.
38+
39+
![Example for AVL Tree Search(GFG)](../../assets/Avl-Tree.png)
40+
41+
## Problem Description
42+
43+
Given an AVL tree and a target element, implement the AVL Tree Search algorithm to find the node containing the target value in the tree. If the element is not present, return null.
44+
45+
## Examples
46+
47+
**Example 1:**
48+
```
49+
Input:
50+
AVL Tree:
51+
9
52+
/ \
53+
5 12
54+
/ \ \
55+
2 7 15
56+
57+
Target: 7
58+
Output: Node with value 7
59+
60+
```
61+
62+
## Your Task:
63+
64+
You don't need to read input or print anything. Complete the function avlTreeSearch() which takes the root of the AVL tree and a target value as input parameters and returns the node containing the target value. If the target value is not present in the tree, return null.
65+
66+
Expected Time Complexity: $O(LogN)$
67+
Expected Auxiliary Space: $O(1)$
68+
69+
## Constraints
70+
71+
- $1 <= Number of nodes <= 10^5$
72+
- $1 <= Node value <= 10^6$
73+
- $1 <= Target value <= 10^6$
74+
75+
## Implementation
76+
77+
<Tabs>
78+
79+
<TabItem value="C++" label="C++">
80+
<SolutionAuthor name="@ngmuraqrdd"/>
81+
```cpp
82+
#include <iostream>
83+
84+
struct AVLNode {
85+
int value;
86+
AVLNode* left;
87+
AVLNode* right;
88+
AVLNode(int val) : value(val), left(nullptr), right(nullptr) {}
89+
};
90+
91+
AVLNode* avlTreeSearch(AVLNode* root, int target) {
92+
AVLNode* current = root;
93+
while (current) {
94+
if (current->value == target) {
95+
return current;
96+
} else if (current->value < target) {
97+
current = current->right;
98+
} else {
99+
current = current->left;
100+
}
101+
}
102+
return nullptr;
103+
}
104+
105+
int main() {
106+
AVLNode* root = new AVLNode(9);
107+
root->left = new AVLNode(5);
108+
root->right = new AVLNode(12);
109+
root->left->left = new AVLNode(2);
110+
root->left->right = new AVLNode(7);
111+
root->right->right = new AVLNode(15);
112+
113+
int target = 7;
114+
AVLNode* result = avlTreeSearch(root, target);
115+
if (result) {
116+
std::cout << "Node with value " << result->value << " found." << std::endl;
117+
} else {
118+
std::cout << "Node not found." << std::endl;
119+
}
120+
121+
return 0;
122+
}
123+
124+
</TabItem>
125+
126+
<TabItem value="Java" label="Java">
127+
<SolutionAuthor name="@ngmuraqrdd"/>
128+
```java
129+
class AVLNode {
130+
int value;
131+
AVLNode left, right;
132+
AVLNode(int value) {
133+
this.value = value;
134+
left = right = null;
135+
}
136+
}
137+
138+
public class AVLTreeSearch {
139+
public static AVLNode avlTreeSearch(AVLNode root, int target) {
140+
AVLNode current = root;
141+
while (current != null) {
142+
if (current.value == target) {
143+
return current;
144+
} else if (current.value < target) {
145+
current = current.right;
146+
} else {
147+
current = current.left;
148+
}
149+
}
150+
return null;
151+
}
152+
153+
public static void main(String[] args) {
154+
AVLNode root = new AVLNode(9);
155+
root.left = new AVLNode(5);
156+
root.right = new AVLNode(12);
157+
root.left.left = new AVLNode(2);
158+
root.left.right = new AVLNode(7);
159+
root.right.right = new AVLNode(15);
160+
161+
int target = 7;
162+
AVLNode result = avlTreeSearch(root, target);
163+
if (result != null) {
164+
System.out.println("Node with value " + result.value + " found.");
165+
} else {
166+
System.out.println("Node not found.");
167+
}
168+
}
169+
}
170+
171+
```
172+
</TabItem>
173+
</Tabs>
174+
175+
## Complexity Analysis
176+
177+
- **Time Complexity**:$O(log n)$, where $n$ is the number of nodes in the AVL tree. The height of the tree is kept balanced, leading to logarithmic time complexity.
178+
- **Space Complexity**: $O(1)$, as no extra space is required apart from the input tree.
179+
180+
## Advantages and Disadvantages
181+
182+
**Advantages:**
183+
- Ensures balanced tree structure for efficient search, insert, and delete operations.
184+
- Fast search time due to logarithmic time complexity.
185+
186+
**Disadvantages:**
187+
- Requires additional rotations to maintain balance during insert and delete operations.
188+
- More complex to implement compared to simple binary search trees.
189+
190+
191+
## References
192+
193+
- **GFG Problem:** [GFG Problem](https://www.geeksforgeeks.org/practice-questions-height-balancedavl-tree//)
194+
- **Author's Geeks for Geeks Profile:** [MuraliDharan](https://www.geeksforgeeks.org/user/ngmuraqrdd/)
195+
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
---
2+
id: arithmetic-slices
3+
title: Arithmetic Slices (LeetCode)
4+
sidebar_label: 0413-Arithmetic Slices
5+
tags:
6+
- Array
7+
- Dynamic Programming
8+
description: Given an integer array nums, return the number of arithmetic subarrays of nums.
9+
sidebar_position: 0413
10+
---
11+
12+
## Problem Description
13+
14+
An integer array is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.
15+
- For example, `[1,3,5,7,9]`, `[7,7,7,7]`, and `[3,-1,-5,-9]` are arithmetic sequences.
16+
17+
Given an integer array nums, return the number of arithmetic subarrays of nums.
18+
19+
A subarray is a contiguous subsequence of the array.
20+
21+
### Example 1
22+
23+
- **Input:** `nums = [1,2,3,4]`
24+
- **Output:** `3`
25+
- **Explanation:** We have 3 arithmetic slices in nums: [1, 2, 3], [2, 3, 4] and [1,2,3,4] itself.
26+
27+
### Example 2
28+
29+
- **Input:** ` nums = [1]`
30+
- **Output:** `0`
31+
32+
### Constraints
33+
34+
- `1 <= nums.length <= 5000`
35+
- `-1000 <= nums[i] <= 1000`
36+
37+
## Approach
38+
- 1. Initialize Variables: Before looping through the nums, we initialize the ans variable to 0. This variable will accumulate the total count of arithmetic subarrays. We also initialize a cnt variable to 0; it keeps track of consecutive pairs with the same difference. The d variable, which holds the current common difference between pairs, is set to a number outside the valid range (3000) to handle the edge case at the beginning of the array.
39+
40+
- 2. Loop through pairwise(nums): We iterate over each pair (a, b) in pairwise(nums). Here, a and b represent consecutive elements in nums.
41+
42+
- 3. Check the Difference and Update Counter: We compare the difference b - a with the current d. If they're equal, increment cnt by 1, because this extends the current arithmetic subarray sequence by one element. If they're different, update d to the new difference b - a and reset cnt to 0, because we're starting to count a new set of arithmetic subarrays.
43+
44+
- 4. Update the Answer: Add the current cnt to ans in each iteration. By adding cnt, we're accounting for all the new arithmetic subarrays that end at the current element b. The reason adding cnt works is that for each extension of an arithmetic subarray by one element, we introduce exactly cnt new subarrays where cnt is the count of previous consecutive elements that were part of such subarrays.
45+
46+
- 5. Return Result: After the loop completes, ans represents the total number of arithmetic subarrays in nums, which is then returned as the final answer.
47+
48+
### Solution Code
49+
50+
#### Python
51+
52+
```python
53+
from itertools import pairwise
54+
55+
class Solution:
56+
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
57+
total_slices = 0
58+
current_sequence_length = 0
59+
previous_difference = 3000
60+
for current_num, next_num in pairwise(nums):
61+
current_difference = next_num - current_num
62+
if current_difference == previous_difference:
63+
current_sequence_length += 1
64+
else:
65+
previous_difference = current_difference
66+
current_sequence_length = 0
67+
total_slices += current_sequence_length
68+
return total_slices
69+
```
70+
71+
#### C++
72+
```c++
73+
class Solution {
74+
public:
75+
int numberOfArithmeticSlices(vector<int>& nums) {
76+
int totalSlices = 0;
77+
int currentStreak = 0;
78+
int previousDifference = 3000;
79+
for (int i = 0; i < nums.size() - 1; ++i) {
80+
int currentDifference = nums[i + 1] - nums[i];
81+
if (currentDifference == previousDifference) {
82+
++currentStreak;
83+
} else {
84+
previousDifference = currentDifference;
85+
currentStreak = 0;
86+
}
87+
totalSlices += currentStreak;
88+
}
89+
return totalSlices;
90+
}
91+
};
92+
93+
```
94+
95+
#### Java
96+
```Java
97+
class Solution {
98+
public int numberOfArithmeticSlices(int[] nums) {
99+
int arithmeticSliceCount = 0;
100+
int currentSliceLength = 0;
101+
int difference = 3000;
102+
for (int i = 0; i < nums.length - 1; ++i) {
103+
if (nums[i + 1] - nums[i] == difference) {
104+
++currentSliceLength;
105+
} else {
106+
difference = nums[i + 1] - nums[i];
107+
currentSliceLength = 0;
108+
}
109+
arithmeticSliceCount += currentSliceLength;
110+
}
111+
return arithmeticSliceCount;
112+
}
113+
}
114+
115+
```
116+
117+
118+
119+
120+
#### Conclusion
121+
- Time Complexity
122+
The time complexity is o(n).
123+
124+
- Space Complexity
125+
The space complexity is O(1).

0 commit comments

Comments
 (0)