Found 7347 Articles for C++

Sum of the minimum elements in all connected components of an undirected graph in C++

sudhir sharma
Updated on 06-Aug-2020 08:17:40

325 Views

In this problem, we are given an array arr of N numbers where arr[i] represents (i+1)th node. Also, there are M pairs of edges where u and v represent the node connected by the edge. Our task is to create a program to find the sum of the minimum elements in all connected components of an undirected graph. If a node has no connectivity to any other node, count it as a component with one node.Let’s take an example to understand the problem, Input arr[] = {2, 7, 5, 1, 2} m = 2 1 2 4 5Output 8Explanation Below is the graph ... Read More

Sum of the elements from index L to R in an array when arr[i] = i * (-1)^i in C++

sudhir sharma
Updated on 06-Aug-2020 08:12:58

300 Views

In this problem, we are given two numbers L and R. We also have an array arr[] such that arr[i] = i*(-1)^i. Our task is to create a program to calculate the sum of the element from index L to R in an array when arr[i] = i*(-1)^i.So, we need to find the sum of elements within the range [L, R] of the array.Let’s take an example to understand the problem, Input L = 2 , R = 6Output 4Explanation arr[] = {-1, 2, -3, 4, -5, 6} Sum = 2+ (-3) + 4 + (-5) + 6 = 4A simple solution to ... Read More

Sum of the digits of a number N written in all bases from 2 to N/2 in C++

sudhir sharma
Updated on 06-Aug-2020 08:10:56

113 Views

In this problem, we are given a number N. Our task is to create a program to find the sum of the digits of the number N in bases from 2 to N/2.So, we have to convert the base of the number to all bases from 2 to N/2 i.e. for n = 9, bases will be 2, 3, 4. And the find the sum of all digits in these bases.Let’s take an example to understand the problem, Input N = 5Output 2Explanation  base from 2 to N/2 is 2. 52 = 101, sum of digits is 2.To, solve this problem, we take ... Read More

Sum of the alternate nodes of linked list in C++

sudhir sharma
Updated on 06-Aug-2020 08:08:29

440 Views

In this problem, we are given a linked list. Our task is to print the sum of alternate nodes of the linked list.Linked list is a sequence of data structure which are connected together via links.Now, let’s get back to the problem. Here, we will add alternate nodes of the linked list. This means we will add nodes are positions 0, 2, 4, 6, …Let’s take an example to understand the problem,  Input 4 → 12 → 10 → 76 → 9 → 26 → 1Output 24Explanation considering alternate strings − 4 + 10 + 9 + 1 = 24To solve this problem, ... Read More

Sum of the alphabetical values of the characters of a string in C++

sudhir sharma
Updated on 06-Aug-2020 08:05:11

1K+ Views

In this problem, we are given an array of string str[]. Our task is to find the score of all strings in the array. The score is defined as the product of the position of the string with the sum of the alphabetical values of the characters of the string.Let’s take an example to understand the problem, Input str[] = {“Learn”, “programming”, “tutorials”, “point” }Explanation Position of “Learn” − 1 →sum = 12 + 5 + 1 + 18 + 14 = 50. Score = 50Position of “programming” − 2 →sum = 16 + 18 + 15 + 7 + 18 + ... Read More

Bessel’s Interpolation in C++

sudhir sharma
Updated on 06-Aug-2020 08:01:38

1K+ Views

Interpolation is a type of estimation technique of unknown value which lies between know values. Interpolation is the process of constructing new data points between the range of a discrete set of know data points.An application or reason to use interpolation is that it might reduce computation costs. When the formula (function) to calculate certain values is too complicated or costly to compute, we prefer using interpolation. A few data points are calculated using the original function, the rest of them can be estimated using interpolation. These may not be completely accurate but fairly close!So basically here the reduced computation ... Read More

Alexander Bogomolny’s UnOrdered Permutation Algorithm in C++

sudhir sharma
Updated on 06-Aug-2020 07:53:40

168 Views

Here, we are given a number N. Our task is to find unorder permutation of N using Alexander Bogomolny’s UnOrdered Permutation Algorithm.Let’s discuss permutation first, A permutation is the number of ways an item in a set can be uniquely ordered is called a permutation.Example − Permutation of {4, 9, 2} would be {4, 9, 2}, {4, 2, 9}, {9, 4, 2}, {9, 2, 4}, {2, 4, 9} and {2, 9, 4}.Permutations have found usage in defining switching networks in computer networking, parallel processing and also used in a variety of cryptographic algorithms.Alexander Bogomolny's Unordered Permutation AlgorithmThis algorithm computes all ... Read More

Aho-Corasick Algorithm for Pattern Searching in C++

sudhir sharma
Updated on 06-Aug-2020 07:51:00

638 Views

In this problem, we are given an input string and an array arr[]. Our task is to find all occurrences of all words of the array in the string. For this, we will be using the Aho-Corasick Algorithm for Pattern Searching.String and pattern searching is an important thing in programming. And in programming, the better the algorithm the more practical uses it can have. Aho-Corasick algorithm is a very important and powerful algorithm that makes string searching easy. It is kind of a dictionary matching algorithm, matching all the strings simultaneously. The algorithm uses the Trie data structure for its ... Read More

Queries on number of Binary sub-matrices of Given size in C++

Ayush Gupta
Updated on 09-Sep-2020 07:35:21

84 Views

In this problem, we are given a binary matrix bin[][] of size nXm. Our task is to solve all q queries. For query(x, y), we need to find the number of submatrix of size x*x such that all the elements of array y (binary number).Problem descriptionHere, we need to count the total number of sub-matrix of a given size that consists of only one of the two bits i.e. sub-matrix will all elements 0/1.Let’s take an example to understand the problem, Inputn = 3 , m = 4 bin[][] = {{ 1, 1, 0, 1} { 1, 1, 1, 0} ... Read More

Queries on count of points lie inside a circle in C++

Ayush Gupta
Updated on 09-Sep-2020 07:38:06

508 Views

In this problem, we are given n points that lie of a 2D plane, each coordinate is (x, y). Our task is two solve queries. For each query, we are given an integer R. We need to find the count of points lying inside the circle, taking the circle’s center at origin and radius R.Problem descriptionFor each query, we need to find the total number of points out of n points that lie inside the circle (i.e. inside the circumference) of radius R and center point origin (0, 0).Let’s take an example to understand the problem betterInputn = 4 2 ... Read More

Advertisements