Found 7347 Articles for C++

Range Sum Queries Without Updates using C++

Prateek Jangid
Updated on 26-Nov-2021 10:09:29

320 Views

In this article, we will give an array of size n, which will be an integer. Then, we will compute the sum of elements from index L to index R and execute the queries multiple times, or we need to calculate the sum of the given range from [L, R]. For example −Input : arr[] = {1, 2, 3, 4, 5}    L = 1, R = 3    L = 2, R = 4 Output : 9    12 Input : arr[] = {1, 2, 3, 4, 5}    L = 0, R = 4    L = ... Read More

Queries to Print All the Divisors of n using C++

Prateek Jangid
Updated on 26-Nov-2021 10:05:03

359 Views

In the given problem, we are required to print all the divisors of a given integer n.Input: 15 Output: 1 3 5 15 Explanation Divisors of 15 are: 1, 3, 5, 15 Input: 30 Output: 1 2 3 5 15 30In the given problem, we can apply the approach used in the sieve of Eratosthenes for finding all the divisors of n.Approach to find The SolutionIn the given approach, we will apply the concept in which the sieve of Eratosthenes is based and find the divisors of n.Example#include #define MOD 1000000007 using namespace std; vector divisors[100001]; ... Read More

Queries for number of array elements in a range with Kth Bit Set using C++

Prateek Jangid
Updated on 26-Nov-2021 09:56:19

227 Views

In this article we will discuss a problem of finding the number of elements present in the given range that have a kth bit set, for example −Input : arr[] = { 4, 5, 7, 2 } Query 1: L = 2, R = 4, K = 4 Query 2: L = 3, R = 5, K = 1 Output :    0    1We are going to solve this problem by a brute force approach and see if this approach can work for higher constraints or not. If not, then we try to think of a new efficient approach.Brute ... Read More

Queries for greater than and not less than using C++

Prateek Jangid
Updated on 26-Nov-2021 09:37:59

399 Views

In this article, we are given a problem, we are given an array, and there are two types of queries we need to answer.Type 0 − we have to calculate the number of greater elements than or equal to x(given value).Type 1 − we have to calculate the number of strictly greater elements than x(given value).So here is a simple example −Input : arr[] = { 10, 15, 30 , 40, 45 } and Q = 3    Query 1: 0 50    Query 2: 1 40    Query 3: 0 30 Output :    0    1    3 ... Read More

Queries for Bitwise OR in the Index Range [L, R] of the Given Array using C++

Prateek Jangid
Updated on 26-Nov-2021 10:01:24

814 Views

In this article, we are given an array of integers. We are tasked to find the bitwise OR of all the numbers present in the given range, for example, Input: arr[] = {1, 3, 1, 2, 3, 4}, q[] = {{0, 1}, {3, 5}} Output: 3 7 1 OR 3 = 3 2 OR 3 OR 4 = 7 Input: arr[] = {1, 2, 3, 4, 5}, q[] = {{0, 4}, {1, 3}} Output: 7 7In the given problem, we will approach it with a brute force approach and then check if it can work for higher constraints or not. ... Read More

Queries for bitwise AND in the index range [L, R] of the given Array using C++

Prateek Jangid
Updated on 26-Nov-2021 08:07:35

1K+ Views

In this article, we have given a problem in which we are given an array of integers, and we are tasked to find the bitwise AND of the given ranges, for example 7minus;Input: arr[ ] = {1, 3, 1, 2, 32, 3, 3, 4, 4}, q[ ] = {{0, 1}, {3, 5}} Output: 1 0 0 1 AND 31 = 1 23 AND 34 AND 4 = 00 Input: arr[ ] = {1, 2, 3, 4, 510, 10 , 12, 16, 8}, q[ ] = {{0, 42}, {1, 33, 4}} Output: 0 8 0We are going to apply the brute ... Read More

Find the Pentagonal Pyramidal Number using C++

Prateek Jangid
Updated on 26-Nov-2021 07:40:29

115 Views

A pentagonal pyramidal number is equal to the number of items in a pentagonal base pyramid. Look at some Pentagonal numbers below.Sum of Pentagonal Numbers till N equals to Nth Pentagonal Pyramidal Number. In this article, we will discuss finding the Nth Pentagonal Pyramidal number, for exampleInput : N = 4 Output : 40 Explanation : Sum of first four pentagonal numbers 1, 5, 12, 22 is 40. Input : N = 6 Output : 126 Explanation : Sum of first four pentagonal numbers 1, 5, 12, 22, 35, 51 is 40.Approach to find The SolutionSimple ApproachAs per the ... Read More

Find the Pell Number using C++

Prateek Jangid
Updated on 26-Nov-2021 07:29:58

416 Views

In the given problem, we are given an integer n we need to find Pn, i.e., the pell number in that position. Now, as we know, pell number is a part of a series given by this formula −Pn = 2*Pn-1 + Pn-2With first two starting numbers − P0 = 0 and P1 = 1Approach to find The SolutionNow we will solve this problem by two approaches: recursive and iterative.Recursive ApproachIn this formula, we will recursively apply the formula of Pell Number and do n iterations.Example#include using namespace std; int pell(int n) {    if(n

Find the Pattern of 1’s inside 0’s using C++

Prateek Jangid
Updated on 26-Nov-2021 07:21:51

237 Views

In this article we are given values of several rows and several columns. We need to print a Box pattern such that 1’s get printed on 1st row, 1st column, last row, last column, and 0’s get printed on remaining elements. For example −Input : rows = 5, columns = 4 Output :    1 1 1 1    1 0 0 1    1 0 0 1    1 0 0 1    1 1 1 1 Input : rows = 8, columns = 9 Output :    1 1 1 1 1 1 1 1 1   ... Read More

C++ Reverse a path in BST using queue

Prateek Jangid
Updated on 26-Nov-2021 07:18:09

114 Views

Given a binary search tree, and we are required to reverse its path from a particular key, for example.Approach to Find the SolutionIn this approach, we will make a queue and push all the nodes until we get the root.Example  #include using namespace std; struct node {    int key;    struct node *left, *right; }; struct node* newNode(int item){    struct node* temp = new node;    temp->key = item;    temp->left = temp->right = NULL;    return temp; } void inorder(struct node* root){    if (root != NULL) {        inorder(root->left);        cout ... Read More

Advertisements