diff --git a/problem-of-the-day/day02/1071 Number Spiral.cpp b/problem-of-the-day/day02/1071 Number Spiral.cpp new file mode 100644 index 0000000..38b8442 --- /dev/null +++ b/problem-of-the-day/day02/1071 Number Spiral.cpp @@ -0,0 +1,36 @@ +#include +using namespace std; + +int32_t main() { + int t; + cin >> t; + for(int i = 0; i < t; i++) { + long long x, y; + cin >> x >> y; + if (y > x) { + long long xx = y * y - y + 1; + long long gap = y - x; + // If y is odd, move to the right (increase) + if (y % 2 == 1) { + cout << xx + gap << '\n'; // Print the calculated position in the sequence + } + // If y is even, move to the left (decrease) + else { + cout << xx - gap << '\n'; // Print the calculated position in the sequence + } + } + else { + long long yy = x * x - x + 1; // Calculate the base value for the diagonal starting at (x, x) + long long gap = x - y; // Calculate the difference between 'x' and 'y' + + // If x is even, move to the right (increase) + if (x % 2 == 0) { + cout << yy + gap << '\n'; // Print the calculated position in the sequence + } + // If x is odd, move to the left (decrease) + else { + cout << yy - gap << '\n'; // Print the calculated position in the sequence + } + } + } + return 0; diff --git a/problem-of-the-day/day02/1071 Number Spiral.py b/problem-of-the-day/day02/1071 Number Spiral.py new file mode 100644 index 0000000..454f2c3 --- /dev/null +++ b/problem-of-the-day/day02/1071 Number Spiral.py @@ -0,0 +1,21 @@ +# Number Spiral +# A number spiral is an infinite grid whose upper-left square has number 1 +# Your task is to find out the number in row y and column x + +test = int(input()) + +for i in range(test): + y, x = map(int, input().split()) + + if y > x: + if y % 2: + ans = (y - 1) ** 2 + x + else: + ans = y ** 2 - x + 1 + else: + if x % 2: + ans = x ** 2 - y + 1 + else: + ans = (x - 1) ** 2 + y + + print(ans) diff --git a/problem-of-the-day/day02/162 Peak Element.cpp b/problem-of-the-day/day02/162 Peak Element.cpp new file mode 100644 index 0000000..88e5a23 --- /dev/null +++ b/problem-of-the-day/day02/162 Peak Element.cpp @@ -0,0 +1,15 @@ + +class Solution { +public: + int findPeakElement(vector& nums) { + int low = 0, high = nums.size() - 1; + while (low < high) { + int mid = low + (high - low) / 2; + if (nums[mid] < nums[mid + 1]) { + low = mid + 1; + } else + high = mid; + } + return low; + } +}; \ No newline at end of file diff --git a/problem-of-the-day/day02/162 Peak Element.py b/problem-of-the-day/day02/162 Peak Element.py new file mode 100644 index 0000000..fffebf6 --- /dev/null +++ b/problem-of-the-day/day02/162 Peak Element.py @@ -0,0 +1,15 @@ +class Solution: + def findPeakElement(self, nums: list[int]) -> int: + left, right = 0, len(nums) - 1 + + while left < right: + mid = (left + right) // 2 + + if nums[mid - 1] <= nums[mid] >= nums[mid + 1]: + return mid + elif nums[mid] < nums[mid + 1]: + left = mid + 1 + else: + right = mid + + return left \ No newline at end of file diff --git a/problem-of-the-day/day02/34 Find First Last.cpp b/problem-of-the-day/day02/34 Find First Last.cpp new file mode 100644 index 0000000..29c1316 --- /dev/null +++ b/problem-of-the-day/day02/34 Find First Last.cpp @@ -0,0 +1,44 @@ +/// inbuilt function to perform this operation is equal_range() which returns a pair of iterators pointing to the first element and last element of the range of elements that are equal to the value passed as argument. +class Solution { +public: + vector searchRange(vector& nums, int target) { + vector ans; + int index1 = -1, index2 = -1; + int n = nums.size(); + int start = 0, end = n-1, mid; + mid = start + (end-start)/2; + + //finding first occurance + while(start <= end){ + if(target == nums[mid]){ + index1 = mid; + end = mid-1; + } + else if(target < nums[mid]){ + end = mid-1; + } + else start = mid+1; + mid = start + (end-start)/2; + } + + start = 0; end = n-1; mid = start + (end-start)/2; + + //finding last occurance + while(start <= end){ + if(target == nums[mid]){ + index2 = mid; + start = mid+1; + } + else if(target < nums[mid]){ + end = mid-1; + } + else start = mid+1; + mid = start + (end-start)/2; + } + + ans.push_back(index1); + ans.push_back(index2); + return ans; + } +}; + diff --git a/problem-of-the-day/day02/34 Find First Last.py b/problem-of-the-day/day02/34 Find First Last.py new file mode 100644 index 0000000..08f95a7 --- /dev/null +++ b/problem-of-the-day/day02/34 Find First Last.py @@ -0,0 +1,35 @@ +class Solution: + def searchRange(self, nums: List[int], target: int) -> List[int]: + def findFirst(nums, target): + left, right = 0, len(nums) - 1 + while left <= right: + mid = left + (right - left) // 2 + if nums[mid] == target: + if mid == 0 or nums[mid - 1] != target: + return mid + else: + right = mid - 1 + elif nums[mid] < target: + left = mid + 1 + else: + right = mid - 1 + return -1 + + def findLast(nums, target): + left, right = 0, len(nums) - 1 + while left <= right: + mid = left + (right - left) // 2 + if nums[mid] == target: + if mid == len(nums) - 1 or nums[mid + 1] != target: + return mid + else: + left = mid + 1 + elif nums[mid] < target: + left = mid + 1 + else: + right = mid - 1 + return -1 + + first = findFirst(nums, target) + last = findLast(nums, target) + return [first, last] \ No newline at end of file diff --git a/problem-of-the-day/day02/367 Valid Perfectsquare.cpp b/problem-of-the-day/day02/367 Valid Perfectsquare.cpp new file mode 100644 index 0000000..cf10175 --- /dev/null +++ b/problem-of-the-day/day02/367 Valid Perfectsquare.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + bool isPerfectSquare(int num) { + long long low = 1, high = num; + long long mid, ans = -1; + while(low<=high){ + mid = low + (high-low)/2; + if(mid*mid <=num){ + ans = mid; + low = mid + 1; + } + else{ + high = mid - 1; + } + } + return (ans*ans==num); + } +}; \ No newline at end of file diff --git a/problem-of-the-day/day02/367 Valid Perfectsquare.py b/problem-of-the-day/day02/367 Valid Perfectsquare.py new file mode 100644 index 0000000..19b5f7a --- /dev/null +++ b/problem-of-the-day/day02/367 Valid Perfectsquare.py @@ -0,0 +1,13 @@ +class Solution: + def isPerfectSquare(self, num: int) -> bool: + low=1 + high=num + while(low<=high): + mid=(low+high)//2 + if mid*mid==num: + return True + elif mid*mid>num: + high=mid-1 + else : + low=mid+1 + return False \ No newline at end of file diff --git a/problem-of-the-day/day02/704 Binary Search.cpp b/problem-of-the-day/day02/704 Binary Search.cpp new file mode 100644 index 0000000..f1f4d01 --- /dev/null +++ b/problem-of-the-day/day02/704 Binary Search.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + int search(vector& nums, int target) { + int l = 0, h = nums.size() - 1; // Initialize 'l' to the start and 'h' to the end of the vector + int m; // Declare a variable 'm' to store the middle index + // Loop while the search space is valid (l <= h) + while (l <= h) { + m = l + (h - l) / 2; // Calculate the middle index to avoid potential overflow + // If the middle element is the target, return the index 'm' + if (nums[m] == target) return m; + // If the middle element is greater than the target, discard the right half + else if (nums[m] > target) h = m - 1; + // If the middle element is less than the target, discard the left half + else l = m + 1; + } + // If the target is not found, return -1 + return -1; + } +}; diff --git a/problem-of-the-day/day02/704 Binary Search.py b/problem-of-the-day/day02/704 Binary Search.py new file mode 100644 index 0000000..2197216 --- /dev/null +++ b/problem-of-the-day/day02/704 Binary Search.py @@ -0,0 +1,13 @@ +class Solution: + def search(self, nums: List[int], target: int) -> int: + low, high = 0, len(nums)-1 + while lowtarget: + high = mid - 1 + else: + low = mid + 1 + if nums[low]==target: return low + return -1 \ No newline at end of file diff --git a/problem-of-the-day/day02/dsa.md b/problem-of-the-day/day02/dsa.md index 852b176..dba7e76 100644 --- a/problem-of-the-day/day02/dsa.md +++ b/problem-of-the-day/day02/dsa.md @@ -20,3 +20,10 @@ To begin, follow [LeetCode](https://leetcode.com/) and [CSES](https://cses.fi/pr We'll cover more topics in DSA in the following weeks, so it is recommended you try to complete these problems as soon as possible. **Note:** Solutions to [day01](../day01) are uploaded. + +## Solutions +1. Number Spiral:- [C++](./1071%20Number%20Spiral.cpp) [Python3](./1071%20Number%20Spiral.py) +2. Binary Search:- [C++](./704%20Binary%20Search.cpp) [Python3](./704%20Binary%20Search.py) +3. Find first and last position of element in sorted array:- [C++](./34%20Find%20First%20Last.cpp) [Python3](./34%20Find%20First%20Last.py) +4. Valid Perfect Square:- [C++](./367%20Valid%20Perfectsquare.cpp) [Python3](./367%20Valid%20Perfectsquare.py) +5. Find Peak Element:- [C++](./162%20Peak%20Element.cpp) [Python3](./162%20Peak%20Element.py) \ No newline at end of file diff --git a/problem-of-the-day/day03/dsa.md b/problem-of-the-day/day03/dsa.md new file mode 100644 index 0000000..376412d --- /dev/null +++ b/problem-of-the-day/day03/dsa.md @@ -0,0 +1,27 @@ +# DSA Problem of the Day (29/05/2024) + +## Sorting/Searching and Binary Search + +Today, we will continue solving some problems on binary search. And learn about sorting and searching. Before that few follow ups on binary search. +The core idea of binary search relies on dividing our search space by 2. Which eventually leads to a logarithmic complexity. What if we divide our search space into 3 parts (or just say n), which will lead to a faster convergence to solution. Why it isn't used in real life? + +To begin, follow [LeetCode](https://leetcode.com/) and [CSES](https://cses.fi/problemset/list/) to solve the following problems. They will help you understand and practice key concepts in sorting: + +1. [Search in rotated array](https://leetcode.com/problems/search-in-rotated-sorted-array/description/):- Binary Search Problem. +2. [Distinct Numbers](https://cses.fi/problemset/task/1621):- Maps(ordered/unordered) leads to a O(n) solution too. Whereas sorting gives O(nlogn). What is the tradeoff between them ? +3. [Ferris Wheel](https://cses.fi/problemset/task/1090). + +## Resources + +1. [Sorting Algorithms](https://www.geeksforgeeks.org/sorting-algorithms/) +2. [Stable Sorting](https://www.geeksforgeeks.org/stable-and-unstable-sorting-algorithms/) + +## FollowUps + +* Can a sorting algorithm which only uses < or > operations (>= or <=), sort faster than O(nlogn) ? +* What is a stable sorting ? Can any sorting algorithm be made stable ? If yes then what is the use of fussing over stability ? If no, then why ? +* Can a sorting algorithm sort in place, does at max n swaps and be O(nlogn) in time complexity ? + +We'll cover more topics in DSA in the following weeks, so it is recommended you try to complete these problems as soon as possible. + +**Note:** Solutions to [day02](../day02) are uploaded.