Found 7197 Articles for C++

First and Last Three Bits in C++

sudhir sharma
Updated on 01-Feb-2022 09:51:49

243 Views

In this problem, we are given a number N. Our task is to find the decimal conversion of the fist three and last three bits for the given integer values N.Let's take an example to understand the problem, Input : 57 Output : 71Solution ApproachA simple solution is by changing the number n into its binary equivalent and then saving the bits in an array. After this, we will convert the first three and last three values from the array into numbers individually. The decimal conversion of both the sets of bits is our result.For example, take the number 80.The ... Read More

Finding the power of prime number p in n! in C++

sudhir sharma
Updated on 01-Feb-2022 09:48:43

245 Views

In this problem, we are given a number n and a prime number p. Our task is to find the power of prime number p in n!Let's take an example to understand the problem, Input : n = 6, p = 2 Output : 4Solution ApproachA simple solution to the problem is by simply finding the values of n!. And the factorize it, and find the power of prime number p in the factorization.Here, the number can be represented as the power factorization of 2 in 5! = 30 is 3.The value of n factorial is$$n!\:=\:n^*(n-1)^*(n-2)^*(n-3)\dotso{^*}2^*1$$ $$n!\:=\:3^*2^*1\:=\:6$$Let take n = 6 ... Read More

Finding n-th term of series 3, 13, 42, 108, 235... in C++

sudhir sharma
Updated on 01-Feb-2022 09:45:10

190 Views

In this problem, we are given a number n. Our task is to find the n-th term of series 3, 13, 42, 108, 235...Let's take an example to understand the problem, Input : 5 Output : 235Solution ApproachThe series can be represented as the sum of cubes of first n natural numbers. The formula for that is (n*(n+1)/2)2. Also if we add 2* to it we will get the required series.The formula for sum of the series is (n*(n+1)/2)2+2*n.For n = 5 sum by the formula is(5 * (5 + 1 ) / 2)) ^ 2 + 2*5= (5 * ... Read More

Form the smallest number using at most one swap operation in C++

sudhir sharma
Updated on 01-Feb-2022 10:01:59

427 Views

In this problem, we are given a positive integer. Our task is to create a program to form the smaller number using at most one swap operation.We will be creating a new number using the digits of the existing number. The smallest number formed can have only one digit swapped from the existing number.Let’s take an example to understand the problemInput: n = 63519 Output: 36519Solution ApproachOne method to solve the problem is by finding all the numbers created by swapping pair of digits of the given number. Out of all these swapped digit numbers, the smallest one is returned. ... Read More

Form the largest number using at most one swap operation C++

sudhir sharma
Updated on 01-Feb-2022 09:15:48

261 Views

In this problem, we are given a positive integer. Our task is to create a program to form the largest number using at most one swap operation.We will be creating a new number using the digits of the existing number.The largest number formed can have only one digit swapped from the existing number.Let’s take an example to understand the problemInput: n = 63512 Output: 65312Solution ApproachOne method to solve the problem is by finding all the numbers created by swapping pair of digits of the given number. Out of all these swapped digit numbers, the largest one is returned. For ... Read More

Flood fill Algorithm – how to implement fill() in paint in C++

sudhir sharma
Updated on 01-Feb-2022 09:03:01

841 Views

In this problem, we are given a 2d array representing a 2-D screen, the coordinates of a pixel on the screen to be filled with color and the color. Our task is to create a program to Color the current pixel and all the adjacent pixels which have that color.Coloring in paint, we will select a color and click on the given pixel with a brush.Let’s take an example to understand the problemInput: Sceen[][] = {{W, W, B, W, W, W, W, W}, {W, W, W, W, W, W, B, B}, {W, B, B, W, W, B, W, W}, {W, ... Read More

For every set bit of a number toggle bits of other in C++

sudhir sharma
Updated on 01-Feb-2022 08:50:43

296 Views

In this problem, we are given two integer values. Our task is to create a c program to operform the operation, For every set bit of a number toggle bits of other. Let’s take an example to understand the problemInput: 3 7 Output: 4 Binary of 3: 011 Binary of 3: 111First and second bits of the second number is flipped which makes it 100 i.e 4.Solution ApproachAn approach to solve the problem is by performing the XOR operation of the two numbers. The result will be toggled for the bit where ever the bits of the I’st is 1 using ... Read More

Find XOR of two number without using XOR operator in C++

sudhir sharma
Updated on 01-Feb-2022 08:44:57

946 Views

In this problem, we are given integer values A & B. Our task is to find XOR of two numbers without using the XOR operator.Let's take an example to understand the problem, Input : A = 4, B = 5 Output : 1Solution ApproachOne method to solve the problem is by converting the numbers to their respective binary numbers and then performing bitwise operations based on this table.ABOutput000011101110This will return the result. For this we will use bitwise operations.ExampleProgram to illustrate the working of our solution#include using namespace std; int calcXORwoOperator(int a, int b){    int xorVal = 0; ... Read More

Find x, y, z that satisfy 2/n = 1/x + 1/y + 1/z in C++

sudhir sharma
Updated on 01-Feb-2022 08:40:01

161 Views

In this problem, we are given integer values n. Our task is to find x, y, z that satisfy 2/nx + 1/y + 1/z.Let's take an example to understand the problem, Input : n = 4 Output : 4, 5, 20Solution ApproachA simple solution to the problem is by finding the solution using the value of n.If n = 1, no solution for the equation.If n > 1, the solution to the equation is x = n, y = n+1, z = n(n+1).The solution is $2/n\:=\:1/n\:+1\:(n+1)\:+\:1/(n^*(n\:+\:1))$ExampleProgram to illustrate the working of our solution#include using namespace std; void findSolution(int a, ... Read More

Find x and y satisfying ax + by = n in C++

sudhir sharma
Updated on 01-Feb-2022 08:36:35

610 Views

In this problem, we are given three integer values a, b, and n. Our task is to find x and y satisfying ax + by = n.Let's take an example to understand the problemInput : a = 4, b = 1, n = 5 Output : x = 1, y = 1Solution ApproachA simple solution to the problem is by finding the value between 0 to n that satisfies the equation. We will do this by using altered forms of the equation.x = (n - by)/a y = (n- ax)/bIf we get a value satisfying the equation, we will print ... Read More

Advertisements