Found 7197 Articles for C++

C/C++ Program to the Count Inversions in an array using Merge Sort?

sudhir sharma
Updated on 19-Aug-2019 07:40:46

287 Views

The Count of inversions that take place to Sort the given array is known as inversion count. the inversion problem is a classical problem that can be solved using the merge sort Algorithm. in this problem v we will count all elements more than it to its left and add the count to output. ThisLogic is done inside merge function of merge sort.For understanding the topic better let us take an example. Let us consider two sub-arrays involved in merge process -  Input: arr[] = { 1, 9, 6, 4, 5} Output: Inversion count is 5ExplanationInversion count of an arrayGiven an ... Read More

C/C++ Program to check whether it is possible to make the divisible by 3 number using all digits in an array?

sudhir sharma
Updated on 19-Aug-2019 07:35:15

402 Views

To check whether a number is divisible by 3, we add all the digits of the number and then calculate that the sum is divisible by 3 or not. In this problem, there is an array of integers arr[], and we have to check if a number formed with these number is divisible by 3. If the number formed is divisible then print ‘yes’ else print ‘no’Input: arr[] = {45, 51, 90} Output: YesExplanationconstruct a number which is divisible by 3, for example, 945510.So the answer will be Yes Find the remainder of the sum when divided by 3 true ... Read More

C/C++ Program for the Triangular Matchstick Number?

sudhir sharma
Updated on 19-Aug-2019 07:33:21

220 Views

A triangle that is made by using matchsticks arrange to make an equilateral triangle, this is called the triangular matchstick number. Triangular matchstick number is the number of matchsticks required to make the matchstick triangle.In this problem, we have the number is the floor of a matchstick pyramid, X. and our task is to write a program to print the total minimum number of matchstick required to form a pyramid of matchsticks of x floors.Let's look at an example that will make the concept more clear, Input: 7 Output: 84ExplanationThis is an extension of triangular numbers. For integer X, the ... Read More

C/C++ Program for the Odd-Even Sort (Brick Sort)?

sudhir sharma
Updated on 19-Aug-2019 07:26:35

493 Views

The odd-even sword also known as the brick sort is a similar sorting technique, like bubble sort. This sorting technique is subdivided into 2 phases odd phase and even phase, Both these phases simultaneously at every iteration until all the elements get sorted.The Odd phase of this programming technique works as a bubble sort but only on the elements that have an odd index.Similarly, the even phase works only on the elements that have an even index.For making this concept more clear let's take an example :Input: a[]={3, 5, 7, 6, 1, 4, 2} Output: 1 2 3 4 5 ... Read More

C/C++ Program for Number of solutions to Modular Equations?

sudhir sharma
Updated on 19-Aug-2019 07:17:42

140 Views

We have an n number of coins and we have to French the coin way that it makeup Pyramid of maximum height. We will arrange the first coin in First row second and third coin in the second row and so onIn the given diagram, we make pyramid 6 of coins having a height of 3. We cannot make height 4 but we will need 10 coins. It is simple to get the height by using this formula;H = {(-1+ √(1+8N))/2}Input: n = 10 Output: Height of pyramid: 4ExplanationHeight by using this formulaH = {(-1+ √(1+8N))/2}Example#include #include using ... Read More

C/C++ Program for Finding the vertex, focus and directrix of the parabola?

sudhir sharma
Updated on 19-Aug-2019 07:14:05

397 Views

A set of points on a plain surface that forms a curve such that any point on that curve is equidistant from a point in the center (called focus) is a parabola.The general equation for the parabola isy = ax2 + bx + cThe vertex of a parabola is the coordinate from which it takes the sharpest turn whereas a is the straight-line used to generate the curve.Focus is the point with is equidistant from all points of the parabola.Here, we will find the vertex, focus, and directrix of a parabola. There is a mathematical formula that finds all these ... Read More

Addition of two number using ‘-‘ operator?

sudhir sharma
Updated on 16-Aug-2019 12:05:43

3K+ Views

Operator overloading is an important concept in C++. It is a type of polymorphism in which an operator is overloaded to give user-defined meaning to it. The overloaded operator is used to perform the operation on the user-defined data type. For example, '+' operator can be overloaded to perform addition on various data types, like for Integer, String(concatenation), etc.Input10 20 20 30Output30 50ExplanationTo perform addition of two numbers using ‘-‘ operator by Operator overloading. Binary operators will require one object as an argument so they can perform the operation. If we are using Friend functions here then it will need ... Read More

C++ Program for Dijkstra’s shortest path algorithm?

sudhir sharma
Updated on 09-Aug-2019 13:08:53

16K+ Views

Dijkstra's algorithm (or Dijkstra's Shortest Path First algorithm, SPF algorithm) is an algorithm for finding the shortest paths between nodes in a graph, which may represent, for example, road networks. The algorithm creates a tree of shortest paths from the starting vertex, the source, to all other points in the graph.Dijkstra’s algorithm finds a shortest path tree from a single source node, by building a set of nodes that have minimum distance from the source.The graph has the following−vertices, or nodes, denoted in the algorithm by v or u.weighted edges that connect two nodes: (u, v) denotes an edge, and ... Read More

C/C++ Program for nth Catalan Number?

sudhir sharma
Updated on 13-Aug-2019 06:45:25

600 Views

Catalan numbers are a sequence of numbers. Catalan numbers form a sequence of natural numbers that occur in various counting problems, often involving recursively-defined objects.Cn is the number of Dyck words of length 2n. A Dyck word is a string consisting of n X's and n Y's such that no initial segment of the string has more Y's than X's. For example, the following are the Dyck words of length 6XXXYYY XYXXYY XYXYXY XXYYXY XXYXYY.Re-interpreting the symbol X as an open parenthesis and Y as a close parenthesis, Cn counts the number of expressions containing n pairs of parentheses which ... Read More

Concatenate a string given number of times in C++ programming

sudhir sharma
Updated on 09-Aug-2019 12:24:08

790 Views

A program to concatenate a string a given number of times will run the string concatenate method n number of times based on the value of n.The result would be string repeated a number of times.Examplegiven string: “ I love Tutorials point” n = 5OutputI love Tutorials pointI love Tutorials pointI love Tutorials pointI love Tutorials point I love Tutorials pointAfter seeing the output, it is clear that what the function will do.Example#include #include using namespace std; string repeat(string s, int n) {    string s1 = s;    for (int i=1; i

Advertisements