Found 1339 Articles for C

All possible numbers of N digits and base B without leading zeros?

Arnab Chakraborty
Updated on 31-Jul-2019 13:11:26

231 Views

Here we will see one problem, We have N and base B. Our task is to count all N digit numbers of base B without any leading 0s. So if N is 2 and B is 2 there will be four numbers 00, 01, 10, 11. So only two of them are valid for this section. These are 10, 11, there are no leading 0s.If the base is B, then there are 0 to B – 1 different digits. So BN number of different N digit values can be generated (including leading 0s). The first digit is 0m if we ... Read More

All possible co-prime distinct element pairs within a range [L, R]?

Arnab Chakraborty
Updated on 31-Jul-2019 13:08:45

279 Views

Here we will see how to count number of co-prime pairs from the range, where a number will not appear more than a single pair.Before discussing the logic, let us see what are the co-prime numbers? The co-prime numbers are those numbers which has only one positive integer divisor, that is 1. In other words, we can say the GCD of these two numbers is 1.Here we are providing the lower and upper limit. If the lower and upper limits are 1 and 6, then there are three pairs. These are (1, 2), (3, 4) and (5, 6)The approach for ... Read More

All possible binary numbers of length n with equal sum in both halves?

Arnab Chakraborty
Updated on 31-Jul-2019 13:06:08

254 Views

Here we will see all possible binary numbers of n bit (n is given by the user) where the sum of each half is same. For example, if the number is 10001 here 10 and 01 are same because their sum is same, and they are in the different halves. Here we will generate all numbers of that type.AlgorithmgenAllBinEqualSumHalf(n, left, right, diff)left and right are initially empty, diff is holding difference between left and rightBegin    if n is 0, then       if diff is 0, then          print left + right       ... Read More

All palindrome numbers in a list?

Arnab Chakraborty
Updated on 31-Jul-2019 13:00:48

327 Views

Here we will see one simple problem. We have to find all numbers that are palindrome in nature in a given list. The approach is simple, take each number from list and check it is palindrome or not, and print the number.AlgorithmgetAllPalindrome(arr, n)Begin    for each element e in arr, do       if e is palindrome, then          print e       end if    done EndExample#include #include using namespace std; bool isPalindrome(int n){    int reverse = 0, t;    t = n;    while (t != 0){       ... Read More

Add two numbers represented by linked lists?

Nishu Kumari
Updated on 31-Jul-2019 12:58:13

374 Views

We're given two singly linked lists, where each node stores one digit of a number. The digits are arranged from left to right, just like how we normally write numbers. For example: 7 -> 2 -> 4 -> 3 represents the number 7243. Our task is to add these two numbers and return the sum as a new linked list in the same (left-to-right) order. The input number can contain zeroes at the start, but in the output, there should not be any leading zeros. Let's understand this with a diagram given below - Scenario 1 Input: List 1 ... Read More

C/C++ Program to Count trailing zeroes in factorial of a number?

Arnab Chakraborty
Updated on 31-Jul-2019 12:54:40

324 Views

Here we will see how to calculate the number of trailing 0s for the result of factorial of any number. So if the n = 5, then 5! = 120. There is only one trailing 0. For 20!, it will be 4 zeros as 20! = 2432902008176640000.The easiest approach is just calculating the factorial and count the 0s. But this approach fails for large value of n. So we will follow another approach. The trailing zeros will be there, if the prime factors are 2 and 5. If we count the 2s and 5s we can get the result. For ... Read More

C/C++ Program to check whether it is possible to make a divisible by 3 number using all digits in an array?

Arnab Chakraborty
Updated on 31-Jul-2019 12:51:07

164 Views

In this section we will see if one array is given with n numbers, we have to check if we make a number using all of the elements of these numbers, that number will be divisible by 3 or not. If the array elements are {15, 24, 23, 13}, then the we can make integer like 15242313. It will be divisible by 3.AlgorithmcheckDivThree(arr)Begin    rem := 0    for each element e in arr, do       rem := (rem + e) mod 3    done    if rem is 0, then       return true    end ... Read More

Add elements of given arrays with given constraints?

Arnab Chakraborty
Updated on 02-Jul-2020 09:29:33

154 Views

Here we will see one problem. We will add two array elements and store them into another array. But we will follow some constraints. These constraints are like below −Addition should be stated from 0th index of both of the arraySplit the sum if it is more than one-digit number, and place each digit to the corresponding locationsRemaining digits of the larger input array will be stored at output arrayLet us see the algorithm to get the idea.AlgorithmaddArrayConstraints(arr1, arr2)Begin    define empty vector out    i := 0    while i is less than both arr1.length and arr2.length, do   ... Read More

Add 1 to number represented as array (Recursive Approach)?

Arnab Chakraborty
Updated on 31-Jul-2019 12:24:28

320 Views

In this section we will see one interesting problem. Suppose one number is given. We have to increase this number by 1. This is extremely simple task. But here we will place the number as an array. each digit of that number is placed as an element of the array. If the number is 512, then it will be stored as {5, 1, 2}. And also we have to increase the number using recursive approach. Let us see the algorithm to get the clear idea.Algorithmincrement(arr, n, index)Initially the default value of index is 0begin    if index < n, then ... Read More

Add 1 to a number represented as linked list?

Arnab Chakraborty
Updated on 31-Jul-2019 12:20:04

214 Views

Here we will see how to add 1 with a number stored into a linked list. In the linked list, each digit of the numbers is stored. If the number is 512, then it will be stored like below −512 = (5)-->(1)-->(2)-->NULLWe are providing the list into the increment function. That will return another list after adding 1 with it. Here we are using the C++ STL linked list. Let us see the algorithm to bet better idea.AlgorithmincrementList(l1)Begin    carry := 1    res := an empty list    for each node n from l1, scan from last to first, ... Read More

Advertisements