Programming Articles

Page 481 of 2544

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 241 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

isunordered() function in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 91 Views

In this article we are going to discuss the isunordered() function in C++, its syntax, working and its return values.isunordered() function is an inbuilt function in C++ which is defined in header file. The function checks whether the two floating points number be NAN, if both or either one of them is NAN then it will return 1(true) else will return 0(false).Syntaxbool isunordered(float n1, float n2);orbool isunordered(double n1, double n2); orbool isunordered(long double n1, long double n2);The function accepts two floating point variables to compare and check if either one of these is nan.Return valueThe function return the boolean value ...

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

difftime() function in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 251 Views

In this article we are going to discuss the difftime() function in C++, its syntax, working and its return values.difftime() function is an inbuilt function in C++ which is defined in header file. The function accepts two parameters of time_t type, function calculate the difference between the two timesSyntaxdouble difftime(time_t end, time_t beginning);Return valueReturns the difference of the time in seconds, stored as double data type.Example#include #include int main () {    time_t now;    struct tm newyear;    double seconds;    time(&now); /* get current time; */    newyear = *localtime(&now);    newyear.tm_hour = 0; newyear.tm_min = ...

Read More

Minimum Swaps to Make Strings Equal in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 570 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 418 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

iswpunct() function in C++ STL

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 162 Views

In this article we are going to discuss the iswpunct() function in C++, its syntax, working and its return values.iswpunct() function is an inbuilt function in C++ which is defined in header file. The function checks whether the passed wide character is a punctuation character or not. This function is a wide character equivalent of ispunct(), which means it works the same as ispunct() the difference is it supports a wide character. So, the function checks if the argument passed is punctuation character then return any non zero integer value(true), else it will return zero(false)Punctuation characters are as follows! ...

Read More
Showing 4801–4810 of 25,433 articles
« Prev 1 479 480 481 482 483 2544 Next »
Advertisements