diff --git a/CPP/algorithms/revision/25-revision-questions/day-03-stacks-queues/01_valid_parentheses.cpp b/CPP/algorithms/revision/25-revision-questions/day-03-stacks-queues/01_valid_parentheses.cpp new file mode 100644 index 00000000..693a9aaf --- /dev/null +++ b/CPP/algorithms/revision/25-revision-questions/day-03-stacks-queues/01_valid_parentheses.cpp @@ -0,0 +1,91 @@ +// 01_valid_parentheses.cpp +// Problem: Valid Parentheses +// LeetCode: https://leetcode.com/problems/valid-parentheses/ +// Author: DeveloperViraj (curated DSA set) +// Compile: g++ -std=c++17 01_valid_parentheses.cpp -o 01_valid_parentheses +// Run: ./01_valid_parentheses +// +// Description: +// Given a string containing only the characters '(', ')', '{', '}', '[' and ']', +// determine if the input string is valid. An input string is valid if: +// 1) Open brackets are closed by the same type of brackets. +// 2) Open brackets are closed in the correct order. +// The algorithm uses a stack to keep track of opening brackets; upon encountering +// a closing bracket we check whether the top of the stack matches the expected opening. +// +// Use case: +// Syntax validation, small parser checks, expression validation in editors. +// +// Time Complexity: O(n) — each character is pushed/popped at most once. +// Space Complexity: O(n) — stack for open brackets (worst case all opens). +// +// Input format (example): +// A single line containing the bracket string. +// Example: +// ()[]{} => Valid +// (] => Invalid +// +// Output: +// Prints "Valid" if the string is valid, otherwise prints "Invalid". + +#include +#include +#include +#include + +using namespace std; + +bool isValidParentheses(const string &s) { + stack openStack; + // Map closing -> corresponding opening + unordered_map closingToOpening = { + {')', '('}, + {'}', '{'}, + {']', '['}, + }; + + for (char ch : s) { + // If it's an opening bracket, push to stack + if (ch == '(' || ch == '{' || ch == '[') { + openStack.push(ch); + } else if (ch == ')' || ch == '}' || ch == ']') { + // If stack empty or top doesn't match expected opening, invalid + if (openStack.empty() || openStack.top() != closingToOpening[ch]) { + return false; + } + openStack.pop(); + } else { + // If other characters exist, ignore or treat as invalid depending on spec. + // For this problem we assume input contains only parentheses characters. + } + } + + // Valid only if no unmatched opens remain + return openStack.empty(); +} + +int main() { + cout << "🔹 Problem: Valid Parentheses\n"; + cout << "Enter a single line containing the string of brackets (e.g. ()[]{}):\n"; + + string input; + if (!getline(cin, input)) { + cerr << "No input provided.\n"; + return 0; + } + + bool result = isValidParentheses(input); + if (result) cout << "Valid\n"; + else cout << "Invalid\n"; + + return 0; +} + +/* +Example: +Input: +()[]{} + +Output: +Valid +*/ diff --git a/CPP/algorithms/revision/25-revision-questions/day-03-stacks-queues/02_next_greater_element.cpp b/CPP/algorithms/revision/25-revision-questions/day-03-stacks-queues/02_next_greater_element.cpp new file mode 100644 index 00000000..9efe1c49 --- /dev/null +++ b/CPP/algorithms/revision/25-revision-questions/day-03-stacks-queues/02_next_greater_element.cpp @@ -0,0 +1,108 @@ +// 02_next_greater_element.cpp +// Problem: Next Greater Element (monotonic stack) +// LeetCode (related): https://leetcode.com/problems/next-greater-element-i/ +// Author: DeveloperViraj (curated DSA set) +// Compile: g++ -std=c++17 02_next_greater_element.cpp -o 02_next_greater_element +// Run: ./02_next_greater_element +// +// Description: +// For each element in an array, find the next greater element to its right. +// If no greater element exists, the answer for that position is -1. +// We use a monotonic decreasing stack that stores indices of elements whose +// next greater element hasn't been found yet. As we iterate, when we find +// a greater element, we pop and assign its next greater. +// +// Use case: +// Useful in many pattern problems: stock span variants, skyline queries, +// preprocessing for range queries, etc. +// +// Time Complexity: O(n) — each element is pushed and popped at most once. +// Space Complexity: O(n) — stack + output array. +// +// Input format: +// n +// a0 a1 a2 ... a(n-1) +// +// Output: +// n integers (space-separated) — next greater element for each index, -1 if none. +// +// Example: +// Input: +// 9 +// 2 1 2 4 3 5 1 2 0 +// +// Output: +// 4 2 4 5 5 -1 2 -1 -1 +// +// (Each index replaced by its next greater to the right) + +#include +#include +#include + +using namespace std; + +vector nextGreaterElements(const vector &values) { + int n = (int)values.size(); + vector answer(n, -1); + stack indexStack; // store indices of elements, monotonic decreasing by value + + for (int i = 0; i < n; ++i) { + int currentValue = values[i]; + // While current value is greater than the value at stack top index, + // we have found the next greater for that index. + while (!indexStack.empty() && values[indexStack.top()] < currentValue) { + int idx = indexStack.top(); + indexStack.pop(); + answer[idx] = currentValue; + } + // Push current index — its NG not known yet + indexStack.push(i); + } + + // Remaining indices have no next greater; their answer remains -1. + return answer; +} + +int main() { + cout << "🔹 Problem: Next Greater Element (to the right)\n"; + cout << "Input format:\n n\n a0 a1 ... a(n-1)\nEnter n and the array:\n"; + + int n; + if (!(cin >> n) || n < 0) { + cerr << "Invalid input for n.\n"; + return 0; + } + + vector arr(n); + for (int i = 0; i < n; ++i) cin >> arr[i]; + + vector result = nextGreaterElements(arr); + + cout << "\nNext greater elements:\n"; + for (int i = 0; i < n; ++i) { + cout << result[i]; + if (i + 1 < n) cout << ' '; + } + cout << '\n'; + + return 0; +} + +/* +Example: +Input: +6 +2 7 3 5 4 6 + +Output: +7 -1 5 6 6 -1 + +Explanation: +- Next greater after 2 is 7 +- 7 has no next greater -> -1 +- After 3 is 5 +- After 5 is 6 +- After 4 is 6 +- 6 has none -> -1 +*/