Articles on Trending Technologies

Technical articles with clear explanations and examples

Maximum Length of a Concatenated String with Unique Characters in C++

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

Suppose we have an array of strings arr. The string s is a concatenation of a sub-sequence of arr which have unique characters. Find the maximum possible length of s. If the input is like [“cha”, “r”, “act”, “ers”], then the output will be 6, possible solutions are “chaers” and “acters”.To solve this, we will follow these steps −make one method called ok() and this will take strings s and t. This will act like belowmake one map xfor i in range 0 to size of sincrease x[s[i]] by 1if x[s[i]] > 1, then return falsefor i in range 0 ...

Read More

Count all the numbers in a range with smallest factor as K in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 223 Views

In this tutorial, we will be discussing a program to find the numbers in a range with the smallest factor as K.For this we will be provided with a range [a,b]. Our task is to count the numbers in the given range who have their smallest factor as K.Example#include using namespace std; //checking if K is a prime bool if_prime(int k){    if (k

Read More

Shortest Path in Binary Matrix in C++

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

Suppose we have an N by N square grid, there each cell is either empty (0) or blocked (1). A clear path from top-left to bottom-right has length k if and only if it is composed of cells C_1, C_2, ..., C_k such that −Adjacent cells C_i and C_{i+1} are connected 8-directionally (So they are different and share an edge or corner)C_1 is at location (0, 0)C_k is at location (N-1, N-1)If C_i is located at (r, c), then grid[r, c] is empty or contains 0We have to find the length of the shortest such clear path from top-left to ...

Read More

Reconstruct a 2-Row Binary Matrix in C++

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

Suppose we have the following details of a matrix with n columns and 2 rows −Matrix elements will be either 0 or 1Sum of elements of the 0-th(upper) row is given as upper.Sum of elements of the 1-st(lower) row is given as lower.Sum of elements in the i-th column(0-indexed) is colsum[i], where colsum is given as an integer array with length n.The task is to reconstruct the matrix with upper, lower and colsum. We have to find it as a 2D integer array. If there are more than one valid solution, any of them will be accepted. If there is ...

Read More

Count all the columns in a matrix which are sorted in descending in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 228 Views

In this tutorial, we will be discussing a program to find the number of columns in a matrix which are sorted in descending.For this we will be provided with a matrix. Our task is to count the number of columns in the matrix having elements sorted in descending order.Example#include #define MAX 100 using namespace std; //counting columns sorted in descending order int count_dcolumns(int mat[][MAX], int r, int c){    int result = 0;    for (int i=0; i0; j--)          if (mat[i][j-1] >= mat[i][j])             break;       if (c ...

Read More

Encode Number in C++

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

Suppose we have a non-negative integer n, and we have to find the encoded form of it. The encoding strategy will be as follows −NumberEncoded number0“”1“0”2“1”3”00”4”01”5”10”6”11”7”000”So if the number is 23, then result will be 1000, if the number is 54, then it will be 10111To solve this, we will follow these steps −Create one method called bin, this will take n and k, this method will act like belowres := empty stringwhile n > 0res := res + the digit of n mod 2n := n /2reverse the number reswhile x > length of resres := prepend 0 with ...

Read More

Count all sorted rows in a matrix in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 301 Views

In this tutorial, we will be discussing a program to find the number of all sorted rows in a matrix.For this we will be provided with m*n matrix. Our task is to count all the rows in the given matrix that are sorted either in ascending or descending order.Example#include #define MAX 100 using namespace std; //counting sorted rows int count_srows(int mat[][MAX], int r, int c){    int result = 0;    for (int i=0; i

Read More

Smallest Common Region in C++

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

Suppose we have some lists of regions where the first region of each list includes all other regions in that list. basically, if a region X contains another region Y then, X is larger than Y. Also by definition a region X contains itself. So if we have two regions r1 and r2, we have to find the smallest region that contains both of them. So if we have r1, r2 and r3 such that r1 includes r3, it is guaranteed there is no r2 such that r2 includes r3. So if the input is like [["Earth", "North America", "South ...

Read More

Count all sub-arrays having sum divisible by k

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 311 Views

In this tutorial, we will be discussing a program to find the number of sub-arrays having sum divisible by k.For this we will be provided with an array and a value k. Our task is to find the number of sub arrays that are having their sum as equal to the given value k.Example#include using namespace std; //counting subarrays with k sum int count_subarray(int arr[], int n, int k){    int mod[k];    memset(mod, 0, sizeof(mod));    int cumSum = 0;    for (int i = 0; i < n; i++) {       cumSum += arr[i];   ...

Read More

Greatest Sum Divisible by Three in C++

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

Suppose we have an array nums of integers, we need to find the maximum possible sum of elements of the given array such that it is divisible by three. So if the input is like [3, 6, 5, 1, 8], then the output will be 18, as the subarray is [3, 6, 1, 8], and the sum is 18, that is divisible by 3.To solve this, we will follow these steps −n := size of nums arraycreate one 2d array dp of size (n + 1) x 3set dp[0, 0] := 0, dp[0, 1] := -inf, dp[0, 2] := inffor ...

Read More
Showing 27881–27890 of 61,297 articles
Advertisements