Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 202 of 377

Zigzag Iterator in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 337 Views

Suppose there are two 1D arrays, we have to implement an iterator that will return their elements alternately. There will be two methods −next() − to get next elementhasNext() − to check whether the next element is present or not.So, if the input is like v1 = [1, 2] v2 = [3, 4, 5, 6] , then the output will be [1, 3, 2, 4, 5, 6], To solve this, we will follow these steps −Define one queue q of pairsFrom the initializer ake two arrays v1 and v2, if size of v1, then −insert { 0, 0 } into ...

Read More

Inorder Successor in BST in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 248 Views

Suppose we have a binary search tree and a node in it, we have to search the in-order successor of that node in the BST. As we know that the successor of a node p is the node with the smallest key greater than p.val.So, if the input is like root = [2, 1, 3], p = 1, then the output will be 2, To solve this, we will follow these steps −Define recursive method inorderSuccessor(), this will take root and pif root null, then −return nullif val of root val val){          return inorderSuccessor(root->right, p);   ...

Read More

Program to remove sublist to get same number of elements below and above k in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 215 Views

Suppose we have a list of numbers called nums and another number k, we can remove any sublist at most once from the list. We have to find the length of the longest resulting list such that the amount of numbers strictly less than k and strictly larger than k is the same.So, if the input is like nums = [6, 10, 8, 9, 3, 5], k = 6, then the output will be 5, as if we remove the sublist [9] then we will get [6, 10, 8, 3, 5] and there are two numbers [3, 5] which are ...

Read More

Walls and Gates in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 631 Views

Suppose we have one m x n 2D grid, and that is initialized with these three possible values.-1 for a wall or an obstacle.0 for a gate.INF This is infinity means an empty room.Here 2^31 - 1 = 2147483647 is INF as we may assume that the distance to a gate is less than 2147483647. Fill each empty room with the distance to its nearest gate. If it is impossible to reach a gate, it should be filled with INF.So, if the input is likeINF-10INFINFINFINF-1INF-1INF-10-1INFINFthen the output will be3-101221-11-12-10-134To solve this, we will follow these steps −Define an array dir ...

Read More

Flip Game II in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 402 Views

Suppose there are two players who are playing the flip game. Here we have a string that contains only these two characters: + and -, player1 and player2 take turns to flip two consecutive "++" into "--". The game ends when one player can no longer make a move and therefore the other one will be the winner. We have to define a function to check whether the starting player can guarantee a win.So, if the input is like s = "++++", then the output will be true, as the starting player can guarantee a win by flipping the middle ...

Read More

Binary Tree Longest Consecutive Sequence in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 278 Views

Suppose we have a binary tree; we have to check whether we can find the length of the longest consecutive sequence path. If the path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to follow parent to child but not reverse.So, if the input is like, then the output will be 3, as the Longest consecutive sequence path is 3-4-5, so return 3.To solve this, we will follow these steps −Define a function solveUtil(), this will take node, prev, len initialize it with ...

Read More

Binary Tree Vertical Order Traversal in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 492 Views

Suppose there is a binary tree, we have to find the vertical order traversal of its nodes' values. If two nodes are in the same row and column, the order should be from left to right.So, if the input is like, then the output will be [[9], [3, 15], [20], [7]]To solve this, we will follow these steps −Define one map mDefine a function solve(), this will take node, x initialize it with 0, if node is null, then −returnsolve(left of node, x - 1)solve(right of node, x + 1)insert value of node at the end of m[x]From the main ...

Read More

Generalized Abbreviation in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 333 Views

Suppose there is a word. We have to define a function that can generate the generalized abbreviations of a word.So, if the input is like "word", then the output will be ["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]To solve this, we will follow these steps −Define an array retDefine a function solve(), this will take s, idx, if idx >= size of s, then −insert s at the end of retreturny := substring of s from index 0 to idx - 1i := size of ynum := blank stringwhile (i >= ...

Read More

Number of Connected Components in an Undirected Graph in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 2K+ Views

Suppose we have n nodes and they are labeled from 0 to n - 1 and a list of undirected edges, are also given, we have to define one function to find the number of connected components in an undirected graph.So, if the input is like n = 5 and edges = [[0, 1], [1, 2], [3, 4]], then the output will be 2To solve this, we will follow these steps −Define a function dfs(), this will take node, graph, an array called visited, if visited[node] is false, then −visited[node] := truefor initialize i := 0, when i < size ...

Read More

Maximum Size Subarray Sum Equals k in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 859 Views

Suppose we have an array called nums and a target value k, we have to find the maximum length of a subarray that sums to k. If there is not present any, return 0 instead.So, if the input is like nums = [1, -1, 5, -2, 3], k = 3, then the output will be 4, as the subarray [1, - 1, 5, -2] sums to 3 and is the longest.To solve this, we will follow these steps −ret := 0Define one map mn := size of numstemp := 0, m[0] := -1for initialize i := 0, when i < ...

Read More
Showing 2011–2020 of 3,768 articles
« Prev 1 200 201 202 203 204 377 Next »
Advertisements