Found 7197 Articles for C++

Absolute distinct count in a sorted array in C++?

Akansha Kumari
Updated on 22-Jul-2025 15:53:47

368 Views

An array is a data structure that is used to store elements of the same data type, where a sorted array, in which the elements are arranged in ascending or descending order. The absolute distinct count gives the count of absolute (non-negative or unsigned) values of distinct (unique) elements present in an array. In this article, we will learn how to find the count of absolute values of all distinct elements in the given sorted array in C++. Consider the following input and output scenarios to under the problem better: Scenario 1 Input: [-5, -2, 1, 2, 5, 5, 6, ... Read More

Anti Clockwise spiral traversal of a binary tree in C++?

sudhir sharma
Updated on 03-Oct-2019 13:02:36

173 Views

Anti-Clockwise spiral traversal of a binary tree is traversing the elements of a tree in such a way that if traversed they make a spiral but in reverse order. The following figure shows how a anti-clockwise spiral traversal of binary tree.The Algorithm defined for spiral traversal of a binary tree works in the following way −Two variables i and j are initialized and values are equated as i = 0 and j = height of variable. A flag is used to check while section is to be printed. Flag is initially is set of false. A loop working till i ... Read More

Absolute Difference of even and odd indexed elements in an Array in C++?

sudhir sharma
Updated on 01-Aug-2025 16:58:01

980 Views

An array in C++ is a collection of elements of the same data type stored in contiguous memory locations. Arrays provide a way to store multiple values using a single variable name, and each element can be accessed using an index starting from 0. Even and Odd Indexed Elements in an Array Each element in an array has an index. Indexes that are divisible by 2 (like 0, 2, 4, etc.) are known as even indexes, while indexes that are not divisible by 2 (like 1, 3, 5, etc.) are referred to as odd indexes. For example, in the ... Read More

Adding one to number represented as array of digits in C++?

Nishu Kumari
Updated on 29-Jul-2025 14:22:35

311 Views

In this problem, we are given a number in the form of an array, and each digit of the number is stored at one index of the array, and the most significant digit comes first. Our task is to add 1 to this number and return the updated number in the same array format. Let's understand it with some example scenarios. Scenario 1 Input: Input_arr[] = {2, 6, 1} Output: 262 Explanation: The array represents 261. Adding 1 gives 261 + 1 = 262. Scenario 2 Input: input_arr[] = {5, 9, 9, 9} Output: 6000 Explanation: The array ... Read More

C++ program to find whether there is a path between two cells in matrix

Ayush Gupta
Updated on 03-Oct-2019 12:28:49

189 Views

In this article, we will be discussing a program to find whether there exists a path between two cells in a given matrix.Let us suppose we have been given a square matrix with possible values 0, 1, 2 and 3. Here, 0 means Blank Wall1 means Source2 means Destination3 means Blank CellThere can only be one Source and Destination in the matrix. The program is to see if there’s a possible path from Source to Destination in the given matrix moving in all four possible directions but not diagonally.Example Live Demo#include using namespace std; //creating a possible graph from given array ... Read More

C++ program to find whether only two parallel lines contain all coordinates points or not

Ayush Gupta
Updated on 03-Oct-2019 12:21:59

393 Views

In this article, we will be discussing a program to find whether only two parallel lines can hold all the given coordinates points.For this we will be given an array, such that the coordinates will be (i, arr[i]). Let us suppose we are given an array, arr = {2, 6, 8, 12, 14}Then we can have these points on two parallel lines, the first line containing (1, 2), (3, 8) and (5, 14). The second line having the rest coordinates i.e (2, 6) and (4, 12).This problem can be solved by comparing the slopes of the lines made by the ... Read More

C++ program to find the vertex, focus and directrix of a parabola

Ayush Gupta
Updated on 03-Oct-2019 12:18:03

178 Views

In this article, we will be discussing a program to find the vertex, focus and directrix of a parabola when the coefficients of its equation is given.Parabola is a curve whose all points on the curve are equidistant from a single point called focus.As we know the general equation for a parabola isy = ax2 + bx + cFor this equation, the following are defined as :Vertex -(-b/2a, 4ac - b2/4a) Focus - (-b/2a, 4ac - b2+1/4a) Directrix - y = c - (b2 +1)4aExample Live Demo#include using namespace std; void calc_para(float a, float b, float c) {    cout ... Read More

C++ program to find the probability of a state at a given time in a Markov chain

Ayush Gupta
Updated on 03-Oct-2019 12:15:35

476 Views

In this article, we will be discussing a program to find the probability of reaching from the initial state to the final state in a given time period in Markov chain.Markov chain is a random process that consists of various states and the associated probabilities of going from one state to another. It takes unit time to move from one state to another.Markov chain can be represented by a directed graph. To solve the problem, we can make a matrix out of the given Markov chain. In that matrix, element at position (a, b) will represent the probability of going ... Read More

C++ program to find the Parity of a number efficiently

Ayush Gupta
Updated on 03-Oct-2019 12:10:44

2K+ Views

In this article, we will be discussing a program to find the parity of a given number N.Parity is defined as the number of set bits (number of ‘1’s) in the binary representation of a number.If the number of ‘1’s in the binary representation are even, the parity is called even parity and if the number of ‘1’s in the binary representation is odd, the parity is called odd parity.If the given number is N, we can perform the following operations.y = N ^ (N >> 1)y = y ^ (y >> 2)y = y ^ (y >> 4)y = ... Read More

C++ program to find the number of triangles amongst horizontal and vertical line segments

Ayush Gupta
Updated on 03-Oct-2019 12:07:05

158 Views

In this article, we will be discussing a program to find the number of triangles that can be formed by joining the intersection points of the given horizontal and vertical line segments.For example, let us say we had been given the following line segments. In this we have 3 intersection points. So the number of triangles that can be formed using these points will be 3C2.   | ---|--------|--    |        |    |  --|---|    |        |We will be following the Sweep Line Algorithm. We will be storing all the values of the line ... Read More

Advertisements