Found 7347 Articles for C++

Count Numbers with N digits which consists of even number of 0's in C++

Sunidhi Bansal
Updated on 01-Dec-2020 12:02:25

114 Views

We are given a number N as input. The goal is to find all N digit numbers that have an even number of 0’s as digits. The number also could have preceding zeros like in case of N=3 numbers included will be 001, 002, 003….010….so on.Let us understand with examples.Input − N=4Output − Count of no. with N digits which consists of even number of 0's are − 7047Explanation − All 4 digits numbers would be like −Smallest will be 0000, then 0011, 0012, 0013, 0014…..Highest will be 9900.Input − N=5Output − Count of no. with N digits which consists ... Read More

Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B in C++

Sunidhi Bansal
Updated on 01-Dec-2020 12:01:08

491 Views

We are given an input N. The goal is to find all pairs of A, B such that 1

Program to find nth ugly number in C++

Arnab Chakraborty
Updated on 26-Nov-2020 08:06:06

234 Views

Suppose we have a number n; we have to find the nth ugly number. As we know that the ugly numbers are those numbers, whose prime factors are only 2, 3 and 5. So if we want to find 10th ugly number, the output will be 12, as the first few ugly numbers are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 and so on.To solve this, we will follow these steps:Define an array v of size(n + 1)if n is same as 1, then:return 1two := 2, three := 3, five := 5twoIdx := 2, threeIdx := 2, fiveIdx := 2for initialize i := 2, when i

Program to find median of two sorted lists in C++

Arnab Chakraborty
Updated on 26-Nov-2020 07:56:38

161 Views

Suppose we have two sorted lists. We have to find the median of these two lists. So if the arrays are like [1,5,8] and [2,3,6,9], then the answer will be 5.To solve this, we will follow these steps:Define a function solve(), this will take an array nums1, an array nums2,if size of nums1 > size of nums2, then:return solve(nums2, nums1)x := size of nums1, y := size of nums2low := 0, high := xtotalLength := x + ywhile low

Program to count number of consecutive lists whose sum is n in C++

Arnab Chakraborty
Updated on 26-Nov-2020 07:46:21

304 Views

Suppose we have a number n, we have to find the number of lists of positive consecutive values that sum up to n.So, if the input is like n = 15, then the output will be 4, as The possible lists are: [1, 2, 3, 4, 5], [4, 5, 6], [7, 8], and [15].To solve this, we will follow these steps:begin := 1, end := 1, x := (n + 1)sum := 0while end = n, do:if sum is same as n, then:(increase count by 1)sum := sum - begin(increase begin by 1)(increase end by 1)return count + 1Let us ... Read More

Lonely Pixel I in C++

Arnab Chakraborty
Updated on 19-Nov-2020 10:17:47

159 Views

Suppose we have a picture consisting of black and white pixels, we have to find the number of black lonely pixels. Here the picture is represented by a 2D char array consisting of 'B' and 'W', for the black and white pixels respectively.A black lonely pixel is actually 'B' that located at a specific position where the same row and same column don't have any other black pixels.If the input is like −WWBWBWBWWOutput will be 3. Because all the three 'B's are black lonely pixels.To solve this, we will follow these steps −n := size of picturem := (if n ... Read More

Inorder Successor in BST II in C++

Arnab Chakraborty
Updated on 19-Nov-2020 10:15:29

306 Views

Suppose we have a node in a binary search tree, we have to find the in-order successor of that node in the BST. If there is no in-order successor, return null. As we know that the successor of a node is the node with the smallest key greater than value of node.We will have direct access to the node but not to the root of the tree. Here each node will have a reference to its parent node. Below is the definition for Node −class Node {    public int val;    public Node left;    public Node right;   ... Read More

The Maze II in C++

Arnab Chakraborty
Updated on 19-Nov-2020 10:13:27

432 Views

Suppose there is a ball in a maze with empty spaces and walls. Now the ball can go through empty paths by rolling any direction like up, down, left or right, but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction.We have to start position of ball, the destination and the maze, we have to find the shortest distance for the ball to stop at the destination. Here the distance is actually defined by the number of empty cells, that are covered by the ball (Excluding start position, including starting position). ... Read More

The Maze in C++

Arnab Chakraborty
Updated on 19-Nov-2020 10:05:40

3K+ Views

Suppose there is a ball in a maze with empty spaces and walls. Now the ball can go through empty paths by rolling any direction like up, down, left or right, but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction.We have to start position of ball, the destination and the maze, we have to check whether the ball could stop at the destination. The maze is represented by one 2D array. Here 1 indicates the wall and 0 indicates the empty space. The borders of the maze are all walls. ... Read More

Max Consecutive Ones II in C++

Arnab Chakraborty
Updated on 19-Nov-2020 10:00:51

1K+ Views

Suppose we have a binary array; we have to find the maximum number of consecutive 1s in this array if we can flip at most one 0.So, if the input is like [1, 0, 1, 1, 0], then the output will be 4 because if we flip the first zero will get the maximum number of consecutive 1s. After flipping, the maximum number of consecutive 1s is 4.To solve this, we will follow these steps −ret := 1, n := size of numsif not n is non-zero, then −return 0j := 0, zero := 0for initialize i := 0, when ... Read More

Advertisements