C++ Articles

Page 250 of 597

Find sum of even index binomial coefficients in C++

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

Consider we have a number n, we have to find the sum of even indexed binomial coefficients like $$\left(\begin{array}{c}n\ 0\end{array}\right)+\left(\begin{array}{c}n\ 2\end{array}\right)+\left(\begin{array}{c}n\ 4\end{array}\right)+\left(\begin{array}{c}n\ 6\end{array}\right)+...\left(\begin{array}{c}4\ 0\end{array}\right)+\left(\begin{array}{c}4\ 2\end{array}\right)+\left(\begin{array}{c}4\ 4\end{array}\right)++=1+6+1=8$$So here we will find all the binomial coefficients, then only find the sum of even indexed values.Example#include using namespace std; int evenIndexedTermSum(int n) {    int coeff[n + 1][n + 1];    for (int i = 0; i

Read More

Find k-th bit in a binary string created by repeated invert and append operations in C++

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

Suppose we have a binary string s, initially this is say “0”. Now in each iteration invert it, and append it, thus after nth iteration, we will find the kth bit. Suppose the number of iteration is 4, and k = 7, so it will be −IterationValue (Initially 0)1012011030110100140110100110010110so 7th bit is 1.In each iteration, find complement, and append, thus after nth iteration, finds kth bitExample#include using namespace std; string getComplement(string bin){    string temp = "";    for(int i= 0; i

Read More

Find length of period in decimal value of 1/n in C++

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

Suppose we have a number n. We have to find the length of period in decimal value of 1/n. So if the value of n is 7, then 1/7 = 0.142857142857… That part in bold letters are repeating. So here the length of period is 6.For a number n, there can be n distinct remainders in the output, but the period may not begin from the first remainder as some initial remainders are non-repeating. So we have to make sure that a remainder from period is picked, start from (n+1)th remainder, and start looking for the next occurrence. The distance ...

Read More

Find maximum array sum after making all elements same with repeated subtraction in C++

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

Suppose we have an array of n elements. Find the maximum possible sum of all elements such that all the elements are same. Only operation that is allowed is choosing any two elements and replacing the larger of them by the absolute difference of the two. Suppose elements are like [9, 12, 3, 6]. Then the output will be 12. So at first replace A[1] with A[1] – A[3] = 12 – 6 = 6. So now elements are [9, 6, 3, 6], then replace A[3] with A[3] – A[2] = 6 – 3 = 3. So the elements are ...

Read More

Find Maximum Sum Strictly Increasing Subarray in C++

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

Suppose we have an array of n integers. Find the max sum of strictly increasing subarrays. So if the array is like [1, 2, 3, 2, 5, 1, 7], the sum is 8. In this array there are three strictly increasing sub-arrays these are {1, 2, 3}, {2, 5} and {1, 7}. The max sum sub-array is {1, 7}To solve this problem, we have to keep track of max sum and the current sum. For each element arr[i] if this is larger than arr[i – 1], then we add this to the current sum, otherwise arr[i] is the starting point ...

Read More

Find maximum volume of a cuboid from the given perimeter and area in C++

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

Suppose we have the area A and a perimeter P, now we have to find what will be the maximum volume that can be made in form of cuboid from given perimeter and surface area. So when the P is 24 and A is 24, then the output will be 8.As we know for given perimeter of cuboid P = 4(length + breadth + Depth), for area, it will be A = 2(length* breadth + breadth*Depth + length *Depth), and the volume is V = (length* breadth*Depth)Example#include #include using namespace std; float maxVolumeCuboid(float Peri, float Area) {    float length ...

Read More

Find minimum moves to reach target on an infinite line in C++

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

Suppose we have a number position in infinite number line. (-inf to +inf). Starting from 0, we have to reach to the target by moving as described. In ith move, we can go i steps either left or right. We have to find minimum number of moves that are required. Suppose the target is 2, so minimum steps will be 3. From 0 to 1, from 1 to -1 and from -1 to 2.To solve this problem, we have some important points to remember. If the target is negative, then just take this as positive, as the number line is ...

Read More

Find minimum number of Log value needed to calculate Log upto N in C++

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

As we know that log(x*y) = log(x) + log(y). So we will see what are the minimum number of log values are required to calculate all log values from 1 to N. So if N is 6, then output will be 3, as from log(1) to log(6), there are three log values are required except log(1). As log(1) is always 0, then ignore it, now for log(2) and log(3), we have to find. After that for log(4) this is log(2)+ log(2), but value of log(2) is known, so we do not calculate this again, for log(5), we need to calculate. ...

Read More

Find minimum number to be divided to make a number a perfect square in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 2K+ Views

Suppose we have a number N. We have to find minimum number that divide N to make it perfect square. So if N = 50, then minimum number is 2, as 50 / 2 = 25, and 25 is a perfect square.A number is perfect square if it has even number of different factors. So we will try to find the prime factors of N, and find each prime factor power. Find and multiply all prime factors whose power is odd.Example#include #include using namespace std; int findMinimumNumberToDivide(int n) {    int prime_factor_count = 0, min_divisor = 1;    while (n%2 ...

Read More

Find minimum radius such that atleast k point lie inside the circle in C++

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

Suppose we have some points, and one integer k. We have to find minimum radius of a circle whose center is at (0, 0) to cover k points. So if the points are like (1, 1), (-1, -1), (1, -1), and k = 3, then radius will be 2.Here we will find the Euclidean distance between each point and (0, 0), then sort the distances and return the kth element after sorting.Example#include #include using namespace std; struct point{    int x, y; }; int minRadius(int k, point points[], int n) {    int dist[n];    for (int i = 0; ...

Read More
Showing 2491–2500 of 5,961 articles
« Prev 1 248 249 250 251 252 597 Next »
Advertisements