Found 7197 Articles for C++

Program to find the Area and Perimeter of a Semicircle in C++

Ayush Gupta
Updated on 16-Sep-2020 17:30:28

317 Views

In this problem, we are given a value that denotes the radius of a semicircle. Our task is to create a program to find the Area and Perimeter of a Semicircle in C++.SemiCircle is a closed figure that is half of a circle.Let’s take an example to understand the problem, InputR = 5Outputarea = 39.25 perimeter = 15.7Solution ApproachTo solve the problem, we will use the mathematical formula for the area and perimeter of a semi-circle which is derived by dividing the area of circle by 2.Area of semicircle, A= $½(\prod^*a^2)=1.571^*a^2$Perimeter of semicircle, P =(π*a)Area of semicircle, area = $½(π^*a^2)$Program ... Read More

Smallest Rotation with Highest Score in C++

Arnab Chakraborty
Updated on 02-Jun-2020 11:18:15

156 Views

Suppose we have an array A, we may rotate it by a K so that the array becomes A[K], A[K+1], A{K+2], ... A[A.length - 1], A[0], A[1], ..., A[K-1]. Then, any entries that are less than or equal to their index are worth 1 point.So for example, let we have an array [2, 4, 1, 3, 0], and we rotate by K = 2, it becomes [1, 3, 0, 2, 4]. This is worth 3 points because 1 > 0 [gain no point], 3 > 1 [gain no point], 0

Preimage Size of Factorial Zeroes Function in C++

Arnab Chakraborty
Updated on 02-Jun-2020 11:15:02

217 Views

Suppose we have a function f(x), this will return the number of zeroes at the end of factorial of x. So for f(3) = 0 because 3! = 6 has no zeroes at the end, while f(11) = 2 because 11! = 39916800 has 2 zeroes at the end. Now when we have K, we have to find how many non-negative integers x have the property that f(x) = K.So if the input is like K = 2, then the answer will be 5.To solve this, we will follow these steps −Define a function ok(), this will take x,ret := 0for initialize i := 5, when i

K-th Smallest Prime Fraction in C++

Arnab Chakraborty
Updated on 02-Jun-2020 11:12:58

545 Views

Suppose we have one sorted list, there is 1 and some prime numbers, now for every p < q in the list, we will consider fraction p/q, then we have to find which is the kth smallest fraction. We have to return an array as answer, so ans[0] will be p and ans[1] will be q.So if the input is like [1, 3, 5, 7], and k = 2, then the answer will be 1/5, as the fractions are 1/3, 1/5, 1/7, 3/5, 3/7, 5/7, the second smallest is 1/5.To solve this, we will follow these steps −Define Data, this ... Read More

Transform to Chessboard in C++

Arnab Chakraborty
Updated on 02-Jun-2020 11:09:24

520 Views

Suppose we have one N x N board contains only 0s and 1s. Now in each move, we can swap any 2 rows, or any 2 columns. We have to find the minimum number of moves to transform the board into a "chessboard". If the solution does not exist, then return -1.So if the input is like −Then the output will be 2, as first two columns in the first move, then board will be like −Then swap second and 3rd rows −This is the chessboardTo solve this, we will follow these steps −n := size of bfor initialize i ... Read More

Reaching Points in C++

Arnab Chakraborty
Updated on 02-Jun-2020 10:52:50

176 Views

Suppose we have a starting points (sx, sy), and target point (tx, ty), we have to check whether a sequence of moves exists from the start point to the end point. Here move consists of taking a point (x, y) and transforming it to either (x, x+y) or (x+y, y).So if the inputs are (1, 1) and (4, 5), then the answer will be true, this is because move (1, 1) to (2, 1), then (3, 1), then (4, 1), then (4, 5).To solve this, we will follow these steps −while tx > sx and ty > sy, do −if ... Read More

Swim in Rising Water in C++

Arnab Chakraborty
Updated on 02-Jun-2020 10:50:40

315 Views

Suppose we have one N x N grid, each square grid[i][j] represents the elevation at that point (i, j). Now consider it has started raining. At time t, the depth of the water everywhere is t. We can swim from a square to another 4-directionally adjacent square when elevation of both squares individually is at most t. We can swim infinite distance in zero time.We should start from position (0, 0). We have to find the least time until we can reach the bottom right square (N-1, N-1)So if the input is like0123424232221512131515161117181920109876The correct way is colored. So the answer ... Read More

Cracking the Safe in C++

Arnab Chakraborty
Updated on 02-Jun-2020 10:48:04

377 Views

Suppose we have a box that is protected by a password. The password is a sequence of n digits where each digit can be one of the first k digits 0, 1, ..., k-1. So, when we are putting a password, the last n digits entered will automatically be matched against the correct password.So for example, assuming the correct password is "563", if we put "285639", the box will open because the correct password matches the suffix of the entered password. We have to find any password of minimum length that is guaranteed to open the box at some point ... Read More

Cherry Pickup in C++

Arnab Chakraborty
Updated on 02-Jun-2020 10:44:52

414 Views

Suppose we have one N x N grid, this is filled with cherries. Each cell has one of the possible integers as follows −0 − Indicates cell is empty, so we can pass through1 − Indicates cell contains a cherry, that we can pick up and pass through-1 − Indicates the cell is containing a thorn that blocks the wayWe have to collect maximum number of cherries using these few rules −Start from position (0, 0) and end at (N-1, N-1) by moving right or down through valid path cellsAfter reaching the cell (N-1, N-1), returning to (0, 0) by ... Read More

Find K-th Smallest Pair Distance in C++

Arnab Chakraborty
Updated on 02-Jun-2020 10:42:28

291 Views

Suppose we have an integer array; we have to find the kth smallest distance among all the pairs. The distance of a pair (A, B) is actually the absolute difference between A and B. So if the input is like [1, 3, 8], then all possible pairs are [1, 3], [3, 8], [1, 8], then when k = 2, the second smallest distance is 5 (8 - 3).To solve this, we will follow these steps −n := size of nums, x := 0for initialize i := 0, when i < n, update (increase i by 1), do −x := maximum ... Read More

Advertisements