Found 7347 Articles for C++

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

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

145 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

714 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

210 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

728 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

121 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

509 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

Find whether an array is subset of another array - Added Method 3 in C++

sudhir sharma
Updated on 01-Feb-2022 08:28:58

259 Views

In this problem, we are given two arrays of integers arr1[] and arr2[] of size m and n. Our task is to find whether an array is subset of another array - Added Method 3.Both arrays arr1[] and arr2[] are unorders and have distinct elements.Let's take an example to understand the problem, Input : arr1[] = {5, 2, 1, 6, 8, 10}, arr2[] = {6, 2, 1} Output : arr2 is a subset of arr1.Solution ApproachTo solve this problem, we have discussed multiple methods here. Let's see the working of each of them with a program.Method 1One method to solve ... Read More

Find whether a subarray is in form of a mountain or not in C++

sudhir sharma
Updated on 01-Feb-2022 08:13:26

105 Views

In this problem, we are given an array of integers arr[] and an range. Our task is to find whether a subarray is in the form of a mountain or not.Let's take an example to understand the problem, Input : arr[] = {1, 4, 2, 5, 6, 7, 3, 0}, range = [2, 7] Output : YesExplanation −Subarray of range = {2, 5, 6, 7, 3, 0} The values first increase and then decrease.Solution ApproachA simple solution to the problem is by using extra arrays. We will find the index of the last element which is increasing for each element ... Read More

Find whether a given number is a power of 4 or not in C++

sudhir sharma
Updated on 01-Feb-2022 08:09:16

578 Views

In this problem, we are given an integer N. Our task is to find whether a given integer is a power of 4 or not.Let's take an example to understand the problem, Input : N = 64 Output : YesExplanation −43 = 64 Solution ApproachA simple solution to the problem is by recursively dividing the number by 4 and checking whether the resultant number is divided by 4 or not. If the value after recursive division becomes 1, return true.ExampleProgram to illustrate the working of our solution#include using namespace std; bool isPowerOf4(int n){ if(n == 0) ... Read More

Find whether a given integer is a power of 3 or not in C++

sudhir sharma
Updated on 01-Feb-2022 08:04:05

607 Views

In this problem, we are given an integer N. Our task is to find whether a given integer is a power of 3 or not.Let's take an example to understand the problem, Input : N = 729 Output : YesExplanation −36 = 719 Solution ApproachA solution to the problem is by checking for the value that is power of 3. We will check if the given number N divides 1162261467 (319). If it is a power of 3, the remainder with be 0 i.e. N will divide it. If it does not, the number is not the power of 3.ExampleProgram ... Read More

Advertisements