C++ Articles

Page 133 of 597

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

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 587 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

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

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 264 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

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

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

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 455 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

Count distinct points visited on the number line in C++

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

We are given a binary sequence of 0’s and 1’s. Also assume a person is sitting at a position or point stored in current_pos. Now starting from current_pos, if the binary sequence has 0 then he moves one step left ( current_pos - 1). If it’s 1 he moves one step right ( current_pos + 1). The goal is to find distinct positions or points he visited after the whole binary sequence is completed.We will solve this using the number of times a point is visited. If frequency is non-zero, increase count of distinct points.InputPath[]= “001100” current_pos=3OutputDistinct points visited on ...

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 508 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

Program for Centered Icosahedral Number in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 222 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

Count binary strings with k times appearing adjacent two set bits in C++

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

We are given with integers N and K. We have binary strings of length N, containing 0’s and 1’s only. The goal is to find the number of such strings of length N that have K consecutive 1’s. I.e, if N=3, and K=2, then count all 3-digit binary strings possible that have 2 consecutive 1’s.Example − 111, here adjacent 1’s appear twice ( K times ).In 011 and 110 adjacent 1’s appeared once only.We will solve this by storing the results of previous values.Taking 3D array count[x][y][z]. Where x is N, y is K and z is last digit of ...

Read More

Maximum number of contiguous array elements with same number of set bits in C++

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

We are given with an unsorted array of integer elements and the task is to calculate the two major things i.e.Elements with same number of set bitsAlso, Elements with the same set bits should be contiguous in nature.Inputint arr[] = { 5, 8, 1, 2, 9, 12}Output − Maximum number of contiguous array elements with same number of set bits are − 3Explanation − we will calculate the binary digits for the elements of an array and calculate their set bits.arr[0] = 5 => 0101 => total set bits are -: 2 arr[1] = 8 => 1000 => total set ...

Read More

Program for centered nonagonal number in C++

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

Given with a value ‘n’ and the task is to generate the centered nonagonal number for n and centered nonagonal series till n and display the results.What is centered nonagonal number?Centered nonagonal number contains the nonagonal layers formed by the dots and one corresponding dot in the center.Given above is the figure of centered nonagonal number 𝑁2.It can be calculated using the formula −$$Nc(n)=\frac{(3n-2)(3n-1)}{2}$$Input number: 20Output centered nonagonal number : 1711Input number: 10Output centered nonagonal series : 1 10 28 55 91 136 190 253 325 406AlgorithmStart Step 1→ declare function to calculate centered nonagonal number    int calculate_number(int num)       return ...

Read More
Showing 1321–1330 of 5,962 articles
« Prev 1 131 132 133 134 135 597 Next »
Advertisements