Found 7347 Articles for C++

Find position of the only set bit in C++

sudhir sharma
Updated on 16-Mar-2021 05:19:25

939 Views

In this problem we are given a number N which has only one set bit in its binary representation. Our task is to find the position of the only set bit. If the number has only one set bit return the position of the number otherwise print invalid number.Let’s take an example to understand the problem, InputN = 32Output6ExplanationBinary representation of the number is 10000.Solution ApproachOne fact to know before we proceed further is the number will have only 1 set bit if it is a power of 2. Otherwise it will have more number of set bits.A simple solution ... Read More

Find position of the given number among the numbers made of 4 and 7 in C++

sudhir sharma
Updated on 16-Mar-2021 05:15:10

318 Views

In this problem we are given a number N. Our task is to Find position of the given number among the numbers made of 4 and 7. The series consisting of 4 and 7 only is 4, 7, 44, 47, 74, 77, 444….Let’s take an example to understand the problem, InputN = 5Output74ExplanationSeries upto 5 terms is 4, 7, 44, 47, 74…Solution ApproachA simple solution to the problem is based on finding the pattern in the series.Here, every even position contains 7 in the end.And every odd position contains 4 in the end.So, we can find the series by going ... Read More

Find position of left most dis-similar bit for two numbers in C++

sudhir sharma
Updated on 16-Mar-2021 05:13:08

144 Views

In this problem we are given two numbers, num1 and num2. Our task is to find the position of the leftmost dis-similar bit for two numbers. We need to print the first bit which is not the same for both numbers in their respective binary representation. The length of both needs to be the same to find the bit. This is done by appending 0’s to the start of the number with less bits.Let’s take an example to understand the problem, Inputnum1 = 4, num2 = 7Output1ExplanationBinary representation of 4 is 100Binary representation of 7 is 111The first bit is ... Read More

Find position of an element in a sorted array of infinite numbers in C++

sudhir sharma
Updated on 16-Mar-2021 05:10:36

316 Views

In this problem we are given an array consisting of infinite sorted numbers. Our task is to Find position of an element in a sorted array of infinite numbers.Let’s take an example to understand the problem, Inputarr[] = {2, 4, 6, 8, 9, 12, 14, 17, ….}, ele = 9Output4ExplanationSolution ApproachFor searching elements from a sorted array efficiently, we will be using the binary searching method. Here, single the end point is not known, we will modify the algorithm a bit.We will fix the start pointer to first position, then take the end pointer to second position. After this, we ... Read More

Find politeness of a number in C++

sudhir sharma
Updated on 16-Mar-2021 05:08:54

86 Views

In this problem, we are given a positive integer N. Our task is to find the politeness of a number.Polite Number is a number which can be expressed as a sum of two or more consecutive numbers.Politeness of a number is defined as the number of ways the number can be expressed as sum of consecutive integers.Let’s take an example to understand the problem, Inputn = 5Output1Explanation2 + 3 = 5, is the only consecutive sum.Solution ApproachA simple solution to the problem is to check all consecutive numbers till N and if their sum is equal to N, increase count ... Read More

Find perimeter of shapes formed with 1s in binary matrix in C++

sudhir sharma
Updated on 16-Mar-2021 05:06:43

181 Views

In this problem, we are given a binary matrix bin[][] of size nXm consisting of 0’s and 1’s only. Our task is to Find perimeter of shapes formed with 1s in a binary matrix.The perimeter taken will cover the figure from all sides, i.e.For 1 single value, the perimeter is 4.Let’s take an example to understand the problem, Inputbin[][] = [1, 0] [1, 0]Output6ExplanationThe cells (0, 0) and (1, 0) are connected making a rectangle of sides 2 and 1. The perimeter is 6.Solution ApproachA simple solution to the problem is simply finding all one and their ... Read More

Find Perimeter of a triangle in C++

sudhir sharma
Updated on 16-Mar-2021 05:04:14

2K+ Views

In this problem, we will see the perimeter of a triangle, formula for the perimeter of different types of triangle and program to find them.Perimeter is defined as the total distance about the figure. Basically, it is the sum of all sides of the given figure.Perimeter of a triangleThe perimeter of a triangle is the sum of all its three sides (triangle is a three sides figure).Formula, Perimeter = sum of all sidesPerimeter = x + y + zProgram to find the perimeter of a triangle, Example Live Demo#include using namespace std; int calcPerimeter(int x, int y, int z ){ ... Read More

Find pairs with given sum in doubly linked list in C++

sudhir sharma
Updated on 16-Mar-2021 14:05:05

207 Views

In this problem, we are given a doubly linked list and a value sum. Our task is to find pairs with a given sum in a doubly linked list.Let’s take an example to understand the problem, Inputhead − 2 5 6 9 12 x = 11Output(2, 9), (5, 6)ExplanationFor pairs (2, 9), the sum of values is 11 For pairs (5, 6), the sum of values is 11Solution ApproachA simple solution to the problem is traversing the whole linked-list and taking elements one by one and finding the element in the remaining linked list whose sum ... Read More

Find pairs in array whose sums already exist in array in C++

sudhir sharma
Updated on 16-Mar-2021 04:57:22

190 Views

In this problem, we are given an array arr[] consisting of N integer. Our task is to find pairs in an array whose sums already exist in the array. We need to find pairs with sum value = a value in the arrayLet’s take an example to understand the problem, Inputarr[] = {1, 2, 4, 6, 7}Output(1, 6), (2, 4)ExplanationFor pairs (1, 6), the sum of values is 7 which is present in the array.For pairs (2, 4), the sum of values is 6 which is present in the array.Solution ApproachA simple solution to the problem is by finding all ... Read More

Find other two sides of a right angle triangle in C++

sudhir sharma
Updated on 16-Mar-2021 04:54:20

215 Views

In this problem, we are given an integer a denoting one side of a right angle triangle. We need to check whether it is possible to have a right angle triangle with side a. If it is possible, then find the other two sides of a right angle triangle.Let’s take an example to understand the problem, Inputa = 5OutputSides : 5, 12, 13ExplanationThe sides of right angle are found as 52 + 122 = 132Solution ApproachA simple solution to the problem is using pythagoras theorem. We know that the sides of a right angled triangle follow pythagoras theorem, which isa2 ... Read More

Advertisements