Found 7197 Articles for C++

Queries to find maximum product pair in range with updates in C++

Ayush Gupta
Updated on 09-Oct-2020 08:58:39

388 Views

In this problem, we are given an array arr[] and Q queries. Each Query can be one of 2 types, 1st to find the maximum pair product in a given range [ Start - End ]. 2nd to update the ith index element with value. Our task is to create a program to solve Queries to find maximum product pair in range with updates in C++.Let’s take an example to understand the problem, Input:arr = {4, 2, 6, 9, 1}Q = 3Q1 = [1, 1, 4]Q2 = [2, 2, 3]Q3 = [1, 0, 2]Output: 54, 12ExplanationFor query 1, type 1: range ... Read More

Queries to find distance between two nodes of a Binary tree – O(logn) method in C++

Ayush Gupta
Updated on 09-Oct-2020 09:03:23

176 Views

In this problem, we are given a binary tree and we are given Q queries. Our task is to create a program to solve Queries to find distance between two nodes of a Binary tree – O(logn) method in C++.Problem DescriptionIn each query, we are given two nodes of the binary tree and we need to find the number distance between two nodes i.e. the number of edges to be traversed to reach one node from another node.Let’s take an example to understand the problem, Input: Binary TreeQueries = 3Q1 -> [2, 6]Q2 -> [4, 1]Q3 -> [5, 3]Output:3, 2, ... Read More

Queries to count the number of unordered co-prime pairs from 1 to N in C++

Ayush Gupta
Updated on 09-Oct-2020 09:04:59

287 Views

In this problem, we are given Q queries each contains a number N. Our task is to create a program to solve Queries to count the number of unordered coprime pairs from 1 to N in C++.Co-prime also known as relatively prime or mutually prime are the pair of numbers that have only one factor i.e. 1.Let’s take an example to understand the problem, Input: Q = 2, queries = [5, 6] Output: 10ExplanationThe pairs are : (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 5), (3, 4), (3, 5), (4, 5)Solution ApproachThe most promising solution ... Read More

Queries to check whether a given digit is present in the given Range in C++

Ayush Gupta
Updated on 09-Oct-2020 09:09:14

351 Views

In this problem, we have given an array arr[] and some queries each consisting of three values, L and R, and val. Our task is to create a program to solve Queries to check whether a given digit is present in the given Range in C++.Problem Description−To solve each query, we need to check if the given element val is present in the given Range between L and R or not.Let’s take an example to understand the problem, Input:arr[] = {4, 8, 1, 7, 2, 9, 3, 5, 1}Q = 3query = {{1, 4, 3}, {0, 2, 1}, {4, 7, ... Read More

Queries to check if it is possible to join boxes in a circle in C++

Ayush Gupta
Updated on 24-Aug-2020 05:37:23

119 Views

In this tutorial, we will be discussing a program to find queries to check if it is possible to join boxes in a circle.For this we will be provided with a circle of boxes running from 1 to n. Our task is to find whether box i can be connected to box j with a rod without intersecting the previous rodes.Example Live Demo#include using namespace std; //checking if making a circle from boxes is possible void isPossible(int n, int q, int queryi[], int queryj[]) {    int arr[50];    for (int i = 0; i queryj[k]) {     ... Read More

Queries to check if a number lies in N ranges of L-R in C++

Ayush Gupta
Updated on 09-Oct-2020 09:11:02

525 Views

In this problem, we are given N ranges [L, R] and Q queries each containing a number val. Our task is to create a program to solve Queries to check if a number lies in N ranges of L-R in C++.Problem DescriptionWe are given N ranges of type of [L, R] that contain integer values from L to R, for example, range [3, 6] contains 3, 4, 5, 6. In each query, we are given a val, whose presence is to be checked. The program will return true if the val is present in any one of the ranges otherwise ... Read More

Queries to answer the X-th smallest sub-string lexicographically in C++

Ayush Gupta
Updated on 09-Oct-2020 09:13:05

184 Views

In this problem, we are given a string str and Q queries. Each Query has a number X. Our task is to create a program to solve the Queries to answer the X-th smallest sub-string lexicographically in C++.Problem DescriptionWe need to find the Xth lexicographically smallest substring for each query i.e. based on alphabetical order sorting we will have to find Xth substring.Let’s take an example to understand the problem, Input: str = “point”Q = 4 query = {4, 7, 2, 13}Output:n, oi, in, poinExplanationAll substrings of str in lexicographical order are−i, in, int, n, nt, o, oi, oin, oint, ... Read More

Find distance between two nodes of a Binary Tree in C++ Program

Ayush Gupta
Updated on 09-Oct-2020 09:14:45

293 Views

In this problem, we are given a binary tree and two nodes. Our task is to create a program to Find distance between two nodes of a Binary Tree.Problem DescriptionWe need to find the distance between two nodes which is the minimum number of edges that will be traversed when we go from one node to another node.Let’s take an example to understand the problem, Input: binary treeNode1 = 3 ,Node2 = 5Output: 3ExplanationThe path from node 3 to node 5 is 3 -> 1 -> 2 -> 5. There are 3 edges traversed that make distance 3.Solution ApproachA simple ... Read More

Queries to add, remove and return the difference of maximum and minimum in C++

Ayush Gupta
Updated on 09-Oct-2020 08:50:07

132 Views

In this problem, we are given Q queries. These are of three types, they are −Query 1: Add number N to the list.Query 2: Remove number N to the list.Query 3: Return the difference of minimum and maximum element of the list.Our task is to create a program to solve queries to add, remove, and return the difference of maximum and minimum in C++.Problem DescriptionWe will be given a Q number of queries that we will be performing on the list. There are 3 types of queries to add, remove, and find the difference of maximum and minimum element of ... Read More

Different methods to copy in C++ STL - std::copy(), copy_n(), copy_if(), copy_backwards()

Sunidhi Bansal
Updated on 14-Aug-2020 08:43:35

458 Views

As the method name suggests copy() method is used to copy the data through various methods available in C++ STL. All the methods differ in functionalities and the parameters. These methods are available in header file. Let’s discuss each method and their functionalities.Copy(start_i1, end_i1, start_i2)This method is used to copy the data from one iterator to another iterator within specified range where both the start and end elements of an iterator are inclusive. It takes three types of arguments i.e. −Start_i1 − It will point to the initial element of the iterator, let’s say, i_1 from where the element ... Read More

Advertisements