Programming Articles

Page 1286 of 2547

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

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 370 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 find distance between two nodes of a Binary tree – O(logn) method in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 251 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

Maximum possible sum of a window in an array such that elements of same window in other array are unique in c++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 150 Views

In this tutorial, we will be discussing a program to find maximum possible sum of a window in an array such that elements of same window in other array are unique.For this we will be provided with two arrays with equal number of elements. Our task is to find the window in one element with maximum sum such that the same window in other array is unique.Example#include using namespace std; //returning maximum sum window int returnMaxSum(int A[], int B[], int n) {    //storing elements with their count    unordered_set mp;    int result = 0;    int curr_sum ...

Read More

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

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 456 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 the last non-repeating character in the sub-string of a given string in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 237 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

Maximum power of jump required to reach the end of string in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 306 Views

In this tutorial, we will be discussing a program to find maximum power of jump required to reach the end of string.For this we will be provided with a string of 0s and 1s. Our task is to find the maximum jump required to move from front to end of string given you can move to the same element as current one.Example#include using namespace std; //finding maximum power jump int powerOfJump(string s) {    int count = 1;    int max_so_far = INT_MIN;    char ch = s[s.length() - 1];    for (int i = 0; i < s.length(); i++) ...

Read More

Queries to find whether a number has exactly four distinct factors or not in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 482 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

Maximum Product Cutting | DP-36 in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 229 Views

In this tutorial, we will be discussing a program to find maximum Product Cutting | DP-36.For this we will be provided with a rope of N meters. Our task is to cut the rope in different integer lengths such that the product of their lengths is the maximumExample#include using namespace std; //finding maximum of two, three integers int max(int a, int b) {    return (a > b)? a : b; } int max(int a, int b, int c) {    return max(a, max(b, c)); } //returning maximum product int maxProd(int n) {    if (n == 0 || ...

Read More

Sum of the products of all possible Subsets in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 870 Views

In this problem, we are given an array arr[] of N numbers. Our task is to create a program that will find the sum of the products of all possible subsets.Here, we will find all subsets and then find the product of all elements for each subset. Then add all the values to calculate the sum.Let’s take an example to understand the problem, Inputarr[] = {4, 5, 6}Output209Explanation −All subsets of arr[] are: {4}, {5}, {6}, {4, 5}, {5, 6}, {4, 6}, {4, 5, 6} Sum of product = (4) + (5) + (6) + (4*5) + (5*6) + (4*6) ...

Read More

Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 187 Views

In this tutorial, we will be discussing a program to find maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k.For this we will be provided with an array and an integer k. Our task is to find the maximum product from the array given that the frequency sum of all digits must be smaller than or equal to 2 * k.Example#include using namespace std; #define ll long long int //returning maximum product value ll maxProd(int arr[], int n, int k) {    ll product = ...

Read More
Showing 12851–12860 of 25,466 articles
Advertisements