-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bbfd87c
commit 6bd28e5
Showing
12 changed files
with
263 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include <iostream> | ||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
class Solution { | ||
public: | ||
int findPeakElement(vector<int>& 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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<int> searchRange(vector<int>& nums, int target) { | ||
vector<int> 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; | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Solution { | ||
public: | ||
int search(vector<int>& 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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Solution: | ||
def search(self, nums: List[int], target: int) -> int: | ||
low, high = 0, len(nums)-1 | ||
while low<high: | ||
mid = (low + high)//2 | ||
if nums[mid]==target: | ||
return mid | ||
elif nums[mid]>target: | ||
high = mid - 1 | ||
else: | ||
low = mid + 1 | ||
if nums[low]==target: return low | ||
return -1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |