π Types of Errors in a Program: π₯β‘οΈπ
ππ @AceCoding Presents! ππ
π₯ Compilation Error: (Cannot compile, cannot run)
Occurs when the code has syntax issues, preventing it from being compiled. Common examples include missing semicolons, mismatched parentheses, or undeclared variables.
Example: int x = ;
β‘οΈ Runtime Error: (Compiles but cannot run, compilation comes before code execution or running)
Happens while the program is running. It usually occurs due to illegal operations like division by zero, accessing invalid memory, or infinite loops.
Example: int x = 5 / 0;
π Logical Error: (Code compiles and runs but won't result a correct output)
Occurs when the code runs without crashing, but it produces incorrect results due to flawed logic or assumptions. It can be tricky to spot as the program compiles and runs fine.
Example: int a = 5; int b = 2; cout << a - b; (Expecting multiplication but using subtraction)
ππ @AceCoding Presents! ππ
π Simple Sorting Algorithms implementation in in C++
1. Insertion Sort
2. Bubble Sort
β Optimized version of bubble sort, which will give a TC of O(n) best case (array is already sorted)
3. Selection Sort
βοΈ Which Sorting Algorithm is Best?
- Insertion Sort is efficient for small or nearly sorted datasets.
- Bubble Sort is simple but slow for large datasets (O(nΒ²) time complexity).
- Selection Sort is also O(nΒ²) but has a fixed number of swaps.
ππ @AceCoding Presents! ππ
1. Insertion Sort
void insertion_sort(int arr[], int size) {
int temp, i, j;
for (i = 0; i < size; i++) {
temp = arr[i];
j = i;
while (j > 0 && arr[j-1] > temp) {
arr[j] = arr[j - 1];
j--;
}
arr[j] = temp;
}
}2. Bubble Sort
void bubble_sort(int arr[], int size) {
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
if (arr[i] > arr[j]) {
swap(arr[i], arr[j]);
}
}
}
return;
}β Optimized version of bubble sort, which will give a TC of O(n) best case (array is already sorted)
void bubbleSortOptimized(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
bool swapped = false;
for (int j = 0; j < size - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
swapped = true;
}
}
if (!swapped) break;
}
}3. Selection Sort
void selection_sort(int arr[], int size) {
int min_index;
for (int i = 0; i < size; i++) {
min_index = i;
for (int j = i + 1; j < size; j++) {
if (arr[j] < arr[min_index]) {
min_index = j;
}
}
swap(arr[i], arr[min_index;
}
}βοΈ Which Sorting Algorithm is Best?
- Insertion Sort is efficient for small or nearly sorted datasets.
- Bubble Sort is simple but slow for large datasets (O(nΒ²) time complexity).
- Selection Sort is also O(nΒ²) but has a fixed number of swaps.
ππ @AceCoding Presents! ππ
Here you can see the trade-offs of different sorting algorithms π
ππ @AceCoding Presents! ππ
ππ @AceCoding Presents! ππ
Data Structures Using C++.pdf
5.3 MB
Data Structures Using C++
Data Structure Worksheet.docx
20.4 KB
Data Structure Worksheet
β
Optimized version of bubble sort, which will give a TC of O(n) best case (array is already sorted)
void bubbleSortOptimized(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
bool swapped = false;
for (int j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
swapped = true;
}
}
if (!swapped) break;
}
}β When do we encounter a Time Complexity of O(n * log n)? To be more explicit O(n * log2 n) the base is 2 not 10.
Whenever we repeatedly divide the problem by 2 or multiply the input by 2 (as seen in algorithms like merge sort or heap sort), we often deal with quasi-linear time complexity, which is O(n * log n). This pattern is common in efficient sorting and searching algorithms, so keep it in mind when breaking down problems!
#DSA #TimeComplexity #CodingTips
@AceCoding
π₯2
DSA MID TEST 2023-ANSWERSHEET.pdf
700.7 KB
ππ DSA AASTU 2023 Mid Exam
With Solution Answers ππ‘
#AASTU #DSA #DSAmidexam #softwaremidexam
ππ @AceCoding Presents! ππ
With Solution Answers ππ‘
#AASTU #DSA #DSAmidexam #softwaremidexam
ππ @AceCoding Presents! ππ
Basic_data_structures_and_time_and_space_complexity.pdf
3.9 MB
From a2sv
ππ @AceCoding Presents! ππ
ππ @AceCoding Presents! ππ
β‘1π1
< Ace Coding /> π
Basic_data_structures_and_time_and_space_complexity.pdf
Quote of the day
βKarsn Lamb
βA year from now you may wish you had
started todayβ
βKarsn Lamb
π3
time_complexity_Analysis_worked_examples.pdf
357.8 KB
ππ Understanding Time Complexity in Depth - Princeton University
#TimeComplexity #DSA #PrincetonUniversity #AceCoding
ππ @AceCoding Presents! ππ
#TimeComplexity #DSA #PrincetonUniversity #AceCoding
ππ @AceCoding Presents! ππ
π₯ Are U up for the Challenge? π₯
Here are some common binary search problems on LeetCode that will test your skills! ππ»
#LeetCode #BinarySearch #CodingChallenge #DSA
ππ @AceCoding Presents! ππ
Here are some common binary search problems on LeetCode that will test your skills! ππ»
#LeetCode #BinarySearch #CodingChallenge #DSA
ππ @AceCoding Presents! ππ
π₯4
β
34. Find First and Last Position of Element in Sorted Array
π’Using regular binary search
π‘Using the concept of lower bound and upper bound
π΄Using the bisect method (import bisect)
πΎC++ implementation using STL (lower_bound and upper_bound)
ππ @AceCoding Presents! ππ
π’Using regular binary search
class Solution:
def searchRange(self, nums: List[int], target: int) -> List[int]:
def first():
l, r = 0, len(nums)-1
first = -1
while l <= r:
mid = (l+r)//2
if nums[mid] == target:
first = mid
r = mid - 1
elif nums[mid] < target:
l = mid + 1
else:
r = mid - 1
return first
def last():
l, r = 0, len(nums)-1
last = -1
while l <= r:
mid = (l+r)//2
if nums[mid] == target:
last = mid
l = mid + 1
elif nums[mid] < target:
l = mid + 1
else:
r = mid - 1
return last
f = first()
if f == - 1: return [-1, -1]
la = last()
return [f, la]
π‘Using the concept of lower bound and upper bound
class Solution:
def searchRange(self, nums: List[int], target: int) -> List[int]:
def lower_bound():
l, r = 0, len(nums)-1
ans = len(nums)
while l <= r:
mid = (l+r)//2
if nums[mid] >= target:
ans = mid
r = mid - 1
else:
l = mid + 1
return ans
def upper_bound():
l, r = 0, len(nums)-1
ans = len(nums)
while l <= r:
mid = (l+r)//2
if nums[mid] > target:
ans = mid
r = mid - 1
else:
l = mid + 1
return ans
lb = lower_bound()
up = upper_bound() - 1
if lb <= up:
return [lb, up]
return [-1, -1]
π΄Using the bisect method (import bisect)
class Solution:
def searchRange(self, nums: List[int], target: int) -> List[int]:
lb = bisect.bisect_left(nums, target)
if lb >= len(nums) or nums[lb] != target:
return [-1, -1]
ub = bisect.bisect_right(nums, target)
return [lb, ub-1]
πΎC++ implementation using STL (lower_bound and upper_bound)
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
auto lb = lower_bound(nums.begin(), nums.end(), target) - nums.begin();
if (lb >= nums.size() || nums[lb] != target)
return {-1, -1};
auto ub = upper_bound(nums.begin(), nums.end(), target) - nums.begin();
return {(int)lb, (int)ub-1};
}
};ππ @AceCoding Presents! ππ
β
74. Search a 2D Matrix
ππ @AceCoding Presents! ππ
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
row = len(matrix)
col = len(matrix[0])
l, r = 0, (row * col) - 1
while l <= r:
mid = (l+r)//2
# i = mid // col
# j = mid % col
i, j = divmod(mid, col) # one liner for the above
if matrix[i][j] == target:
return True
elif target >= matrix[i][j]:
l = mid + 1
else:
r = mid - 1
return False
ππ @AceCoding Presents! ππ
π₯ Hard Medium (1945) is nearly a hard question; binary search
π§ It took me around 35 min but I think I was lucky this time
@AceCoding
https://leetcode.com/problems/minimum-number-of-days-to-make-m-bouquets/
π§ It took me around 35 min but I think I was lucky this time
Time complexity = O(nΓlog(maxDays)) ; Space complexity = O(1)
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
max_days = max(bloomDay)
min_days = -1
L, R = 0, max_days
while L <= R:
mid = (L+R)//2
count = 0
m_count = 0
for bloom in bloomDay:
if mid >= bloom:
count += 1
if count >= k:
m_count += 1
count = 0
else:
count = 0
if m_count >= m:
min_days = mid
R = mid - 1
else:
L = mid + 1
return min_days
@AceCoding
https://leetcode.com/problems/minimum-number-of-days-to-make-m-bouquets/
LeetCode
Minimum Number of Days to Make m Bouquets - LeetCode
Can you solve this real interview question? Minimum Number of Days to Make m Bouquets - You are given an integer array bloomDay, an integer m and an integer k.
You want to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.β¦
You want to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.β¦