Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 156 of 377

Design A Leaderboard in C++

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

Suppose we have to design a Leaderboard class, there are three different functions −addScore(playerId, score) − This will update the leaderboard by adding score to the given player's score. When there is no such player with given id in the leaderboard, add him to the leaderboard with the given score.top(K) − This will return the score sum of the top K players.reset(playerId) − This will reset the score of the player with the given id to 0. It is guaranteed that the player was added to the leaderboard before calling this function.Initially, the leaderboard should empty.If we perform the operations ...

Read More

Binary Tree Right Side View in C++

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

Suppose we have a binary tree, if we see the tree from right side, then we can see some elements of it. we have to display those elements. So if the tree is like −To solve this, we will follow these steps −We will create one helping method for dfs. This will take tree_node, an array to hold answers, and level. The level is initially 0. The dfs will work like below −if node is null, then returnif level = length of the answer array, then insert value of node into the ans arraydfs(right of the node, ans, level + ...

Read More

Tree Diameter in C++

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

Suppose we have an undirected tree; we have to find its diameter − the number of edges in the longest path in that tree is the diameter of that tree. Here tree is given as an edge list where edges[i] = [u, v] is a bidirectional edge between nodes u and v. Each node has labels in the set {0, 1, ..., edges.length}. So if the graph is like −The output will be 4.To solve this, we will follow these steps −Define a map ldefine a method called dfs(). this will take v, an array called visited, the graph and ...

Read More

Number of Islands in Python

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

Suppose we have a grid, there are few 0s and few 1s. We have to count the number of islands. An island is place that is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. We can assume that all four edges of the grid are all surrounded by water.Suppose the grid is like −11000110000010000011There are three islands.To solve this, we will follow these steps −There will be two methods, one will be used to count number of islands called numIslands() and makeWater(). The makeWater() will be like −if number of rows in the grid is ...

Read More

Minimum Swaps to Make Strings Equal in C++

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

Suppose we have two strings s1 and s2 of equal length consisting of letters only "x" and "y". Our task is to make these two strings equal to each other. We can swap any two characters that belong to different strings, which means − swap s1[i] and s2[j]. We have to find the minimum number of swaps required to make s1 and s2 equal, or return -1 if it is impossible to do so. So if the strings are s1 = “xy” and s2 = “yx”, then the output will be 2. If we swap s1[0] and s2[0], s1 = ...

Read More

Triangle in C++

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

Suppose we have a triangle. We have to find the minimum path sum from top to the bottom. In each step, we can move to adjacent numbers on the row below.For example, if the following triangle is like[       [2],      [3, 4],     [6, 5, 7],    [4, 1, 8, 3] ]The minimum path sum from top to bottom is 11 (2 + 3 + 5 + 1 = 11).Let us see the steps −Create one table to use in Dynamic programming approach.n := size of trianglefor i := n – 2 down to 0for ...

Read More

Implement Trie (Prefix Tree) in Python

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

Suppose we have to make the trie structure, with three basic operations like insert(), search(), startsWith() methods. We can assume that all inputs are in lowercase letters. For example, if we call the functions as follows, we will see the outputsTrie trie = new Trie()trie.insert(“apple”)trie.search(“apple”)     //This will return truetrie.search(“app”)        //This will return falsetrie.startsWith(“app”)   //This will return truetrie.insert(“app”)trie.search(“app”)        //This will return trueTo solve this, we will follow these steps −Initially make one dictionary called child.The insert method will be like −current := childfor each letter l in word −if l is not present in ...

Read More

Count Number of Nice Subarrays in C++

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

Suppose we have an array of integers nums and an integer k. A subarray is known as nice subarray if there are k odd numbers on it. We have to find the number of nice sub-arrays. So if the array is [1, 1, 2, 1, 1], and k = 3, then the output will be 2, as the subarrays are [1, 1, 2, 1], and [1, 2, 1, 1]To solve this, we will follow these steps −ans := 0, n := size of nums arrayleft := 0 and right := 0, and count := 0define an array odd, fill this ...

Read More

Minimum Path Sum in Python

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

Suppose we have a m x n matrix filled with non-negative integers, find a path from top left corner to bottom right corner which minimizes the sum of all numbers along its path. Movements can only be either down or right at any point in time. So for example, if the matrix is like below131151421The output will be 7, the path will be 1, 3, 1, 1, 1, this will minimize the sumLet us see the steps −a := number of rows, b := number of columnsi := a – 1, j := b – 1while j >= 0matrix[a, j] ...

Read More

House Robber II in C++

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

Consider, you are a professional robber. And you are planning to rob houses along a street. Each house has a certain amount of money stored. All houses are arranged in a circle. That means the first house is the neighbor of the last house. We have to keep in mind that the adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night. So if we have a list of integers representing the amount of money of each house, determine the maximum amount of money you can ...

Read More
Showing 1551–1560 of 3,768 articles
« Prev 1 154 155 156 157 158 377 Next »
Advertisements