C++ Articles

Page 36 of 597

Count of words whose i-th letter is either (i-1)-th, i-th, or (i+1)-th letter of given word in C++

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

We are given a string str[] as input. The goal is to count the words from str[] that have the same length as str[] and have positions of letters such that ith letter is replaced with letter at position (i1) or (i) or (i+1).For the first letter replacement will be from position i or i+1For the last letter replacement will be from position i-1 or i.Let us understand with examples.Input − str[] = “TPP”Output − Count of words whose i-th letter is either (i-1)-th, i-th, or (i+1)-th letter of given word are − 4Explanation Replacing T by T (i)th or 1st ...

Read More

Program to find minimum number of roads we have to make to reach any city from first one in C++

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

Suppose we have two lists costs_from and costs_to of same size where each index i represents a city. It is making a one-way road from city i to j and their costs are costs_from[i] + costs_to[j]. We also have a list of edges where each edge contains [x, y] indicates there is already a one-way road from city x to y. If we want to go to any city from city 0, we have to find the minimum cost to build the necessary roads.So, if the input is like costs_from = [6, 2, 2, 12] costs_to = [2, 2, 3, ...

Read More

Program to count number of operations to convert binary matrix to zero matrix in C++

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

Suppose we have a binary matrix. Now consider an operation where we take one cell and flip it and all its neighboring cells (up, down, left, right). We have to find the minimum number of operations required such that matrix contains only 0s. If there is no solution, then return -1.So, if the input is like0010then the output will be 3.To solve this, we will follow these steps −Define an array dir of size: 4 x 2 := {{1, 0}, {0, 1}, { - 1, 0}, {0, - 1}}const int inf = 10^6Define a function getPos(), this will take i, ...

Read More

Count of suffix increment/decrement operations to construct a given array in C++

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

We are given a target array arr[] containing positive integers. The goal is to construct the target array arr[] using an initial array with all 0s. The operations that can be applied on a given empty array with all 0s will suffix increment/decrement operations.If we choose any index say i, then in case of suffix increment operation we will add 1 to all elements from index i till last index.In case of suffix decrement operation we will subtract 1 from all elements from index i till last index.Let us understand with examplesInput − arr[]= { 1, 2, 3 }Output − ...

Read More

Entringer Number in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 203 Views

Entringer Number is a special number which is equal to the number of permutations of {1, 2, 3, … n+1}, starting with K+1 which is updated by decreasing then increasing the values alternatively.The value of Entringer Number is formulated using, The recurrence relation, E(n, k) = E(n, k-1) + E(n-1, n-k)The base value is, E(0, 0) = 1E(n, 0) = 0We can find the Entringer number using, Let’s take an example to see valuesN = 5, k = 3E(5, 3) = 14Program to illustrate the working of our solution, Example#include using namespace std; int EntringerNumber(int n, int k) { ...

Read More

Count of total anagram substrings in C++

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

We are given a string str[] as input. The goal is to count the number of anagram substrings present in str[]. Two strings are anagrams of each other if they contain the same number of characters and all characters occur in both. The order of characters can be different.“abc” is an anagram of “cba”, “bca” etc.Let us understand with examples.Input − str[] = “abccb”Output − Count of total anagram substrings are − 4Explanation − Anagrams are − (b, b), (c, c), (bc, cb), (bcc, ccb)Input − str = “aaa”Output − Count of total anagram substrings are − 4Explanation − Anagrams ...

Read More

Equable Shapes in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 211 Views

In this problem, we are given the coordinates of a polygon. Our task is to create a program to check whether the given polygon is equable or not.Equable Shape is the shape whose perimeter is equal to the area of the shape.Let’s take an example to understand the problem, Input: polygon[][] = {{0, 0}, {5, 7}, {2, 0}}Output: Not equableExplanation: Perimeter = 18.21Area = 7Solution Approach:The solution to the problem lies in find the area and perimeter of the shape and then compare both of them to check weather the given shape is an equable shape or not.Finding a perimeter using the coordinates is ...

Read More

Count of triangles with total n points with m collinear in C++

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

We are given two variables n and m representing the number of points on a 2D plane. Out of n points, m points are collinear. The goal is to find the number of triangles that can be formed using these n points.Collinear points − The points that lie on the same line are called collinear. Points A and B are collinear.Given n=4 (A, B, C, D ) , m=2 (A, B)Number of triangles −Choosing any three points out of 4 = 4C3But collinear points cannot form triangle so remove possible triangles that will be counted above = 2C3Total triangles= 4C3 ...

Read More

Equal Sum and XOR in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 922 Views

In this problem, we are given an integer n. Our task is to create a program to find the count of integers from i = 0 to n, where sum is equal to XOR i.e. (n+i) = (n^i).Let’s take an example to understand the problem, Input:  n = 4Output: 4Explanation: Considering all values of i from 0 to n, i = 0,  4 + 0 = 4, 4^0 = 4i = 1,  4 + 1 = 5, 4^1 = 5i = 2,  4 + 2 = 6, 4^2 = 6i = 3,  4 + 3 = 7, 4^3 = 7i = 4, ...

Read More

Count pairs formed by distinct element sub-arrays in C++

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

We are given an array arr[] containing integer elements. The goal is to find the count of pairs that can be formed by elements of sub-arrays of arr[] such that each subarray has only distinct elements. If the array is [ 1, 2, 2, 3, 3 ] then subarrays with distinct elements only will be [ 1, 2 ] and [ 2, 3 ]. And pairs will be (1, 2) and (2, 3) hence count of pairs is 2.Let us understand with examplesInput − arr[] = {1, 2, 5, 3 }Output − Count of pairs formed by distinct element sub-arrays ...

Read More
Showing 351–360 of 5,961 articles
« Prev 1 34 35 36 37 38 597 Next »
Advertisements