Found 7347 Articles for C++

Find time taken for signal to reach all positions in a string in C++

Ayush Gupta
Updated on 19-Aug-2020 10:44:25

61 Views

In this tutorial, we will be discussing a program to find time taken for signal to reach all positions in a stringFor this we will be provided with a string containing ‘x’ and ‘o’. A signal originates from ‘x’ and travels in both directions changing one ‘o’ value in one unit time. Our task is to calculate the complete time to convert whole string into ‘x’s.Example Live Demo#include using namespace std; //calculating the total required time int findMaximumDuration(string s, int n) {    int right = 0, left = 0;    int count = 0, maximumLength = INT_MIN;    s ... Read More

Find two numbers with sum and product both same as N in C++

Ayush Gupta
Updated on 19-Aug-2020 10:42:22

130 Views

In this tutorial, we will be discussing a program to find two numbers with sum and product both same as N.For this we will be provided with an integer value. Our task is to find two other integer values whose product and sum is equal to the given value.Example Live Demo#include using namespace std; //finding a and b such that //a*b=N and a+b=N void calculateTwoValues(double N) {    double val = N * N - 4.0 * N;    if (val < 0) {       cout

Find triplet such that number of nodes connecting these triplets is maximum in C++

Ayush Gupta
Updated on 19-Aug-2020 10:41:07

57 Views

In this tutorial, we will be discussing a program to find triplet such that number of nodes connecting these triplets is maximum.For this we will be provided with a tree with N nodes. Our task is to find a triplet of nodes such that the nodes covered in the path joining them in maximum.Example Live Demo#include #define ll long long int #define MAX 100005 using namespace std; vector nearNode[MAX]; bool isTraversed[MAX]; //storing the required nodes int maxi = -1, N; int parent[MAX]; bool vis[MAX]; int startnode, endnode, midNode; //implementing DFS to search nodes void performDFS(int u, int count) {   ... Read More

Find trace of matrix formed by adding Row-major and Column-major order of same matrix in C++

Ayush Gupta
Updated on 19-Aug-2020 10:38:48

89 Views

In this tutorial, we will be discussing a program to find trace of matrix formed by adding Row-major and Column-major order of same matrix.For this we will be provided with two arrays one in row-major and other in columnmajor. Our task is to find the trace of the matrix formed by the addition of the two given matrices.Example Live Demo#include using namespace std; //calculating the calculateMatrixTrace of the new matrix int calculateMatrixTrace(int row, int column) {    int A[row][column], B[row][column], C[row][column];    int count = 1;    for (int i = 0; i < row; i++)       for ... Read More

Find uncommon characters of the two strings in C++

Ayush Gupta
Updated on 19-Aug-2020 10:36:14

99 Views

In this tutorial, we will be discussing a program to find uncommon characters of the two strings.For this we will be provided with two strings. Our task is to print out the uncommon characters of both strings in sorted order.Example Live Demo#include using namespace std; const int LIMIT_CHAR = 26; //finding the uncommon characters void calculateUncommonCharacters(string str1, string str2) {    int isthere[LIMIT_CHAR];    for (int i=0; i

Find two distinct prime numbers with given product in C++

Ayush Gupta
Updated on 19-Aug-2020 10:34:42

105 Views

In this tutorial, we will be discussing a program to find two distinct prime numbers with given product.For this we will be provided with an integer value. Our task is to find the two prime integer values such that their product is equal to the given value.Example Live Demo#include using namespace std; //generating prime numbers less than N. void findingPrimeNumbers(int n, bool calcPrime[]) {    calcPrime[0] = calcPrime[1] = false;    for (int i = 2; i

Sum of two numbers where one number is represented as array of digits in C++

sudhir sharma
Updated on 17-Aug-2020 10:43:45

84 Views

In this problem, we are given two numbers, from which one is represented using array of digits. Our task is to create a program that will find the sum of two numbers where one number is represented as array of digits.Let’s take an example to understand the problem, Input: n = 213, m[] = {1, 5, 8, } Output: 371 Explanation: 213 + 158 = 371To solve this problem, we will simply digit by digit from the number which element of the array. It lsb of the number is added to the (n-1)th element of the array. The carry will ... Read More

Sum of two large numbers in C++

sudhir sharma
Updated on 17-Aug-2020 10:40:28

4K+ Views

In this problem, we are given two string that defines two large numbers. Our task is to create a program to find the sum of two large numbers.Let’s take an example to understand the problem, Input: number1 = “341299123919” number2 = “52413424” Output: 341351537343To solve this problem, we will traverse both the string. And add digit by digit and propagate the carry. And store the result digit by digit to sum string.AlgorithmInitialize sum = 0, carry = 0. Step 1: loop from n to 0. Step 1.1: intSum = number1[i] + number2[i] Step 1.2: carry = intSum/10. Sum += intSum ... Read More

Sum of the series Kn + ( K(n-1) * (K-1)1 ) + ( K(n-2) * (K-1)2 ) + ... (K-1)n in C++

sudhir sharma
Updated on 17-Aug-2020 10:22:15

228 Views

In the problem, we are ginen two number k and n of the series K^n + ( K^(n-1) * (K-1)^1 ) + ( K^(n-2) * (K-1)^2 ) + ... (K-1)^n. Our task is to create a program to find the sum of the series.Let’s take an example to understand the problem, Input: n = 3, k = 4 Output: 175 Explanation: Sum of the series is = 4^3 + ( (4^2)*(3^1) ) + ( (4^1)*(3^2) ) + ( (4^0)*(3^3) ) = 64 + 48 + 36 + 27 = 175A simple way to solve the problem, is using a for ... Read More

Sum of XOR of all subarrays in C++

sudhir sharma
Updated on 17-Aug-2020 10:14:33

541 Views

In this problem, we are given an array arr[] of n numbers. Our task is to create a program to find the sum of XOR of all subarrays of the array.Here, we need to find all sub-arrays of the given array, and then for each subarray, we will find the xor of element and add the XOR value to the sum variable.Let’s take an example to understand the problem, Input: arr[] = {5, 1, 4} Output: Explanation: XOR of all subarrays for the array : XOR {5} = 5 XOR {1} = 1 XOR {4} = 4 XOR {5, 1} ... Read More

Advertisements