Skip to content

Commit

Permalink
day03dsa day02dsasolution
Browse files Browse the repository at this point in the history
  • Loading branch information
33arsenic75 committed May 29, 2024
1 parent bbfd87c commit 6bd28e5
Show file tree
Hide file tree
Showing 12 changed files with 263 additions and 0 deletions.
36 changes: 36 additions & 0 deletions problem-of-the-day/day02/1071 Number Spiral.cpp
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;
21 changes: 21 additions & 0 deletions problem-of-the-day/day02/1071 Number Spiral.py
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)
15 changes: 15 additions & 0 deletions problem-of-the-day/day02/162 Peak Element.cpp
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;
}
};
15 changes: 15 additions & 0 deletions problem-of-the-day/day02/162 Peak Element.py
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
44 changes: 44 additions & 0 deletions problem-of-the-day/day02/34 Find First Last.cpp
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;
}
};

35 changes: 35 additions & 0 deletions problem-of-the-day/day02/34 Find First Last.py
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]
18 changes: 18 additions & 0 deletions problem-of-the-day/day02/367 Valid Perfectsquare.cpp
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);
}
};
13 changes: 13 additions & 0 deletions problem-of-the-day/day02/367 Valid Perfectsquare.py
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
19 changes: 19 additions & 0 deletions problem-of-the-day/day02/704 Binary Search.cpp
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;
}
};
13 changes: 13 additions & 0 deletions problem-of-the-day/day02/704 Binary Search.py
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
7 changes: 7 additions & 0 deletions problem-of-the-day/day02/dsa.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
27 changes: 27 additions & 0 deletions problem-of-the-day/day03/dsa.md
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.

0 comments on commit 6bd28e5

Please sign in to comment.