Skip to content

Commit

Permalink
Merge branch 'Saloni6111:main' into add-1
Browse files Browse the repository at this point in the history
  • Loading branch information
subin-shk authored Oct 31, 2024
2 parents abfe2f9 + a3fac8b commit cb12379
Show file tree
Hide file tree
Showing 17 changed files with 695 additions and 95 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/greetings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ jobs:
- uses: actions/first-interaction@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
issue-message: "Thank you for raising a issue, Hope you enjoing the open source. we try to reply or assign as soon possibe. Connect with mentor."
issue-message: "Thank you for raising a issue, Hope you enjoing the open source. I'm sorry to inform you that this project got excluded from Hacktoberfest-2024 your contribution will not be counted. If you still wants to contribute please go-ahead."
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.vscode/
.vscode/
*.class
36 changes: 36 additions & 0 deletions C++/98_Validate-BST.cpp
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
*/
63 changes: 11 additions & 52 deletions C++/AdjacentBitCounts.cpp
Original file line number Diff line number Diff line change
@@ -1,74 +1,35 @@
/**
* Problem:- Adjacent Bit Counts
* Link: https://www.spoj.com/problems/GNYR09F/
*/

/**
* Explaination:-
*
* State Representation: We will define a dynamic programming (DP) state dp[n][k][last]:
* n: The current length of the bit string.
* k: The remaining adjacent bit count to satisfy.
* last: The last bit of the string (either 0 or 1)
*
* Base Cases:
* If n == 1 and k == 0, there is only one valid bit string (either "0" or "1"), but no adjacent pairs can exist for n == 1.
* If k > 0 when n == 1, it's impossible to satisfy, so return 0 in that case.
*
* Recursive-Calls:
* If the current bit is 1, the previous bit could either be 1 (which would contribute to the adjacent bit count) or 0.
* If the current bit is 0, the previous bit could either be 0 or 1 (but no adjacent bit count is added).
*/

#include <iostream>
#include <cstring> // For memset
#include <cstring>
using namespace std;
#define ll long long int

// DP table where dp[n][k][last] represents the number of ways to get a bit string of length n with exactly k adjacent '1's ending in 'last' (0 or 1)
ll dp[1001][101][2];

/**
* Time complexity: (N*K*2) => (N*K)
* Space complexity: (N*K*2) => (N*K)
*/
ll dp[101][101][2];

// Function to calculate the number of valid bit strings
ll ftd(int n, int k, int last){ // function-of-top-down
ll ftd(int n, int k, int last) {
// Base case: If n is 1, we can only have a single bit string
if (n == 1)
{
// If we want 0 adjacent pairs, it's valid if k == 0
if (k == 0){
return 1;
}
return 0;
if (n == 1) {
return k == 0 ? 1 : 0;
}

// If we have already solved this subproblem, return the result
if (dp[n][k][last] != -1){
if (dp[n][k][last] != -1) {
return dp[n][k][last];
}


// Initialize the result for the current state
ll ans = 0;

// If the current bit is 1, we consider two possibilities:
// 1. The previous bit was also 1 (adds to adjacent count).
// 2. The previous bit was 0 (no adjacent 1s added).
if (last == 1)
{
// We can only decrement k if we place a '1' next to another '1'.
if (k > 0)
{
if (last == 1) {
if (k > 0) {
ans += ftd(n - 1, k - 1, 1); // Case when previous bit was 1 (k-1)
}
ans += ftd(n - 1, k, 0); // Case when current bit was 0 (k remains the same)
}
// If the last bit is 0, we can append a '0' or '1' to the previous part of the string.
else
{
} else {
ans += ftd(n - 1, k, 0); // Current bit was also 0 (k stays the same)
ans += ftd(n - 1, k, 1); // Current bit was 1 (k stays the same)
}
Expand All @@ -77,13 +38,11 @@ ll ftd(int n, int k, int last){ // function-of-top-down
return dp[n][k][last] = ans;
}

int main()
{
int main() {
int t;
cin >> t; // Number of test cases

while (t--)
{
while (t--) {
int number_of_test_case, n, k;
cin >> number_of_test_case >> n >> k;

Expand Down
48 changes: 23 additions & 25 deletions C++/LCAinBinaryTree.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// C++ program to print right view of Binary Tree
// using recursion
#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

class Node {
Expand All @@ -14,30 +14,29 @@ class Node {
}
};

void RecursiveRightView(Node* root, int level,
int& maxLevel, vector<int>& result) {
if (!root) return;

if (level > maxLevel) {
result.push_back(root->data);
maxLevel = level;
}

RecursiveRightView(root->right, level + 1,
maxLevel, result);
RecursiveRightView(root->left, level + 1,
maxLevel, result);
}

vector<int> rightView(Node *root) {
vector<int> rightView(Node* root) {
vector<int> result;
int maxLevel = -1;
RecursiveRightView(root, 0, maxLevel, result);

if (!root) return result;

queue<Node*> q;
q.push(root);

while (!q.empty()) {
int n = q.size();
for (int i = 0; i < n; ++i) {
Node* node = q.front();
q.pop();
if (i == n - 1) {
result.push_back(node->data);
}
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
}
return result;
}

void printArray(vector<int>& arr) {
void printArray(const vector<int>& arr) {
for (int val : arr) {
cout << val << " ";
}
Expand All @@ -52,8 +51,7 @@ int main() {
root->right->right = new Node(5);

vector<int> result = rightView(root);

printArray(result);

return 0;
}
37 changes: 21 additions & 16 deletions C++/ValidParentheses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,32 @@

class Solution {
public:
bool isValid(std::string s) {
std::stack<char> mp;
bool isValid(const std::string& s) {
std::stack<char> stack;

for (char c : s) {
if (c == '(' || c == '[' || c == '{') {
mp.push(c);
} else {
if (mp.empty()) {
return false;
}
if ((c == ')' && mp.top() == '(') ||
(c == ']' && mp.top() == '[') ||
(c == '}' && mp.top() == '{')) {
mp.pop();
} else {
return false;
}
switch (c) {
case '(': case '[': case '{':
stack.push(c);
break;
case ')':
if (stack.empty() || stack.top() != '(') return false;
stack.pop();
break;
case ']':
if (stack.empty() || stack.top() != '[') return false;
stack.pop();
break;
case '}':
if (stack.empty() || stack.top() != '{') return false;
stack.pop();
break;
default:
return false; // Invalid character
}
}

return mp.empty();
return stack.empty();
}
};

Expand Down
46 changes: 46 additions & 0 deletions C++/connected-components.cpp
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;
}
43 changes: 43 additions & 0 deletions C++/linkedListCycle.cpp
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;
}
33 changes: 33 additions & 0 deletions C++/searchInRotatedArrayII.cpp
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;
}
};
Loading

0 comments on commit cb12379

Please sign in to comment.