C++ Articles - Page 296 of 719

Count number of right triangles possible with a given perimeter in C++

Sunidhi Bansal
Updated on 29-Aug-2020 08:44:24

349 Views

We are given a perimeter P of a triangle. Perimeter is the sum of all sides of the triangle. The goal is to find the number of right triangles that can be made which have the same perimeter.If the sides of the triangle are a, b and c. Then a + b + c = P and a2 + b2 = c2 ( pythagoras theorem for any combination of a, b, and c )We will check this by taking a from 1 to p/2 and b from a+1 to p/3. Then c = p-a-b (a+b+c=p)For all right triangles, apply Pythagoras ... Read More

Count number of squares in a rectangle in C++

Sunidhi Bansal
Updated on 29-Aug-2020 08:41:48

728 Views

We are given with a rectangle of length L and breadth B, such that L>=B. The goal is to find the number of squares that a rectangle of size LXB can accommodate.Above figure shows a rectangle of size 3 X 2. It has 2, 2X2 squares and 6, 1X1 squares in it.Total squares= 6+2=8.Every rectangle of size LXB has L*B number of 1X1 squares.Biggest squares are of size BXB.For L=B=1, squares = 1.For L=B=2, squares = 1 + 4 = 5. ( 1 of 2X2, 4 of 1X1 )For L=B=3, squares = 1 + 4 + 9 = 14. ( ... Read More

Count number of smallest elements in given range in C++

Sunidhi Bansal
Updated on 29-Aug-2020 08:37:30

301 Views

We are given an array of integers of size N. Variables L and R define a range between 1 and N. The goal is to find the number of smallest elements that lie in range L and R such that L>=1 and R

Count number of primes in an array in C++

Sunidhi Bansal
Updated on 29-Aug-2020 08:33:54

10K+ Views

We are given with an array of numbers. The goal is to find the count of prime numbers in that array.A prime number is the one which is divisible by 1 and the number itself. It has only two factors. We will check if the number is prime starting from the first element till the last and increase the count of prime numbers found so far.To check if the number N is prime, check if numbers between the range [2 to N/2], fully divides N. If yes then it is non-prime. Else it is prime.Let’s understand with examples.Input − arr[]= ... Read More

Maximize profit when divisibility by two numbers have associated profits in C++

Sunidhi Bansal
Updated on 29-Aug-2020 08:30:31

143 Views

We are given with five integers N, A, B, X and Y. The goal is to maximize the profit by checking that between numbers in range [ 1 to N ] , ifA number is divisible by A, then profit increases by X.A number is divisible by B then profit increases by Y.A profit can be added once only, for a particular number in range.Let’s understand with examples.Input − N=4, A=2, B=3, X=2, Y=3Output − Maximized profit is − 7Explanation −2, 4 are divisible by A ( 2 ). Profit increases from 0 to 2, then 2 to 4 ( ... Read More

Maximum number of partitions that can be sorted individually to make sorted in C++

Sunidhi Bansal
Updated on 29-Aug-2020 08:27:47

679 Views

We are given with an array of N numbers with elements lying in range 0 and N-1. The elements are unsorted. The goal is to find the maximum number of partitions of the array which can be sorted individually and then can be concatenated to make a whole sorted array of length N.Each partition is chosen such that elements in it are unsorted. For N numbers ranging between 0 and N-1, sorted elements are at index equal to the value. Arr[i] = i.We will solve this by comparing each element by maximum value found so far on its left. When ... Read More

Find a permutation that causes worst case of Merge Sort in C++

Arnab Chakraborty
Updated on 28-Aug-2020 08:25:54

158 Views

Suppose we have a set of elements; we have to find which permutation of these elements would result in worst case of Merge Sort? As we know asymptotically, merge sort always consumes O (n log n) time, but some cases need more comparisons and consumes more time. Here we have to find a permutation of input elements that will require higher number of comparisons when sorted implementing a typical Merge Sort algorithm.So, if the input is like [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26] , then the output will be [11, ... Read More

Find a pair with given sum in a Balanced BST in C++

Arnab Chakraborty
Updated on 28-Aug-2020 08:20:06

136 Views

Suppose we have a balanced binary search tree and a target sum, we have to define a method that checks whether it is a pair with sum equals to target sum, or not. In this case. We have to keep in mind that the Binary Search Tree is immutable.So, if the input is likethen the output will be (9 + 26 = 35)To solve this, we will follow these steps −Define stacks s1, s2done1 := false, done2 := falseval1 := 0, val2 := 0curr1 := root, curr2 := rootinfinite loop, do −while done1 is false, do −if curr1 is not ... Read More

Construct a linked list from 2D matrix in C++

Arnab Chakraborty
Updated on 27-Aug-2020 14:20:57

1K+ Views

Suppose we have one matrix, we have to convert it to 2d linked list using recursive approach.The list will have the right and down pointer.So, if the input is like102030405060708090then the output will beTo solve this, we will follow these steps −Define a function make_2d_list(), this will take matrix mat, i, j, m, n, if i and j are not in the matrix boundary, then −return nulltemp := create a new node with value mat[i, j]right of temp := make_2d_list(mat, i, j + 1, m, n)down of temp := make_2d_list(mat, i + 1, j, m, n)return tempExampleLet us see the ... Read More

Construct a linked list from 2D matrix (Iterative Approach) in C++

Arnab Chakraborty
Updated on 27-Aug-2020 14:18:10

226 Views

Suppose we have one matrix, we have to convert it to 2d linked list using iterative approach. The list will have the right and down pointer.So, if the input is like102030405060708090then the output will beTo solve this, we will follow these steps −real_head := NULLDefine an array head_arr of size: m.for initialize i := 0, when i < m, update (increase i by 1), do −head_arr[i] := NULLfor initialize j := 0, when j < n, update (increase j by 1), do −p := new tree node with value mat[i, j]if real_head is null, then −real_head := pif head_arr[i] is ... Read More

Advertisements