-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'Saloni6111:main' into add-1
- Loading branch information
Showing
17 changed files
with
695 additions
and
95 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
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
.vscode/ | ||
.vscode/ | ||
*.class |
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 @@ | ||
struct TreeNode { | ||
int val; | ||
TreeNode *left; | ||
TreeNode *right; | ||
TreeNode(int x) : val(x), left(NULL), right(NULL) {} | ||
}; | ||
|
||
class Solution { | ||
public: | ||
bool isValidBST(TreeNode* root, TreeNode* minNode = nullptr, TreeNode* maxNode = nullptr) { | ||
// Base case: an empty tree is a valid BST | ||
if (root == nullptr) return true; | ||
|
||
// If there is a restriction on the minimum value | ||
if (minNode != nullptr && root->val <= minNode->val) return false; | ||
// If there is a restriction on the maximum value | ||
if (maxNode != nullptr && root->val >= maxNode->val) return false; | ||
|
||
// Recursively validate the left and right subtrees | ||
return isValidBST(root->left, minNode, root) && isValidBST(root->right, root, maxNode); | ||
} | ||
}; | ||
/* | ||
2 | ||
/ \ | ||
1 3 | ||
Expected : True | ||
10 | ||
/ \ | ||
5 15 | ||
/ \ | ||
6 20 | ||
Expected : 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
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
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,46 @@ | ||
#include<iostream> | ||
#include <vector> | ||
using namespace std; | ||
void dfs(vector<vector<int>>& graph, vector<bool>& arr, int at) | ||
{ | ||
if (arr[at]) return; | ||
arr[at] = true; | ||
for (int i = 0; i < graph.size(); ++i) { | ||
if (graph[at][i] == 1 && !arr[i]) { | ||
dfs(graph, arr, i); | ||
} | ||
} | ||
} | ||
bool isconn(vector<vector<int>>& graph) { | ||
int n = graph.size(); | ||
vector<bool> v(n, false); | ||
int count = 0; | ||
for (int i = 0; i < n; ++i) { | ||
if (!v[i]) { | ||
count += 1; | ||
// if (count > 1) { | ||
// return false; | ||
// } | ||
dfs(graph, v, i); | ||
} | ||
} | ||
return count; | ||
} | ||
int main() { | ||
vector<vector<int>> g1 = { | ||
{0, 1, 0, 0}, | ||
{1, 0, 0, 0}, | ||
{0, 0, 0, 1}, | ||
{0, 0, 1, 0} | ||
}; | ||
vector<vector<int>> g2 = { | ||
{0, 1, 1, 0}, | ||
{1, 0, 1, 1}, | ||
{1, 1, 0, 1}, | ||
{0, 1, 1, 0} | ||
}; | ||
|
||
cout << isconn(g1) << endl; | ||
cout << isconn(g2) << endl; | ||
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,43 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
// Definition for Linked List | ||
struct ListNode { | ||
int val; | ||
ListNode *next; | ||
ListNode(int x) : val(x), next(NULL) {} | ||
}; | ||
|
||
class Solution { | ||
public: | ||
// checking if the linked list has a cycle | ||
bool hasCycle(ListNode *head) { | ||
ListNode* slow = head; | ||
ListNode* fast = head; | ||
while (fast != NULL && fast->next != NULL) { | ||
slow = slow->next; | ||
fast = fast->next->next; | ||
if (slow == fast) return true; | ||
} | ||
return false; | ||
} | ||
}; | ||
|
||
int main() { | ||
// creating a Linked List with Cycle | ||
ListNode* head = new ListNode(3); | ||
head->next = new ListNode(2); | ||
head->next->next = new ListNode(0); | ||
head->next->next->next = new ListNode(-4); | ||
head->next->next->next->next = head->next; | ||
|
||
// Printing the Output | ||
Solution solution; | ||
if (solution.hasCycle(head)) { | ||
cout << "The linked list has a cycle." << endl; | ||
} else { | ||
cout << "The linked list does not have a cycle." << endl; | ||
} | ||
|
||
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,33 @@ | ||
class Solution { | ||
public: | ||
bool search(vector<int>& nums, int target) { | ||
int left = 0, right = nums.size() - 1; | ||
|
||
while (left <= right) { | ||
int mid = left + (right - left) / 2; | ||
|
||
if (nums[mid] == target) { | ||
return true; | ||
} | ||
|
||
// Handle duplicates | ||
if (nums[left] == nums[mid] && nums[mid] == nums[right]) { | ||
left++; | ||
right--; | ||
} else if (nums[left] <= nums[mid]) { // Left half is sorted | ||
if (nums[left] <= target && target < nums[mid]) { | ||
right = mid - 1; // Target is in the left half | ||
} else { | ||
left = mid + 1; // Target is in the right half | ||
} | ||
} else { // Right half is sorted | ||
if (nums[mid] < target && target <= nums[right]) { | ||
left = mid + 1; // Target is in the right half | ||
} else { | ||
right = mid - 1; // Target is in the left half | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
}; |
Oops, something went wrong.