C++ Articles

Page 80 of 597

Sentence Similarity II in C++

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

Suppose we have Given two arrays words1, words2 these are considered as sentences, and a list of similar word pairs, we have to check whether two sentences are similar or not. So if the input is like words1 = ["great", "acting", "skills"] and words2 = ["fine", "drama", "talent"] these two are similar, if the similar word pairs are like = [["great", "good"], ["fine", "good"], ["acting", "drama"], ["skills", "talent"]].The similarity relation is transitive. For example, if "great" and "good" are similar, and "fine" and "good" are similar, then "great" and "fine" are also similar. And the similarity is also symmetric. So, ...

Read More

First Unique Number in C++

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

Suppose we have a queue of integers, we need to retrieve the first unique integer in that queue. We have to implement the class called FirstUnique: It will be initialized by the numbers in the queue. Define one function showFirstUnique(), this will return the value of the first unique integer of the queue and returns -1 if there is no such integer. Another method is add(value) this will insert value to the queue.So, if the input is likeInitialize with [2, 3, 4] then call the functions as follows −showFirstUnique()add(5)showFirstUnique()add(2)showFirstUnique()add(3)showFirstUnique(), then the output will be 2, 2, 3, -1 respectively.To solve ...

Read More

Check If All 1's Are at Least Length K Places Away in C++

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

Suppose we have an array nums of 0s and 1s and an integer k, we have to check whether all 1's are at least k places away from each other, otherwise, return False.So, if the input is like nums = [1, 0, 0, 0, 1, 0, 0, 1], k = 2, then the output will be true, as each of the 1s are at least 2 places away from each other.To solve this, we will follow these steps −last := -1for initialize i := 0, when i < size of nums, update (increase i by 1), do −if nums[i] is ...

Read More

Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree in C++

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

Suppose we have a binary tree where each path going from the root to any leaf form a valid sequence, we have to check whether a given string is a valid sequence in such binary tree or not.We will get the given string from the concatenation of an array of integers arr and the concatenation of all values of the nodes along a path results in a sequenceSuppose we have a binary tree like.So, if arr = [0, 1, 0, 1], then the output will be True, as the path 0 -> 1 -> 0 -> 1 is a valid ...

Read More

Count Triplets That Can Form Two Arrays of Equal XOR in C++

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

Suppose we have an array of integers arr. We want to select three indices like i, j and k where (0 = 0; j--) {             x1 = x1 ^ arr[j];             m[x1]++;          }          for (int j = i; j < n; j++) {             x2 = x2 ^ arr[j];             ret += m[x2];          }       }       return ret;    } }; main(){    Solution ob;    vector v = {2,3,1,6,7};    cout

Read More

Cheapest Flights Within K Stops in C++

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

Suppose we have n cities connected by m flights. Each flight starts from u and arrives at v with a price w. If we have all the cities and flights, together with starting city src and the destination dst, here our task is to find the cheapest price from src to dst with up to k stops. If there is no such route, then return -1.So, if the input is like n = 3, edges = [[0, 1, 100], [1, 2, 100], [0, 2, 500]], src = 0, dst = 2, k = 1, then the output will be 200To ...

Read More

All Nodes Distance K in Binary Tree in C++

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

Suppose we have a binary tree, a target node, and a one value K. We have to find a list of the values of all nodes that have a distance K from the target node.So, if the input is like root = [3, 5, 1, 6, 2, 0, 8, null, null, 7, 4], target = 5, K = 2, then the output will be [7, 4, 1], this is because the nodes that are a distance 2 from the target node have values 7, 4, and 1.To solve this, we will follow these steps −Define a function dfs(), this will ...

Read More

Longest Uncommon Subsequence II in C++

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

Suppose we have a list of strings; we have to find the longest uncommon subsequence among them. The longest uncommon subsequence is actually the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings.We know that a subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements.Here we will take a list of strings, and the output needs to be the length of the longest uncommon subsequence. If there is no longest uncommon subsequence, then return -1.So, if ...

Read More

01 Matrix in C++

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

Suppose we have a matrix consists of 0 and 1, we have to find the distance of the nearest 0 for each cell. Here the distance between two adjacent cells is 1.So, if the input is like000010111then the output will be000010121To solve this, we will follow these steps −Define an array dir of size: 4 x 2 := {{1, 0}, { - 1, 0}, {0, - 1}, {0, 1}}n := row count, m := column countDefine one matrix ret of order (n x m) and fill this with infDefine one queue qfor initialize i := 0, when i < n, ...

Read More

Number of Steps to Reduce a Number in Binary Representation to One in C++

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

Suppose we have a number s in binary form. We have to find the number of steps to reduce it to 1 under these rules −If the current number is even, we have to divide it by 2.If the current number is odd, you have to add 1 to it.So, if the input is like "1101", then the output will be 6, as "1101" is 13. So, 13 is odd, add 1 and obtain 14. Then 14 is even, divide by 2 and obtain 7. After that 7 is odd, add 1 and obtain 8.Then 8 is again, even so, ...

Read More
Showing 791–800 of 5,962 articles
« Prev 1 78 79 80 81 82 597 Next »
Advertisements