Author: devangini123

🌙 Problem Statement: Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty). Implement the MyStack class: void push(int x)Pushes element x to the top of the stack. int pop()Removes the element on the top of the stack and returns it.. int top()Returns the element on the top of the stack. boolean empty()Returns true if the stack is empty, false otherwise. Notes You must use only standard operations of a queue, which means that only push to back, peek/pop from front, size and is…

Read More

🌙 Problem Statement: Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty). Implement the MyStack class: void push(int x)Pushes element x to the top of the stack. int pop()Removes the element on the top of the stack and returns it.. int top()Returns the element on the top of the stack. boolean empty()Returns true if the stack is empty, false otherwise. Notes You must use only standard operations of a queue, which means that only push to back, peek/pop from front, size and is…

Read More

🌙 Common Operations → Stack Push: Add an element to the top of the stack Pop: Remove the top-most element from the stack Peek/Top: View the top-most element without removing it Stack in JavaScript let stack = []; // Simply, Use Array as Stack. stack.push(1); // Add inside stack stack.push(2); stack.push(3); console.log(stack); // [1, 2, 3] stack.pop(); // A stack is LIFO (Last In, First Out), so the element that goes in last will come out first. In this case, 3 will be removed. stack.pop(); console.log(stack); // [1] stack.push(7); // [1, 7] // For Top element let top = stack[stack.length…

Read More

🌙 What is a Stack? Stack follows the LIFO (Last-In-First-Out) principle. The last element added is the first one to come out. Stack Operations Push: Add an element to the top Pop: Remove the top element Peek/Top: View the top-most element Visualisation: Example Flow stack.push(1) → [1] stack.push(3) → [1, 3] stack.pop() → returns 3 → [1] stack.peek() → returns 1 Examples Stacks of Books Undo/Redo feature Recursion (Function Call Stack) Browsing History What is a Queue? Queue follows the FIFO (First-In-First-Out) principle. The first element added is the first to be removed. Queue Operations Enqueue: Add an element to…

Read More

🌙 Problem Statement: Given a binary array nums, return the maximum number of consecutive 1’s in the array. Examples Example 1: Input:nums = [1,1,0,1,1,1] Output:3 Explanation The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3. Example 2: Input:nums = [1,0,1,1,0,1] Output:2 Constraints: 1 maxCount ? currentCount : maxCount; } public class Solution { public int FindMaxConsecutiveOnes(int[] nums) { int currentCount = 0, maxCount = 0; foreach (int num in nums) { if (num == 1) { currentCount++; } else { maxCount = Math.Max(maxCount, currentCount); currentCount = 0; } }…

Read More

🌙 Problem Statement: Given an integer array nums, move all 0’s to the end of it while maintaining the relative order of the non-zero elements. Note:You must do this in-place without making a copy of the array. Examples Example 1: Input:nums = [0,1,0,3,12] Output:[1,3,12,0,0] Example 2: Input:nums = [0] Output:[0] Constraints: 1

Read More

Problem Statement: You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively. Mergenums1 and nums2 into a single array sorted in non-decreasing order. The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate this, nums1 has a length of m + n, where the first m elements denote the elements that should be merged, and the last n elements are set to 0 and should be ignored. nums2 has a…

Read More

🌙 Problem Statement: You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0. Examples: Example 1: Input:prices = [7, 1, 5, 3, 6, 4] Output:5 Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6 – 1…

Read More

🌙 Problem Statement: Write a function that reverses a string. The input string is given as an array of characters s. You must do this by modifying the input array in-place with O(1)extra memory. Examples: Example 1: Input:s = [“h”,”e”,”l”,”l”,”o”] Output:[“o”,”l”,”l”,”e”,”h”] Example 2: Input:s = [“H”,”a”,”n”,”n”,”a”,”h”] Output:[“h”,”a”,”n”,”n”,”a”,”H”] Approach: Two Pointer Technique Initialize two pointers, one at the start and one at the end of the array. Swap the characters at both pointers. Move the pointers towards the center until they meet. Time Complexity: Time Complexity = O(n) Space Complexity: Space Complexity = O(1) Dry Run Input: s = [“h”, “e”,…

Read More

🌙 Problem Statement: Given an integer array nums and an integer val, remove all occurrences of val in-place. The order of the elements may be changed. Then return the number of elements in nums which are not equal to val. Consider the number of elements in nums which are not equal to val be k. To get accepted, you need to: Modify nums such that the first k elements contain elements not equal to val. The remaining elements beyond k do not matter. Return k. Examples: Example 1: Input: nums = [3,2,2,3], val = 3 Output: 2, nums = [2,2,_,_]…

Read More