C++ Articles

Page 116 of 597

Count Derangements (Permutation such that no element appears in its original position) in C++

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

Derangement is permutation of N numbers such that no number appears at original position. For example one possible derangement of { 1, 2, 3 } is { 2, 1, 3 }. No element in this is at its original position. The goal here is to count the possible derangements for N numbers.We will do this using a recursive solution. For following no. of elements −N=0, no derangement, return 1N=1, only one number, return 0N=2, only one swap of position possible, { 1, 2 } → { 2, 1 }, return 1N=3, 2 possible permutations eg, { 1, 2, 3 } ...

Read More

Sum of the natural numbers (up to N) whose modulo with K yield R in C++

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

In this problem, we are given three numbers N, K and R. Our task is to create a program to find the Sum of the natural numbers (up to N) whose modulo with K yield R.We will add all the numbers less than N that satisfy the following condition, i%K == R.Let’s take an example to understand the problem, Input N = 14, K = 4, R = 1Output 28Explanation − All the numbers less than N, that given 1 as remainder when divided by 4 are 1, 5, 9, 13.To solve this problem, we will loop from R to N, and ...

Read More

Probability of getting more value in third dice throw in C++

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

Given three players A, B, C throwing dice, we have to find the probability of the C throwing the dice and the number scored by C is higher than both A and B.To check the probability of getting more value, we have to keep in mind that the value of the third dice throw is higher than the previous two.Like A thrown the dice and score 2 and B thrown the dice and scored 3 so the probability of C getting higher value is 3/6 = 1/2, because there are only 3 values which can be higher than the A ...

Read More

Probability that the pieces of a broken stick form a n sided polygon in C++

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

We are given with the stick of any length and that stick can be broken randomly into n pieces which can be of type integer or floating point and the task is to find whether the broken pieces can form a n sided polygon.We can calculate the probability by applying the formula$$P(E^{\prime})=1-P(E)=1-\frac{n}{2^{n-1}}$$Where, n is the number of pieces generated by breaking the stick into parts.Input length = 10 , pieces = 4Output probability is : 0.5Explanation − given with length of size 10 cm and it is broken into 4 partsInput length = 5 , pieces = 3Output probability is : 0.25Explanation − given ...

Read More

Product of all pairwise consecutive elements in an Arrays in C++

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

Given an array arr[n] of n number of integers, the task is to find the product of all pairwise consecutive elements.Consecutive elements in an array arr[] are, if we are at ith element, i.e. arr[i] then its consecutive element will be either arr[i+1] or arr[i-1], so the product will be arr[i] * arr[i+1] or arr[i] * arr[i-1].Input arr[] = {1, 2, 3, 4}Output 2, 6, 12Explanation Splitting into pairs {1, 2}, {2, 3}, {3, 4} Their results will be 1*2 = 2, 2*3 = 6, 3*4 = 12Input arr[] = {9, 5, 1, 2, 6, 10}Output 45, 5, 2, 12, 60Explanation Splitting into pairs {9, 5}, ...

Read More

Container with Most Water in C++

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

We are given an array of height of the walls of the container. The goal is to find the container that can contain the maximum volume of water. As heights of walls are elements of an array, the distance between them is considered as width between two walls. For example walls of height Arr[i] and Arr[j] have j-i width between them ( 0

Read More

Maximum and minimum isolated vertices in a graph in C++

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

We are given with the number of edges Noe and number of vertices Nov. The goal is to find the minimum and maximum number of isolated vertices that are possible in such graphs which have no edges and No vertices count.An isolated vertex is the one that has no edge connected to it.For minimum isolated verticesWe will make sure that every edge is isolated. ( No two edges have common vertices ) Each edge requires only 2 vertices. So ,count of non isolated vertices = 2 * no. of edgescount of isolated vertices = total vertices - count of non ...

Read More

Product of all the elements in an array divisible by a given number K in C++

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

Given an array arr[n] with n number of integers and another integer k, the task is to find the product all the elements of arr[] which are divisible by k.To solve the problem we have to iterate every element of the array and find whether it is completely divisible by the number k and then product all the elements and store it into a variable. Like we have an array arr[] = {1, 2, 3, 4, 5, 6 } and assuming we have k = 2 so the numbers in the array which are divisible by 2 are 2, 4, ...

Read More

Maximise the number of toys that can be purchased with amount K in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 2K+ Views

We are given with the prices of toys in the form of an array and an amount K in hand. The goal is to purchase the maximum no. of toys with that amount. Each element of the array is a price of a single toy, so no. of toys is no. of elements. We will sort the array of prices in ascending order so that maximum toys of less prices can be purchased first followed by costly toys.Inputtoyprices[]= { 10, 20, 12, 15, 50, 30 } K=50OutputMaximum no. of toys that can be purchased : 3Explanation − Sorting prices of ...

Read More

Program for Centered Icosahedral Number in C++

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

Given with a value ‘n’ and the task is to generate the centered Icosahedral number for n and centered Icosahedral series till n and display the results.What is centered Icosahedral number?Centered Icosahedral number is a centered number used for representing an icosahedrons(it is a polyhedron figure with 20 faces).The first few centered icosahedral number series till n = 1000 are −1, 13, 55, 147, 309, 561, 923Centered Icosahedral number can be calculated using the formula −$$(2n+1)\times\frac{5n^{2}+5n+3}{3}$$Input number: 20Output Centered Icosahedral Number is : 28741Input number: 12Output Centered Icosahedral Number is : 6525AlgorithmStart Step 1→ declare function to calculate centered iscosahedral number    int calculate(int ...

Read More
Showing 1151–1160 of 5,961 articles
« Prev 1 114 115 116 117 118 597 Next »
Advertisements