Found 7197 Articles for C++

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

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

468 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

210 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

786 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

746 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

Find value of y mod (2 raised to power x) in C++

sudhir sharma
Updated on 01-Feb-2022 08:00:18

203 Views

In this problem, we are given two values x and y. Our task is to find value of y mod (2 raised to power x).Let's take an example to understand the problem, Input : x = 2, y = 19 Output : 3Explanation −y % 2x = 19 % 22 = 19 % 4 = 3 Solution ApproachA simple solution to the problem is by directly calculating the value of 2x using the pow() function and then finding the value of y % 2x.Another approach to solve the problem is by using log. For the value of y < 2x, ... Read More

For Versus While in C++

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

231 Views

Loops in programming are used to compute a block of code multiple times. Here we will be seeing the difference between two types of loops in the program, For Loop and While Loop.For LoopFor Loop is a type of repetition control loop which allows the user to loop over the given block of code upto a specific number of times.Syntaxfor(initisation; condition; update){    …code to be repeated }While LoopWhile loop is a type of entry-controlled loop that allows the user to repeatedly execute the given statement till the given condition is true.Syntaxwhile(condition){    …code to be repeated }Difference between For ... Read More

Find value of k-th bit in binary representation in C++

sudhir sharma
Updated on 01-Feb-2022 07:31:06

406 Views

In this problem, we are given two values n and k. Our task is to find value of k-th bit in binary representation.Let's take an example to understand the problem, Input : n= 5, k = 2 Output : 0Explanation −Binary of 5 = 0101 Second LSB bit is 0.Solution ApproachA solution to the problem is by performing bitwise AND of the binary conversion of the number N with a number with all bits unset and one bit set which is at kth position, to get the result.ExampleProgram to illustrate the working of our solution, #include using namespace std; ... Read More

Find value of (n^1 + n^2 + n^3 + n^4) mod 5 for given n in C++

sudhir sharma
Updated on 01-Feb-2022 07:26:40

226 Views

In this problem, we are given a value n. Our task is to find value of (n^1 + n^2 + n^3 + n^4) mod 5 for given n.Let's take an example to understand the problem, Input : n= 5 Output : 0Explanation −(51 + 52 + 53 + 54) mod 5 = (5 + 25 + 125 + 625) mod 5 = (780) mode 5 = 0Solution ApproachA simple solution to the problem is by directly finding the value of the equation for the given value of N and then calculating its modulus with 5.ExampleProgram to illustrate the working of ... Read More

First element that appears even number of times in an array in C++

sudhir sharma
Updated on 01-Feb-2022 07:22:24

239 Views

In this problem, we are given an array arr[] consisting of N integer values. Our task is to create a program for finding the first element that appears even number of times in an array. If any element exists that satisfies the condition return it otherwise return -1 denoting false.Let's take an example to understand the problem, Input: arr[] = {2, 3, 7, 2, 3, 6, 4, 1, 2} Output: 3Solution ApproachA simple method to solve the problem is by considering each element of the array one by one and then checking the element's occurrence frequency even and returning the ... Read More

Game of Nim with removal of one stone allowed in C++

sudhir sharma
Updated on 01-Feb-2022 08:10:55

240 Views

In this problem called the game of Nim, we are given a positive integer N denoting the pile of stones and there are two players ‘playerA’ and ‘playerB’. Our task is to create a program to predict the winner of the Game of Nim.GAME OF NIM − We have a heap of stones and two players ‘playerA’ and ‘playerB’. Each player can pick one store from the heap if ‘playerA’ starts picking one stone from the heap. We need to predict the winner of the game. The last player to pick the stone from the heap is the winner of ... Read More

Advertisements