Server Side Programming Articles - Page 1889 of 2646

Count all Palindrome Sub-Strings in a String in C++

Ayush Gupta
Updated on 10-Feb-2020 11:13:53

234 Views

In this tutorial, we will be discussing a program to find the number of palindrome sub strings in a string.For this we will be given a string. Our task is to count the number of palindrome sub strings in the given string with length greater than 3.Example#include using namespace std; //counting palindrome strings int count_pstr(char str[], int n){    int dp[n][n];    memset(dp, 0, sizeof(dp));    bool P[n][n];    memset(P, false , sizeof(P));    for (int i= 0; i< n; i++)       P[i][i] = true;    for (int i=0; i

Count all pairs with given XOR in C++

Ayush Gupta
Updated on 10-Feb-2020 11:07:13

268 Views

In this tutorial, we will be discussing a program to find the number of pairs with the given XOR.For this we will be provided with an array and a value. Our task is to find the number of pairs whose XOR is equal to the given value.Example#include using namespace std; //returning the number of pairs //having XOR equal to given value int count_pair(int arr[], int n, int x){    int result = 0;    //managing with duplicate values    unordered_map m;    for (int i=0; i

Count all pairs of an array which differ in K bits in C++

Ayush Gupta
Updated on 10-Feb-2020 10:55:19

206 Views

In this tutorial, we will be discussing a program to find the number of pairs of an array which differ in K bits.For this we will be provided with an array and an integer K. Our task is to find the number of pairs who differ by K bits in their binary representation.Example#include using namespace std; //counting number of bits in //binary representation int count_bit(int n){    int count = 0;    while (n) {       if (n & 1)          ++count;       n >>= 1;    }    return count; } ... Read More

Count all pairs of adjacent nodes whose XOR is an odd number in C++

Ayush Gupta
Updated on 10-Feb-2020 10:50:15

154 Views

In this tutorial, we will be discussing a program to find the number of pairs of adjacent nodes whose XOR is an odd number.For this we will be provided with a binary tree. Our task is to count the number of pairs of adjacent elements whose XOR is an odd number.Example Live Demo#include using namespace std; //node structure of tree struct Node {    int data;    struct Node *left, *right; }; //finding the pairs whose XOR //is odd int count_pair(Node* root, Node *parent=NULL){    if (root == NULL)       return 0;    //checking pair of XOR is ... Read More

Median Of Running Stream of Numbers in C++

Narendra Kumar
Updated on 03-Jun-2020 07:22:47

443 Views

In this problem, we are given a data stream that is continuously reading integers. Our task is to create a program that will read elements and calculate the medians for these elements.The Median of an array is the middle element from a sorted sequence(it can be ascending or descending).Calculating medianFor odd count, the median is the middle elementFor even count, the median is average of two middle elementLet’s take an example to understand the problem, Input − 3, 65, 12, 20, 1At each input, Input - 3 : sequence -(3) : median - 3 Input - 65 : sequence -(3, ... Read More

Median in a stream of integers (running integers) in C++

Narendra Kumar
Updated on 10-Feb-2020 10:23:36

170 Views

Problem statementGiven that integers are read from a data stream. Find median of elements read so for in an efficient wayAfter reading 1st element of stream - 10 -> median - 10After reading 2nd element of stream - 10, 20 -> median - 15After reading 3rd element of stream - 10, 20, 30 -> median - 20, so on...Algorithm1. Use a max heap on left side to represent elements that are less than effective median,    and a min heap on right side to represent elements that are greater than effective median 2. After processing an incoming element, the number ... Read More

Median after K additional integers in C++

Narendra Kumar
Updated on 03-Jun-2020 07:24:50

103 Views

In this problem, we are given an array of n integers and we are adding K elements to the array and then find the median of the resultant array. Given the condition, N+k is odd.Let’s take an example to understand the problem, Input −array = {23, 65, 76, 67} ; k =1Output −67To solve this problem, we will sort the given elements in ascending order and then add k elements at the end of the array i.e. we will take k greater elements.The condition is given that n+k is odd. So, the median can be calculated using the formula, (n+k)/2.ExampleProgram ... Read More

Measure one litre using two vessels and infinite water supplys in C++

Narendra Kumar
Updated on 03-Jun-2020 07:26:25

219 Views

In this problem, we are given two vessels with capacities x and y and a supply of infinite water. Our task is to create a program that will be able to calculate exactly 1 liter in one vessel. Given the condition that x and y are co-primes. Co-primes also is known as relatively prime, mutually prime are numbers two numbers that have 1 as their only common divisor. So, this implies that their gcd(greatest common divisor) is 1.Here, let’s suppose we have two vessels V1 with capacity x and V2 with capacity y. To measure 1 liter using these two ... Read More

Mean of range in array in C++

Narendra Kumar
Updated on 03-Jun-2020 07:27:58

468 Views

In this problem, we are given an array of n integers and some m querries. Our task is to create a program that calculates the integral value(round down) of the mean of the ranges given by the querries.Let’s take an example to understand the problem, Input −array = {5, 7, 8, 9, 10} m = 2; [0, 3], [2, 4]Output −7 9To solve this problem, we have two methods one is direct and the other is using prefix sum.In the direct approach, for each query, we will loop from the start index to the end index of the range. And ... Read More

Mean and Median of a matrix in C++

Narendra Kumar
Updated on 03-Jun-2020 07:29:59

1K+ Views

In this problem, we are given a 2D array of size n*n. Our task is to create a program that will print the mean and median of the matrix in C++.Mean is the average of the date set. In a matrix mean is the average of all elements of the matrix.Mean = (sum of all elements of the matrix)/(number of elements of the matrix)Median is the middlemost element of the sorted data set. For this, we will have to sort the elements of the matrix.Median is calculated as, If n is odd, median = matrix[n/2][n/2]If n is even, median = ... Read More

Advertisements