Found 7347 Articles for C++

Maximum Sum SubArray using Divide and Conquer in C++

Arnab Chakraborty
Updated on 21-Oct-2019 12:45:22

1K+ Views

Suppose we have one list of data with positive and negative values. We have to find the sum of contiguous subarray whose sum is largest. Suppose the list is containing {-2, -5, 6, -2, -3, 1, 5, -6}, then the sum of maximum subarray is 7. It is the sum of {6, -2, -3, 1, 5}We will solve this problem using Divide and Conquer method. The steps will be look like below −Steps −Divide the array into two partsFind the maximum of following threeMaximum subarray sum of left subarrayMaximum subarray sum of right subarrayMaximum subarray sum such that subarray crosses ... Read More

Maximum Possible Edge Disjoint Spanning Tree From a Complete Graph in C++

Arnab Chakraborty
Updated on 21-Oct-2019 12:25:24

167 Views

Suppose we have a complete graph; we have to count number of Edge Disjoint Spanning trees. The Edge Disjoint Spanning trees are spanning trees, where no two trees in the set have an edge in common. Suppose the N (number of vertices) is 4, then output will be 2. The complete graph using 4 vertices is like below −Two edge disjoint spanning trees are like −The maximum number of edge disjoint spanning tree from a complete graph, with N vertices will be $[\frac{n}{2}]$Example#include #include using namespace std; int maxEdgeDisjointSpanningTree(int n){    return floor(n/2); } int main() {    int n = 4;    cout

Sorting an array according to another array using pair in STL in C++

Arnab Chakraborty
Updated on 21-Oct-2019 12:20:28

715 Views

Suppose we have two different arrays. We have to sort one array based on the other array using C++ STL pair class. Consider two arrays are like A1 = [2, 1, 5, 4, 9, 3, 6, 7, 10, 8], and another array is like A2 = [A, B, C, D, E, F, G, H, I, J], The output will be like: A1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], A2 = [B, A, F, D, C, G, H, J, E, I]Here we are using the pairs of C++ STL. The pair is formed by taking one ... Read More

Sort the array of strings according to alphabetical order defined by another string in C++

Arnab Chakraborty
Updated on 21-Oct-2019 12:18:01

295 Views

Suppose we have an array of strings, and another string is there for the reference. We have to take the reference string and using the order of the characters in the reference string we will sort the string array. Here we are considering the strings in the array, and the reference string is in lower case letters.Suppose the string array is like: [“hello”, “programming”, “science”, “computer”, “india”], the reference string is like: “pigvxbskyhqzelutoacfjrndmw”, After sorting the output string will be like [“programming”, “india”, “science”, “hello”, “computer”]The task is simple. We have to traverse the reference string, then store the character ... Read More

Sort elements of the array that occurs in between multiples of K in C++

Arnab Chakraborty
Updated on 21-Oct-2019 12:15:15

75 Views

Suppose we have an array A, and another integer K. We have to sort the elements that are in between any two multiples of K. Suppose A is like [2, 13, 3, 1, 21, 7, 8, 13, 12], and K = 2. The output will be [2, 1, 3, 7, 13, 21, 8, 13, 12]. Here multiple of 2 are 2, 8 and 12, the elements in between 2 and 8 are 13, 3, 1, 21, 7, they will be sorted as 1, 3, 7, 13, 21, the elements between 8 and 12 is only 13, so that is already ... Read More

Check if a number is formed by Concatenation of 1, 14 or 144 only in C++

Arnab Chakraborty
Updated on 21-Oct-2019 10:27:08

129 Views

Here we will see one problem, that can tell that whether a string or a number is a concatenation of 1, 14 or 144 only. Suppose a string is “111411441”, this is valid, but “144414” is not valid.The task is simple, we have to fetch a single digit, double-digit and triple-digit number from the last, and check whether they match with any of these three (1, 14 and 144), if we get one match, divide the number with it, and repeat this process until the entire number is not exhausted.Example#include #include using namespace std; bool checkNumber(long long number) ... Read More

Check if a number is divisible by 41 or not in C++

Arnab Chakraborty
Updated on 21-Oct-2019 10:22:18

131 Views

Here we will see one program, that can check whether a number is divisible by 41 or not. Suppose a number 104413920565933 is given. This is divisible by 41.To check the divisibility, we have to follow this rule −Extract the last digit of the number/truncated number every timesubtract 4 * (last digit of the number calculated previous) to the truncated numberRepeat these steps as long as necessary.30873, so 3087 - 4*3 = 3075 3075, so 307 - 4 * 5 = 287 287, so 28 – 4 * 7 = 0 So, 30873 is divisible by 41.Example Live Demo#include #include ... Read More

Check if a number is divisible by 23 or not in C++

Arnab Chakraborty
Updated on 21-Oct-2019 10:19:20

170 Views

Here we will see one program, that can check whether a number is divisible by 23 or not. Suppose a number 1191216 is given. This is divisible by 23.To check the divisibility, we have to follow this rule −Extract the last digit of the number/truncated number every timeadd 7 * (last digit of the number calculated previous) to the truncated numberRepeat these steps as long as necessary.17043, so 1704 + 7*3 = 1725 1725, so 172 + 7 * 5 = 207 207, this is 9 * 23, so 17043 is divisible by 23.Example Live Demo#include #include using namespace ... Read More

Check if a number is a Krishnamurthy Number or not in C++

Arnab Chakraborty
Updated on 21-Oct-2019 10:16:33

1K+ Views

Here we will see how to check a number is Krishnamurty number or not. A number is Krishnamurty number, if the sum of the factorial of each digit is the same as the number. For example, if a number is 145, then sum = 1! + 4! + 5! = 1 + 24 + 120 = 145. So this is a Krishnamurty number, The logic is simple, we have to find the factorial of each number, and find the sum, then if that is the same as a given number, the number is Krishnamurty number. Let us see the code ... Read More

Check if a number has bits in alternate pattern - Set-2 O(1) Approach in C++

Arnab Chakraborty
Updated on 21-Oct-2019 10:12:32

368 Views

Let us consider we have an integer n. The problem is to check, whether this integer has alternate patterns in its binary equivalent or not. The alternate pattern means 101010….The approach is like: calculate num = n XOR (n >> 1), now if all bits of num is 1, then the num has alternating patterns.Example Live Demo#include #include using namespace std; bool isAllBitSet(int n){    if (((n + 1) & n) == 0)       return true;    return false; } bool hasAlternatePattern(unsigned int n) {    unsigned int num = n ^ (n >> 1);    return isAllBitSet(num); } int main() {    unsigned int number = 42;    if(hasAlternatePattern(number))       cout

Advertisements