Check If a Number Lies in Ranges of L and R in C++

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

526 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

Check If a Given Digit Is Present in a Range in C++

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

352 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

Count Unordered Co-Prime Pairs from 1 to N in C++

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

288 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

Find Distance Between Two Nodes of a Binary Tree in O(log n) Method

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 Find Maximum Product Pair in Range with Updates in C++

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

391 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

Find Last Non-Repeating Character in Substring of Given String in C++

Ayush Gupta
Updated on 09-Oct-2020 08:55:40

185 Views

In this problem, we are given string str, and Q queries, each consisting of two integers. Our task is to create the program to solve Queries to find the last non-repeating character in the sub-string of a given string in C++.Problem DescriptionIn each query, we have two integers L and R. To solve the queries, we will take a substring starting from index L to index R. And find the last character which is non-repeating in the sub-string.Let’s take an example to understand the problem, Input: str = “Tutorialspoint” Q = 2query = {{4, 8}, {2, 6}}Output: -1 , -1ExplanationsubStr[4...8] ... Read More

Find Whether a Number Has Exactly Four Distinct Factors in C++

Ayush Gupta
Updated on 09-Oct-2020 08:53:01

411 Views

In this problem, we are given a Q number of queries, each having a number N. Our task is to create a program to solve the Queries to find whether a number has exactly four distinct factors or not in C++.Problem DescriptionTo solve each query, we need to find whether the number N has exactly four distinct factors. If it has print YES, else No.Let’s take an example to understand the problem, Input: Q = 3, 4, 6, 15 Output: NOYES YESExplanationFor query 1: Factors of 4 are 1, 2, 4For query 2: Factors of 6 are 1, 2, 3, 6For ... Read More

Add, Remove and Return Difference of Max and Min in C++

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

133 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

Find Last Digit of N-th Fibonacci Number in C++

Ayush Gupta
Updated on 09-Oct-2020 08:47:13

577 Views

In this problem, we are given a number N. Our task is to create a Program to find last digit of Nth Fibonacci number in C++.Problem DescriptionWe need to find the last digit (i.e. LSB ) of the Nth Fibonacci number.Let’s take an example to understand the problem, Input: N = 120 Output: 1Solution ApproachA simple solution will be using the direct Fibonacci formula to find the Nth term. But this method will not be feasible when N is a large number. So to overcome this thing, we will use the property of the Fibonacci Series that the last digit ... Read More

Find Last Two Digits of 2^n in C++

Ayush Gupta
Updated on 09-Oct-2020 07:40:41

619 Views

In this problem, we are given a number N. Our task is to create a Program to find last two digits of 2^n in C++.Problem DescriptionTo find the last two digits. We will use only the product of the last two digits. And leave other things to make the calculation small.Let’s take an example to understand the problem, Input: N = 12 Output: 96Explanation2^12 = 4096Solution ApproachTo solve the problem, a direct approach could be finding the value of 2^N and then finding the remainder when it is divided by 100.Example Live Demo#include using namespace std; int findLastDigit(int N){    int ... Read More

Advertisements