Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
Difficulty: Medium
Tags: Linked List, Depth-First Search, Doubly Linked List

Contributor: SOHAM-GADEKAR

---------------------------------------------------------------
Problem Statement:
---------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions Domains/CompetitiveProgramming/Programs/C/LeetCode/Q100.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
Difficulty: Easy
Tags: Binary Tree, Depth-First Search, Recursion

Contributor: SOHAM-GADEKAR

---------------------------------------------------------------
Problem Statement:
---------------------------------------------------------------
Expand Down
113 changes: 113 additions & 0 deletions Domains/CompetitiveProgramming/Programs/C/LeetCode/Q2095.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
===============================================================
2095. Delete the Middle Node of a Linked List
===============================================================

Difficulty: Medium
Tags: Linked List, Two Pointers

Contributor: SOHAM-GADEKAR

---------------------------------------------------------------
Problem Statement:
---------------------------------------------------------------
You are given the head of a linked list.
Delete the middle node, and return the head of the modified linked list.

The middle node of a linked list of size n is the ⌊n / 2⌋th node
(from the start, using 0-based indexing), where ⌊x⌋ denotes the
largest integer less than or equal to x.

For n = 1, 2, 3, 4, and 5, the middle nodes are indices:
0, 1, 1, 2, and 2 respectively.

---------------------------------------------------------------
Example 1:
---------------------------------------------------------------
Input: head = [1,3,4,7,1,2,6]
Output: [1,3,4,1,2,6]

Explanation:
The linked list has 7 nodes. The middle node is at index 3
(value = 7). After removing it, the new list is [1,3,4,1,2,6].

---------------------------------------------------------------
Example 2:
---------------------------------------------------------------
Input: head = [1,2,3,4]
Output: [1,2,4]

Explanation:
The middle node (index 2, value = 3) is removed,
leaving [1,2,4].

---------------------------------------------------------------
Example 3:
---------------------------------------------------------------
Input: head = [2,1]
Output: [2]

Explanation:
For n = 2, the middle node is index 1 (value = 1).
After removing it, only [2] remains.

---------------------------------------------------------------
Constraints:
---------------------------------------------------------------
- The number of nodes in the list is in the range [1, 10^5].
- 1 <= Node.val <= 10^5
===============================================================
*/

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/

struct ListNode* deleteMiddle(struct ListNode* head) {
struct ListNode* p = head; // Pointer for traversal
struct ListNode* q; // Temporary pointer for the node to delete

// Case 1: If there is only one node in the list
if (p->next == NULL) {
head = NULL; // The list becomes empty
free(p); // Free the single node
}

// Case 2: If there are only two or three nodes
else if (p->next->next == NULL || p->next->next->next == NULL) {
q = p->next; // q points to the second node

if (q->next == NULL) {
// Case: List has exactly two nodes → remove the second one
p->next = NULL;
} else {
// Case: List has three nodes → remove the middle one
p->next = q->next;
}
}

// Case 3: For lists having more than three nodes
else {
int count = 0, i = 1;

// Step 1: Count total number of nodes
for (; p != NULL; p = p->next, ++count);

// Step 2: Move p to the node just before the middle node
for (p = head; i < count / 2; p = p->next, i++);

// Step 3: Delete the middle node
q = p->next; // q points to the middle node
p->next = q->next; // Bypass the middle node
}

// Free memory of the deleted node
free(q);

// Return the updated head pointer
return head;
}
2 changes: 2 additions & 0 deletions Domains/CompetitiveProgramming/Programs/C/LeetCode/Q230.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
Difficulty: Medium
Tags: Binary Search Tree, Depth-First Search, Tree, Binary Tree

Contributor: SOHAM-GADEKAR

---------------------------------------------------------------
Problem Statement:
---------------------------------------------------------------
Expand Down
103 changes: 103 additions & 0 deletions Domains/CompetitiveProgramming/Programs/C/LeetCode/Q3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
===============================================================
3. Longest Substring Without Repeating Characters
===============================================================

Difficulty: Medium
Tags: Hash Table, String, Sliding Window

Contributor: SOHAM-GADEKAR

---------------------------------------------------------------
Problem Statement:
---------------------------------------------------------------
Given a string s, find the length of the longest substring
without repeating characters.

---------------------------------------------------------------
Example 1:
---------------------------------------------------------------
Input: s = "abcabcbb"
Output: 3

Explanation:
The answer is "abc", with the length of 3.
Note that "bca" and "cab" are also correct answers.

---------------------------------------------------------------
Example 2:
---------------------------------------------------------------
Input: s = "bbbbb"
Output: 1

Explanation:
The answer is "b", with the length of 1.

---------------------------------------------------------------
Example 3:
---------------------------------------------------------------
Input: s = "pwwkew"
Output: 3

Explanation:
The answer is "wke", with the length of 3.
Notice that the answer must be a substring,
"pwke" is a subsequence and not a substring.

---------------------------------------------------------------
Constraints:
---------------------------------------------------------------
- 0 <= s.length <= 5 * 10^4
- s consists of English letters, digits, symbols, and spaces
===============================================================
*/


