Server Side Programming Articles

Page 1851 of 2109

C++ Range Sum Query Using Sparse Table

Prateek Jangid
Prateek Jangid
Updated on 26-Nov-2021 384 Views

Sparsh Table is a data structure, which is used to give results of range queries. It provides the result of most range queries in O(logN) complexity. For maximum range queries, it can also compute the result in O(1).This tutorial will discuss the problem of a range sum query using a sparse table where we are given an array. We need to find the sum of all elements in range L and R, for example.Input: arr[ ] = { 2, 4, 1, 5, 6, 3 } query(1, 3), query(0, 2), query(1, 5). Output: 10 7 19 Input: arr[ ] ...

Read More

C++ Queries to Answer the Number of Ones and Zeros to the Left of Given Index

Prateek Jangid
Prateek Jangid
Updated on 26-Nov-2021 294 Views

Discuss a problem to answer queries to the given array. For each query index, we need to find the number of ones and zeros to the left of the index, for example.Input: arr[ ] = { 0, 1, 1, 1, 0, 0, 0, 1, 0, 0}, queries[ ] = { 2, 4, 1, 0, 5 } Output: query 1: zeros = 1, ones = 1 query 2: zeros = 1, ones = 3 query 3: zeros = 1, ones = 0 query 4: zeros = 0, ones = 0 query 5: zeros = 2, ones = 3 Input: arr[ ...

Read More

C++ Queries on XOR of Greatest Odd Divisor of the Range

Prateek Jangid
Prateek Jangid
Updated on 26-Nov-2021 271 Views

Given an array of N integers and Q queries of ranges. For each query, we need to return the XOR of the greatest odd divisor of each number in the range.The greatest odd divisor is the greatest odd number which can divide number N, e.g . The greatest odd divisor of 6 is 3, for example.Input: nums[ ] = { 3, 6, 7, 10 }, query[ ] = { { 0, 2 }, { 1, 3 } } Output: query1: 7 query2: 1 Explanation: greatest odd divisors of nums array are { 3, 3, 7, 5 }. For query ...

Read More

Find the Number of Unique Permutations Starting with 1 of a Binary String using C++

Prateek Jangid
Prateek Jangid
Updated on 25-Nov-2021 502 Views

In the given problem, we are given a string consisting of 0’s and 1’s; we are required to find the total number of permutations such that the string starts with 1’s. As the answer can be a vast number, so we print as a mod with 1000000007.Input : str ="10101001001" Output : 210 Input : str ="101110011" Output : 56We will solve the given problem by applying some combinatorics and building up some formulas to solve this problem.Approach to find The SolutionIn the approach, we will calculate the number of 0's and 1's. Now let's suppose n is the ...

Read More

Find the Number of Unique Pairs in an Array using C++

Prateek Jangid
Prateek Jangid
Updated on 25-Nov-2021 1K+ Views

We require the appropriate knowledge to create several unique pairs in an array syntax in C++. In finding the number of unique pairs, we count all unique pairs in a given array, i.e., all possible pairs can be formed where each pair should be unique. For example −Input : array[ ] = { 5, 5, 9 } Output : 4 Explanation : The number of all unique pairs are (5, 5), (5, 9), (9, 5) and (9, 9). Input : array[ ] = { 5, 4, 3, 2, 2 } Output : 16Approach to find The SolutionThere are Two ...

Read More

Find the Number of Triangles Formed from a Set of Points on Three Lines using C++

Prateek Jangid
Prateek Jangid
Updated on 25-Nov-2021 371 Views

We are given several points present in 3 lines now; we are required to find how many triangles these points can form, for exampleInput: m = 3, n = 4, k = 5 Output: 205 Input: m = 2, n = 2, k = 1 Output: 10We will apply some combinatorics to this question and make up some formulas to solve this problem.Approach to find The SolutionIn this approach, we will devise a formula by applying combinatorics to the current situations, and this formula will give us our results.C++ Code for the Above ApproachHere is the C++ syntax which ...

Read More

Find the Number of Triangles After N Moves using C++

Prateek Jangid
Prateek Jangid
Updated on 25-Nov-2021 254 Views

n the article, first, we have to draw a colored triangle. We need to take an uncolored triangle and divide the triangle into four small equilaterals. Triangles with the same area and keep doing it till the nth step and find the number of equilateral triangles present in the figure.Approach to find The SolutionThere are Two Approaches for this Solution and they are −Brute Force ApproachWe can observe that the number of triangles keeps increasing by some number (increasing by 3*previous_number + 2) after every step. So we can run a loop till n and calculate the number of triangles.Example#include ...

Read More

Find the Number of Trailing Zeroes in base B Representation of N! using C++

Prateek Jangid
Prateek Jangid
Updated on 25-Nov-2021 397 Views

In this article, we will understand the problem of finding trailing zeros of a given number N in the base B representation of its factorial. For examplesInput : N = 7 Base = 2 Output : 4 Explanation : fact(7) = 5040 in base10 and 1001110110000 in base16 having 4 trailing zero. Input : N = 11 Base = 5 Output : 2 Explanation : fact(11) = 39916800 in base10 and 40204314200 in base16 having 2 trailing zeroes.Let's first recap the process of converting any decimal number from one base to another. Let's take an example of converting (5040)10 ...

Read More

Find the Number of Trailing Zeroes in Base 16 Representation of N! using C++

Prateek Jangid
Prateek Jangid
Updated on 25-Nov-2021 288 Views

In this article, we will understand the problem of finding trailing zeros of a given number N in the base 16 representation of its factorial for exampleInput : N = 7 Output : 1 Explanation : fact(7) = 5040 in base10 and 13B0 in base16 having 1 trailing zero. Input : N = 11 Output : 2 Explanation : fact(11) = 39916800 in base10 and 2611500 in base16 having 2 trailing zeroes.Let's first recap the process of converting any decimal number from one base to another; let's take an example of converting (5040)10 to (?)16i.e., dividing the number by ...

Read More

Find the Number of Substrings of One String Present in Other using C++

Prateek Jangid
Prateek Jangid
Updated on 25-Nov-2021 503 Views

In this article, we are given two strings, and we need to find out how many substrings of the 1st string can be found in the 2nd string(the exact substring can occur multiple times). For exampleInput : string1 = “fogl”    string2 = “google” Output : 6 Explanation : substrings of string1 present in string2 are [ “o”, “g”, “l”, “og”, “gl”, “ogl” ]. Input : string1 = “ajva”    string2 = “java” Output : 5 Explanation : substrings of string1 present in string2 are [ “a”, “j”, “v”, “a”, “va” ].Approach to find The SolutionLet's discuss how we ...

Read More
Showing 18501–18510 of 21,090 articles
Advertisements