Found 7197 Articles for C++

Find three integers less than or equal to N such that their LCM is maximum in C++

Ayush Gupta
Updated on 19-Aug-2020 10:54:33

114 Views

In this tutorial, we will be discussing a program to find three integers less than or equal to N such that their LCM is maximum.For this we will be provided with an integer value. Our task is to find other three integers smaller than the given value such that their LCM is maximum.Example Live Demo#include using namespace std; //finding three integers less than given value //having maximum LCM void findMaximumLCM(int n) {    if (n % 2 != 0) {       cout

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

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

142 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 Live Demo#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 ... Read More

Querying the number of distinct colors in a subtree of a colored tree using BIT in C++

Ayush Gupta
Updated on 19-Aug-2020 10:51:40

193 Views

In this tutorial, we will be discussing a program to find querying the number of distinct colors in a subtree of a colored tree using BIT.For this we will be provided with rooted tree where each node has a color denoted by given array. Our task is to find all the distinct coloured nodes below the given node in the tree.Example Live Demo#include #define MAXIMUM_COLOUR 1000005 #define MAXIMUM_NUMBER 100005 using namespace std; vector tree[MAXIMUM_NUMBER]; vector table[MAXIMUM_COLOUR]; int isTraversing[MAXIMUM_COLOUR]; int bit[MAXIMUM_NUMBER], getVisTime[MAXIMUM_NUMBER], getEndTime[MAXIMUM_NUMBER]; int getFlatTree[2 * MAXIMUM_NUMBER]; bool vis[MAXIMUM_NUMBER]; int tim = 0; vector< pair< pair, int> > queries; //storing results of ... Read More

Queries to update a given index and find gcd in range in C++

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

132 Views

In this tutorial, we will be discussing a program to find queries to update a given index and find gcd in range.For this we will be provided with an array containing integers and Q queries. Our task is to find the result of given queries (updating a given value by X, finding the gcd between two given values).Example Live Demo#include using namespace std; //getting middle index int findMiddle(int s, int e) {    return (s + (e - s) / 2); } //updating values at given indices void updateIndexValue(int* st, int ss, int se, int i, int diff, int si) ... Read More

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

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

102 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

169 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

106 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

158 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

175 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

151 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

Advertisements