Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C++ Articles
Page 177 of 597
Measure one litre using two vessels and infinite water supplys in C++
In this problem, we are given two vessels with capacities x and y and a supply of infinite water. Our task is to create a program that will be able to calculate exactly 1 liter in one vessel. Given the condition that x and y are co-primes. Co-primes also is known as relatively prime, mutually prime are numbers two numbers that have 1 as their only common divisor. So, this implies that their gcd(greatest common divisor) is 1.Here, let’s suppose we have two vessels V1 with capacity x and V2 with capacity y. To measure 1 liter using these two ...
Read MoreMedian after K additional integers in C++
In this problem, we are given an array of n integers and we are adding K elements to the array and then find the median of the resultant array. Given the condition, N+k is odd.Let’s take an example to understand the problem, Input −array = {23, 65, 76, 67} ; k =1Output −67To solve this problem, we will sort the given elements in ascending order and then add k elements at the end of the array i.e. we will take k greater elements.The condition is given that n+k is odd. So, the median can be calculated using the formula, (n+k)/2.ExampleProgram ...
Read MoreCount all pairs of adjacent nodes whose XOR is an odd number in C++
In this tutorial, we will be discussing a program to find the number of pairs of adjacent nodes whose XOR is an odd number.For this we will be provided with a binary tree. Our task is to count the number of pairs of adjacent elements whose XOR is an odd number.Example#include using namespace std; //node structure of tree struct Node { int data; struct Node *left, *right; }; //finding the pairs whose XOR //is odd int count_pair(Node* root, Node *parent=NULL){ if (root == NULL) return 0; //checking pair of XOR is odd ...
Read MoreCount all subsequences having product less than K in C++
In this tutorial, we will be discussing a program to find the number of sub sequences having product less than K.For this we will be provided with non-negative array and a value k. Our task is to find all the subsequences in the array having product less than k.Example#include using namespace std; //counting subsequences with product //less than k int count_sub(vector &arr, int k){ int n = arr.size(); int dp[k + 1][n + 1]; memset(dp, 0, sizeof(dp)); for (int i = 1; i
Read MoreCount all the numbers in a range with smallest factor as K in C++
In this tutorial, we will be discussing a program to find the numbers in a range with the smallest factor as K.For this we will be provided with a range [a,b]. Our task is to count the numbers in the given range who have their smallest factor as K.Example#include using namespace std; //checking if K is a prime bool if_prime(int k){ if (k
Read MoreHandling the Divide by Zero Exception in C++
In this tutorial, we will be discussing how to handle the divide by Zero exception in C++.Division by zero is an undefined entity in mathematics, and we need to handle it properly while programming so that it doesn’t return at error at the user end.Using the runtime_error classExample#include #include using namespace std; //handling divide by zero float Division(float num, float den){ if (den == 0) { throw runtime_error("Math error: Attempted to divide by Zero"); } return (num / den); } int main(){ float numerator, denominator, result; numerator = 12.5; denominator = 0; try { result = Division(numerator, denominator); cout
Read MoreCount all the columns in a matrix which are sorted in descending in C++
In this tutorial, we will be discussing a program to find the number of columns in a matrix which are sorted in descending.For this we will be provided with a matrix. Our task is to count the number of columns in the matrix having elements sorted in descending order.Example#include #define MAX 100 using namespace std; //counting columns sorted in descending order int count_dcolumns(int mat[][MAX], int r, int c){ int result = 0; for (int i=0; i0; j--) if (mat[i][j-1] >= mat[i][j]) break; if (c ...
Read MoreCount all sorted rows in a matrix in C++
In this tutorial, we will be discussing a program to find the number of all sorted rows in a matrix.For this we will be provided with m*n matrix. Our task is to count all the rows in the given matrix that are sorted either in ascending or descending order.Example#include #define MAX 100 using namespace std; //counting sorted rows int count_srows(int mat[][MAX], int r, int c){ int result = 0; for (int i=0; i
Read MoreCount all sub-arrays having sum divisible by k
In this tutorial, we will be discussing a program to find the number of sub-arrays having sum divisible by k.For this we will be provided with an array and a value k. Our task is to find the number of sub arrays that are having their sum as equal to the given value k.Example#include using namespace std; //counting subarrays with k sum int count_subarray(int arr[], int n, int k){ int mod[k]; memset(mod, 0, sizeof(mod)); int cumSum = 0; for (int i = 0; i < n; i++) { cumSum += arr[i]; ...
Read MoreCount all Quadruples from four arrays such that their XOR equals to 'x' in C++
In this tutorial, we will be discussing a program to find the number of quadruples from four arrays such that their XOR equals to x.For this we will be provided with four arrays and a value x. Our task is to count all the quadruples whose XOR is equal to the given value x.Example#include using namespace std; //counting quadruples with XOR equal to x int count_quad(int a[], int b[], int c[], int d[], int x, int n){ int count = 0; for (int i = 0 ; i < n ; i++) for (int j ...
Read More