C++ Articles

Page 78 of 597

Queries to return the absolute difference between Lth smallest number and the R-th smallest number in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 169 Views

In this tutorial, we will be discussing a program to find queries to return the absolute difference between L-th smallest number and the R-th smallest number.For this we will be provided with an array containing integers and Q queries. Our task is to find the absolute difference between the indices of Lth smallest and Rth smallest values.Example#include using namespace std; //returning the result of a query int respondingQuery(pair arr[], int l, int r) {    int result = abs(arr[l - 1].second - arr[r - 1].second);    return result; } //implementing the queries void calcDifference(int givenarr[], int a, int q[][2], ...

Read More

Query for ancestor-descendant relationship in a tree in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 202 Views

In this tutorial, we will be discussing a program to find query for ancestor-descendant relationship in a tree.For this we will be provided with a rooted tree and Q queries. Our task is to find the two roots given in the query is an ancestor of the other or not.Example#include using namespace std; //using DFS to find the relation between //given nodes void performingDFS(vector g[], int u, int parent, int timeIn[], int timeOut[], int& count) {    timeIn[u] = count++;    for (int i = 0; i < g[u].size(); i++) {       int v = g[u][i];   ...

Read More

Find the element having different frequency than other array elements in C++

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

Suppose we have an array of N numbers, where each element in the array appears same number of times (m times, this is also given) except one element, We have to find this element.So, if the input is like A = [6, 2, 7, 2, 2, 6, 6], m = 3, then the output will be 7.To solve this, we will follow these steps −INT_SIZE := 8 * size of an integer type variableDefine an array count of size − INT_SIZE. and fill with 0for initialize i := 0, when i < INT_SIZE, update (increase i by 1), do:for initialize ...

Read More

Find the element that appears once in an array where every other element appears twice in C++

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

Suppose we have an array A. In this array there are different numbers that occurs twice. But there is only one number that occurs once. We have to find that element from that array.Suppose A = [1, 1, 5, 3, 2, 5, 2], then the output will be 3. As there are each number twice, we can perform XOR to cancel out that element. because we know y XOR y = 0To solve this, we will follow these steps.Take one variable res = 0for each element e in array A, preform res := res XOR ereturn resExample Let us see the ...

Read More

Find the maximum cost of an array of pairs choosing at most K pairs in C++

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

Suppose we have an array of pairs A; we have to find the maximum cost for selecting at most K pairs. In this case, the cost of an array of pairs type elements is the product of the sum of first elements of the selected pair and the smallest among the second elements of the selected pairs. As an example, if these pairs are selected (4, 8), (10, 3) and (3, 6), then the cost will be (4+10+3)*(3) = 51, for K=3So, if the input is like A = [(15, 5), (65, 25), (35, 20), (20, 5), (35, 20), (15, ...

Read More

Find the probability of reaching all points after N moves from point N in C++

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

Suppose we have a number N this represents the initial position of the person on the number line. We also have L which is the probability of the person of going left. We have to find the the probability of reaching all points on the number line after completing N moves from point N. Each move can be either to the left or to the right.So, if the input is like n = 2, l = 0.5, then the output will be [0.25, 0, 0.5, 0, 0.25]To solve this, we will follow these steps −high := 1 - lowDefine an ...

Read More

Product of all prime numbers in an Array in C++

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

Given an integer array arr[] with some elements, the task is to find the product of all the prime number of that numbers.Prime number are those number which are either divided by 1 or the number itself, or a prime number is a number which is not divisible by any other number except 1 and the number itself. Like 1, 2, 3, 5, 7, 11, etc.we have to find the solution for the given array −Input −arr[] = { 11, 20, 31, 4, 5, 6, 70 }Output − 1705Explanation − Prime numbers in array are − 11, 31, 5 their ...

Read More

Find the value of the function Y = (X^6 + X^2 + 9894845) % 981 in C++

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

Suppose we have given function like f(x) = (x^6 + x^2 + 9894845) % 971, now for a given value of x, we have to find the value of f(x).So, if the input is like 5, then the output will be 469To solve this, we will follow these steps −Define a function power_mod(), this will take base, exponent, modulus, base := base mod modulusresult := 1while exponent > 0, do −if exponent is odd, then −result := (result * base) mod modulusbase := (base * base) mod modulusexponent = exponent /2return resultFrom the main method do the following −return power_mod(n, ...

Read More

Reasons for a C++ program crash

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

The abnormal behavior of C++ programs often leads to program crash. You may have encountered problems like Segmentation fault, Aborted, Floating point exception etc. Following are sample programs that may help you to understand the reasons for a C++ program crash.ExceptionsExceptions in C++ are responses of a program when it encounters an abnormal condition. The program crashes due to such exceptions if they are not handled properly using try-catch blocks. Following program crashes due to divide by zero exception −Example#include int main(){    int num1=10;    int num2=0;    int quotient=num1/num2;    printf(" Quotient is: %d", quotient);    return ...

Read More

What happens when a virtual function is called inside a non-virtual function in C++

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

In this section we will discuss about interesting facts about virtual classes in C++. We will see two cases first, then we will analyze the fact.At first execute the program without using any virtual function.The execute the program using any virtual function under non-virtual function.ExampleLet us see the following implementation to get better understanding −#include using namespace std; class BaseClass { public:    void display(){       cout

Read More
Showing 771–780 of 5,961 articles
« Prev 1 76 77 78 79 80 597 Next »
Advertisements