C++ Articles

Page 106 of 597

Minimum Bracket Addition in C++

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

Suppose we have a string s containing only '(' and ')', we have to find the minimum number of brackets that can be inserted to make the string balanced.So, if the input is like "(()))(", then the output will be 2 as "(()))(", this can be made balanced like "((()))()".To solve this, we will follow these steps −:= 0, cnt := 0for initialize i := 0, when i < size of s, update (increase i by 1), do −if s[i] is same as '(', then −(increase o by 1)Otherwiseif o is non-zero, then −(decrease o by 1)Otherwise(increase cnt by 1)return ...

Read More

Count the number of pairs that have column sum greater than row sum in C++

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

We are given a matrix of size NXN. The goal is to find the count of all valid pairs of indexes (i, j) such that the sum elements of column j is greater than the sum of elements of row i.We will do this by traversing the matrix and calculate sums of elements of each row and column.Store sum of elements of each in rowsum[N] and sum of elements of each column in colsum[N].Now make pairs of rowsum[i] and colsum[j] and check if colsum[j]>rowsum[i]. If true increment count for such pairs.Let’s understand with examples.Input-: matrix= {    { 1, 2, ...

Read More

Shortest Majority Substring in C++

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

Suppose we have a lowercase alphabet string s, we have to find the length of the shortest substring (minimum length is 2) such that some letter appears more than the other letters combined. If we cannot find any solution, then return -1.So, if the input is like "abbbcde", then the output will be 2, the substring "bb" has minimum length and this appears more than other letters.To solve this, we will follow these steps −Define a function ok(), this will take an array cnt, total := 0, maxVal := 0for each element it in cnt, dototal := total + itmaxVal ...

Read More

Count the number of operations required to reduce the given number in C++

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

We are given with a positive integer K and an array Ops[] which contains integers. The goal is to find the number of operations required to reduce K such that it becomes less than 0. Operations are −First operation is K + Ops[0], first element added to KAfter 1. Add Ops[i] to K until K

Read More

Program to find correlation coefficient in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 2K+ Views

In this tutorial, we will be discussing a program to find correlation coefficient.For this we will be provided with two arrays. Our task is to find the correlation coefficient denoting the strength of the relation between the given values.Example#include using namespace std; //function returning correlation coefficient float find_coefficient(int X[], int Y[], int n){    int sum_X = 0, sum_Y = 0, sum_XY = 0;    int squareSum_X = 0, squareSum_Y = 0;    for (int i = 0; i < n; i++){       sum_X = sum_X + X[i];       sum_Y = sum_Y + Y[i];     ...

Read More

Count number of ordered pairs with Even and Odd Product in C++

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

We are given an array of n positive numbers.The goal is to count the ordered pairs (arr[x], arr[y]) with the product of arr[x] and arr[y] is even or odd. Pair ( arr[i], arr[j] ) and ( arr[j], arr[i] are counted as separate.We will traverse the array using two for loops for each number of pairs. Now calculate product, if it is even increment count by 2 for even products else increment count by 2 for odd products.Let’s understand with examples.Input− Arr[]= { 1, 1, 2, 3 } N=4Output − Count of even product pairs: 6 Count of odd product pairs: ...

Read More

Program to find count of numbers having odd number of divisors in given range in C++

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

In this tutorial, we will be discussing a program to find the count of numbers having odd number of divisors in a given range.For this we will be provided with the upper and lower limits of the range. Our task is to calculate and count the number of values having an odd number of divisors.Example#include using namespace std; //counting the number of values //with odd number of divisors int OddDivCount(int a, int b){    int res = 0;    for (int i = a; i

Read More

Count number of ordered pairs with Even and Odd Sums in C++

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

We are given an array of n positive numbers.The goal is to count the ordered pairs (arr[x], arr[y]) with the sum of arr[x] and arr[y] is even or odd. Pair ( arr[i], arr[j] ) and ( arr[j], arr[i] are counted as separate.We will traverse the array using two for loops for each number of pairs. Now calculate sum, if it is even increment count by 2 for even sums else increment count by 2 for odd sums.Let’s understand with examples.Input− Arr[]= { 1, 1, 2, 3 } N=4Output− Count of even product sums − 6 Count of odd sum pairs ...

Read More

Program to find covariance in C++

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

In this tutorial, we will be discussing a program to find covariance.For this we will be provided with two sets of random variables. Our task is to calculate their covariance i.e, the measure of how much those two values differ together.Example#include using namespace std; //function to find mean float mean(float arr[], int n){    float sum = 0;    for(int i = 0; i < n; i++)    sum = sum + arr[i];    return sum / n; } //finding covariance float covariance(float arr1[], float arr2[], int n){    float sum = 0;    for(int i = 0; i < ...

Read More

Count number of substrings with numeric value greater than X in C++

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

We are given a string of numbers 0 to 9. The string represents a decimal number. The goal is to find all substrings that represent a decimal number which is greater than number X. Condition is that substring should not start with 0. I.e in “2021”, “02”, “021”. “0” will not be included.We will do this by checking the first value of all substrings, if it is greater than 0 then start making substrings from that index by converting them into integers using stoi(). If substring>X increment count.Let us understand with examples.Input − str=”123” X=12Output − Count of number of ...

Read More
Showing 1051–1060 of 5,962 articles
« Prev 1 104 105 106 107 108 597 Next »
Advertisements