Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Arnab Chakraborty
Page 264 of 377
Count Square Submatrices with All Ones in C++
Suppose we a binary matrix, of size m x n. We have to count number of square submatrices, with all 1s. So if the matrix is like −011111110111So there will be 15 squares. 10 squares of single ones, 4 squares of four ones, and 1 square with nine ones.To solve this, we will follow these steps −set ans := 0, n := row count and m := column countfor i in range 0 to m – 1ans := ans + matrix[n – 1, i]for i in range 0 to n – 1ans := ans + matrix[i, m – 1]ans := ...
Read MoreGroup the People Given the Group Size They Belong To in C++
Suppose there are n people whose IDs are in range 0 to n - 1 and each person belongs exactly to one group. We have the array groupSizes of length n. This array is indicating that the group size each person belongs to, we have to find the groups there are and the people's IDs each group includes.Suppose the input is like − [3, 3, 3, 3, 3, 1, 3], then the output is [[5], [0, 1, 2], [3, 4, 6]], Other possible solutions can be [[2, 1, 6], [5], [0, 4, 3]] or [[5], [0, 6, 2], [4, 3, ...
Read MoreMinimum Remove to Make Valid Parentheses in C++
Suppose we have a string s of '(' , ')' and lowercase English characters. We have to remove the minimum number of parentheses ( '(' or ')', in any positions ) so that the resulting parenthese string is valid and return any valid string. A parentheses string is valid when all of these criteria are fulfilled −It is the empty string, contains lowercase characters only, orIt can be written as the form of AB (A concatenated with B), where A and B are valid strings, orIt can be written as the form of (A), where A is a valid string.So ...
Read MoreFind the Smallest Divisor Given a Threshold in C++
Suppose we have an array of integers called nums and an integer k, that is threshold value, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. We have to find the smallest divisor such that the result mentioned above is less than or equal to threshold value k. For example − if nums = [1, 2, 5, 9] and k = 6, then the output will be 5. We can get the sum as (1+2+5+9) = 17 when the divisor is 1. If the divisor is 4, then ...
Read MoreSequential Digits in C++
Suppose we have an integer, that has sequential digits if and only if each digit in the number is one more than the previous digit. We have to find a sorted list of all the integers in the range [low, high] inclusive that have sequential digits. So if the low = 100 and high = 300, then the output will be [123,234]To solve this, we will follow these steps −create one array resfor i in range 1 to nfor j := 1, until j + i – 1
Read MoreFind Elements in a Contaminated Binary Tree in C++
Suppose we have a binary tree. The rules of that tree is as follows −root.val == 0If treeNode.val is x and treeNode.left is not null, then treeNode.left.val = 2 * x + 1If treeNode.val is x and treeNode.right is not null, then treeNode.right.val = 2 * x + 2Now as the binary tree is contaminated. This indicates all val of the tree node have been changed to -1. We have to first recover the binary tree and then implement the FindElements class as follows −FindElements(TreeNode* root) Initializes the object with a contamined binary tree, we have to recover it first.bool ...
Read MoreDivide Array in Sets of K Consecutive Numbers in C++
Suppose we have an array of integers nums and a positive integer k, we have to find whether it's possible to divide this array into sets of k consecutive numbers. So we have to return True if its possible otherwise return False. So if the input is like [1, 2, 3, 3, 4, 4, 5, 6] and k = 4, then output will be true. This is because, we can divide the array such that [1, 2, 3, 4] and [3, 4, 5, 6]To solve this, we will follow these steps −Make one map m, set n := size of ...
Read MoreSearch Suggestions System in C++
Suppose we have an array of strings products and a string called searchWord. We want to design a module that suggests at most three product names from products list after each character of searchWord is typed. The suggested products should have common prefix with the searchWord. When there are more than three products with a common prefix return the three lexicographically minimums products. So we have to find the lists of the suggested products after each character of searchWord is typed.If the input is like: [ "mobile", "mouse", "moneypot", "monitor", "mousepad"], and the searchWord is “mouse”, then the output will ...
Read MoreSum of Mutated Array Closest to Target in C++
Suppose we have an integer array arr and a target value target, we have to find the integer value such that when we change all the integers larger than value in the given array will be equal to value, the sum of the array gets as nearest as possible to target. If they are same, then return the minimum such integer. So if the array is like [4, 9, 3] and target is 10, then the output will be 3 as using 3, the array will be [3, 3, 3], so the sum is 9, that is nearest element to ...
Read MoreBulb Switcher in C++
Suppose there are n bulbs that are initially switched off. We first turn on all the bulbs. Then, we turn off every second bulb. On the third round, we toggle every third bulb (turning on if it's off or turning off if it's on). Similarly, for the i-th round, we toggle every i bulb. For the n-th round, we only toggle the last bulb. So we have to find how many bulbs are on after n rounds. So if the input is 3, then the result will be 1. This is because −At first, the three bulbs are [off, off, ...
Read More