Found 7347 Articles for C++

Check if a pair with given product exists in Linked list in C++

Arnab Chakraborty
Updated on 22-Oct-2019 11:55:49

111 Views

We have a set of elements. And a product K. The task is to check whether there exist two numbers in the linked list, whose product is same as K. If there are two numbers, then print them, if there are more than two numbers, then print any of them. Suppose the linked list is like this {2, 4, 8, 12, 15}, and k value is 16. then it will return (2, 8)We will solve this using the hashing technique. Take one hash table, and mark all elements as 0. Now iteratively mark all elements as 1 in hash table, ... Read More

Check if a pair with given product exists in a Matrix in C++

Arnab Chakraborty
Updated on 22-Oct-2019 11:51:52

82 Views

We have one matrix of order N x M. And a product K. The task is to check whether a pair with the given product is present in the matrix or not.Suppose a matrix is like below −12345678910111213141516Now if the K is 42, then there is a pair like (6, 7)To solve this problem, we will use hashing. We will create hash table by taking all elements of the matrix. We will start traversing the matrix, while traversing, check whether the current element of the matrix is divisible by the given product, and when the product K is divided by ... Read More

Check if a number is divisible by all prime divisors of another number in C++

Arnab Chakraborty
Updated on 22-Oct-2019 11:44:31

390 Views

Suppose there are two numbers. We have to check whether a number is divisible by all of the prime factors or the second number or not. Suppose a number is 120. The prime factors are {2, 3, 5}, another number is 75, here the prime factors are {3, 5}. As 120 is divisible by 3 and 5 also, then the decision is yes.If one number is 1, then it has no prime divisors, so answer is True. Otherwise we have to find the GCD of these two numbers. If GCD is 1, then they are co-prime. So answer is false. ... Read More

Check if a number is Bleak in C++

Arnab Chakraborty
Updated on 22-Oct-2019 11:43:02

304 Views

Here we will see whether a number is Bleak or not. A number is said to be bleak if it cannot be represented as sum of a positive number x and set bit count in x. So x + set_bit_count(x) is not equal to n , for any non-negative number x.The concept is very simple, if the set bit count + the number is not same as the number, then that is Bleak, otherwise it is not.Example Live Demo#include using namespace std; int set_bit_count(int x) {    unsigned int bit_count = 0;    while (x != 0) {     ... Read More

Check if a number is an Unusual Number or not in C++

Arnab Chakraborty
Updated on 22-Oct-2019 11:40:15

136 Views

Here we will see a number is unusual number or not. A number is said to be unusual if the greatest prime factor of the number is strictly greater than square root of the number. Some of the unusual numbers are: 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 20, 21, 22, 23, 26, 28, 29, 31, 33, 34, 35, 37, 38, 39, 41, 42, 43, 44, 46To solve this, we will try to find the largest prime factor, then check whether the factor is greater than square root of the number or not. If yes, ... Read More

Check if a Linked List is Pairwise Sorted in C++

Arnab Chakraborty
Updated on 22-Oct-2019 11:37:50

111 Views

We have a list L, with n elements. We have to check whether the list is pairwise sorted or not. Suppose the list is like {8, 10, 18, 20, 5, 15}. This is pairwise sorted as (8, 10), (18, 20), (5, 15) are sorted. If the list has odd number of elements, then last one will be ignored.The approach is too simple, traverse the number from left to right. Two consecutive elements are taken, and check whether they are sorted or not, if any one pair is not sorted, return false, if no pair is found, that is unsorted, return ... Read More

Check if a linked list is Circular Linked List in C++

Arnab Chakraborty
Updated on 22-Oct-2019 11:28:11

2K+ Views

Here we will see, hoe to check a linked list is circular linked list or not. To check whether the linked list is circular or not, we will store the header node into some other variable, then traverse the list, if we get null at the next part of any node, then that is not circular, otherwise we will check the next node is same as the stored node or not, if so then that is circular.Example Live Demo#include using namespace std; class Node{    public:    int data;    Node *next; }; Node* getNode(int data){    Node *newNode = ... Read More

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

Arnab Chakraborty
Updated on 22-Oct-2019 11:24:35

232 Views

Here we will see how to check a number is divisible by 13 or not. In this case the number is very large number. So we put the number as string.A number will be divisible by 13, if the number satisfies the following situations −A number is divisible by 13 if and only if we get the alternating sum i.e. alternatively adding and subtracting of blocks of three numbers from right to left is divisible by 13. For example, 2911285 is divisible by 13 because the alternating sum of blocks of size 3 is 2 – 911 + 285 = ... Read More

Check if a large number can be divided into two or more segments of equal sum in C++

Arnab Chakraborty
Updated on 22-Oct-2019 11:20:20

79 Views

Here we will see a program, that can check whether a number can be divided into more than one segments with equal sum. Suppose a number is like 74325, then this can be segmented into three parts (7), (4, 3), (2, 5), all are of same um value.We have to follow these steps to solve this problem.Take the number as stringuse an array to hold prefix sum of the arrayNow traversing from second element to last element, and the first segment will be 0 to i-1, whose sum will be placed at prefix_sum[i - 1]Use another variable which traverses from ... Read More

Check if a given number is Pronic in C++

Arnab Chakraborty
Updated on 22-Oct-2019 11:16:34

709 Views

Here we will see, how to check whether a number is Pronic number or not. A number that can be arranged to form a rectangle, are called the pronic numbers. First few pronic numbers are: 0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342. The pronin numbers are product of two consecutive integers. So a pronic number n = x * (x + 1).Here we will check and generate some pronic numbers.Example Live Demo#include #include using namespace std; bool isPronicNumber(int num) {    for (int i = 0; i

Advertisements