int lengthOfLongestSubstring(char* s) {
// Array to track all printable ASCII characters (from space ' ' to '~')
// ' ' = 32, '~' = 126 → total 95 characters
int allCharacter[95] = {0};

int maxLength = 0; // Store the length of the longest substring

// Loop over each character in the string as starting point
for (int i = 0; s[i] != '\0'; i++) {
int currMaxLength = 0; // Length of substring starting at index i
int flag = 0; // Flag to break inner loop if a duplicate is found

// Explore substring starting at index i
for (int j = i; s[j] != '\0'; j++) {
// Check if character is printable ASCII (from space ' ' to '~')
if (s[j] >= ' ' && s[j] <= '~') {
// Check if character has already appeared in current substring
if (allCharacter[s[j] - 32] == 0) {
allCharacter[s[j] - 32] = 1; // Mark character as seen
currMaxLength++; // Increase current substring length
} else {
// Duplicate character found → stop current substring
flag = 1;
break;
}
} else {
// Non-printable character → stop current substring
break;
}
}

// Update maximum length if current substring is longer
if (currMaxLength > maxLength) {
maxLength = currMaxLength;
}

// If no duplicate was found, remaining substrings cannot be longer
if (flag == 0) {
break;
} else {
// Reset allCharacter array for the next starting index
for (int k = 0; k < 95; k++)
allCharacter[k] = 0;
}
}

return maxLength; // Return the length of the longest substring without repeating characters
}
153 changes: 153 additions & 0 deletions Domains/CompetitiveProgramming/Programs/C/LeetCode/Q98.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
===============================================================
98. Validate Binary Search Tree
===============================================================

Difficulty: Medium
Tags: Binary Tree, Recursion, Depth-First Search

Contributor: SOHAM-GADEKAR

---------------------------------------------------------------
Problem Statement:
---------------------------------------------------------------
Given the root of a binary tree, determine if it is a valid
binary search tree (BST).

A valid BST is defined as follows:
1. The left subtree of a node contains only nodes with keys
strictly less than the node's key.
2. The right subtree of a node contains only nodes with keys
strictly greater than the node's key.
3. Both the left and right subtrees must also be binary search trees.

---------------------------------------------------------------
Example 1:
---------------------------------------------------------------
Input: root = [2,1,3]
Output: true

Explanation:
The tree is a valid BST:
2
/ \
1 3

---------------------------------------------------------------
Example 2:
---------------------------------------------------------------
Input: root = [5,1,4,null,null,3,6]
Output: false

Explanation:
The root node's value is 5 but its right child's value is 4,
which violates the BST property.

---------------------------------------------------------------
Constraints:
---------------------------------------------------------------
- The number of nodes in the tree is in the range [1, 10^4].
- -2^31 <= Node.val <= 2^31 - 1
===============================================================
*/

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/


/**
* Function: isValidBST
* --------------------
* Checks whether a binary tree is a valid binary search tree (BST).
*
* @param root : Pointer to the root of the tree.
* @return : true if the tree is a valid BST, false otherwise.
*
* Approach:
* 1. For each node, check all nodes in the left subtree are strictly less.
* 2. Check all nodes in the right subtree are strictly greater.
* 3. Recursively validate left and right subtrees.
*/

bool isValidBST(struct TreeNode* root) {
bool isValid = true;

if (root != NULL) {
// Step 1: Validate left subtree values are less than root
isValid = isValidLeftSubTree(root, root->left);
if (isValid == false)
return false;

// Step 2: Validate right subtree values are greater than root
isValid = isValidRightSubTree(root, root->right);
if (isValid == false)
return false;

// Step 3: Recursively validate left subtree
isValid = isValidBST(root->left);
if (isValid == false)
return false;

// Step 4: Recursively validate right subtree
isValid = isValidBST(root->right);
}

return isValid;
}

/**
* Function: isValidLeftSubTree
* ----------------------------
* Checks if all nodes in the left subtree are strictly less than root's value.
*/

bool isValidLeftSubTree(struct TreeNode* root, struct TreeNode* p) {
bool isValid = true;

if (p != NULL) {
// If any node violates BST property, return false
if (root->val <= p->val)
return false;

// Recursively check left child
isValid = isValidLeftSubTree(root, p->left);
if (isValid == false)
return false;

// Recursively check right child
isValid = isValidLeftSubTree(root, p->right);
}

return isValid;
}

/**
* Function: isValidRightSubTree
* -----------------------------
* Checks if all nodes in the right subtree are strictly greater than root's value.
*/
bool isValidRightSubTree(struct TreeNode* root, struct TreeNode* p) {
bool isValid = true;

if (p != NULL) {
// If any node violates BST property, return false
if (root->val >= p->val)
return false;

// Recursively check left child
isValid = isValidRightSubTree(root, p->left);
if (isValid == false)
return false;

// Recursively check right child
isValid = isValidRightSubTree(root, p->right);
}

return isValid;
}
Loading