Found 7347 Articles for C++

Find the only repeating element in a sorted array of size n using C++

sudhir sharma
Updated on 11-Feb-2022 11:50:52

176 Views

In this problem, we are given an arr[] of size N containing values from 1 to N-1 with one value occuring twice in the array. Our task is to find the only repeating element in a sorted array of size n.Let’s take an example to understand the problem, Inputarr[] = {1, 2, 3, 4, 5, 5, 6, 7}Output5Solution ApproachA simple approach to solve the problem is by using linear search and checking if arr[i] and arr[i+1] have the same value. In this case, return arr[i] which is the value repeated.Example 1Program to illustrate the working of our solution#include using ... Read More

Find the only missing number in a sorted array using C++

sudhir sharma
Updated on 11-Feb-2022 11:45:42

1K+ Views

In this problem, we are given an arr[] of size N containing values from 1 to N with one value missing in the array. Our task is to find the only missing number in a sorted array.Let’s take an example to understand the problem, Inputarr[] = {1, 2, 3, 5, 6, 7}Output4Solution ApproachA simple solution to the problem is by traversing the sorted array linearly. And then check for the missing value using the fact that arr[i] = (i + 1).Example 1Program to illustrate the working of our solution#include using namespace std; int findMissingValArray(int arr[], int N){    for(int ... Read More

Find the only element that appears b times using C++

sudhir sharma
Updated on 11-Feb-2022 11:41:26

96 Views

In this problem, we are given an arr[] of size n and two integers a and b. Our task is to find the only element that appears b times.All values of the array occur a time except one value which occurs b times in the array and we need to find that value.Let’s take an example to understand the problem, Inputarr[] = {3, 3, 3, 3, 5, 5, 5, 1, 1, 1, 1} a = 4, b = 3Output5Solution ApproachA simple solution to the problem is by counting the occurrence of each element and then storing it in a 2D ... Read More

Find the only different element in an array using C++

sudhir sharma
Updated on 11-Feb-2022 11:36:37

159 Views

In this problem, we are given an arr[] of size n. Our task is to find the only different element in an array.There are only two different types of elements in the array. All the elements are the same except one.Let’s take an example to understand the problem, Inputarr[] = {1, 1, 1, 2, 1, 1, 1, 1}Output2Solution ApproachA simple approach to solve the problem, we need to traverse the array and find elements which are different from other elements of the array. This approach needs a time complexity of O(N2).Another approach to solve the problem in O(N) is by ... Read More

Find the one missing number in range using C++

sudhir sharma
Updated on 11-Feb-2022 11:31:24

378 Views

In this problem, we are given an arr[] of size n. Our task is to find the one missing number in range.The array consists of all values ranging from smallest value to (smallest + n). One element of the range is missing from the array. And we need to find this missing value.Let’s take an example to understand the problem, Inputarr[] = {4, 8, 5, 7}Output6Solution ApproachA simple solution to the problem is by searching the missing element by sorting the array and then finding the first element of range starting from minimum value which is not present in the ... Read More

Maximum occurring character in an input string using C++

sudhir sharma
Updated on 11-Feb-2022 11:27:16

5K+ Views

In this problem, we are given an input string of lowercase characters. Our task is to maximum occurring character in an input string.In case of multiple values with the same frequency of occurrence, we need to print lexicographically smaller values.Let’s take an example to understand the problem, Inputstring = “programming”OutputgSolution ApproachTo find the solution to the problem, we need to sort the read string and then traverse the string so that we could find the character which has maximum occurrence in the string. We would use hashing method (hash table method) to conquer this problem. Traversing and hashing the each ... Read More

Find the number of zeroes using C++

sudhir sharma
Updated on 11-Feb-2022 11:22:03

657 Views

In this problem, we are given a binary array bin[] consisting of only 0’s and 1’s. Our task is to find the number of zeroes.The array is sorted i.e. all 0’s are arranged together after 1’s.Let’s take an example to understand the problem, Inputarr[] = {1, 1, 1, 0, 0, 0, 0}Output4Solution ApproachA simple solution to the problem is using the fact that the array is sorted, that is the number of 0’s in the array can be found using the first occurrence of 0 in the array. As after the first occurrence all values will be zero.For finding the ... Read More

Find the number of stair steps using C++

sudhir sharma
Updated on 11-Feb-2022 11:13:14

205 Views

In this problem, we are given a number N denoting the number of bricks provided to create the stair. Our task is to find the number of stair steps.Using the given bricks, we need to create a stair step. Each step takes one more brick that the last step. And the first step is two bricks high. We need to find the number of such steps that can be made from bricks.Let’s take an example to understand the problem, InputN = 40Output3ExplanationStep = 1 ; bricks required = 2; total bricks used = 2 ; bricks remaining = 38Step = ... Read More

Find the number of solutions to the given equation in C++

sudhir sharma
Updated on 11-Feb-2022 11:20:12

342 Views

In this problem, we are given three integer values A, B, C. Our task is to find the number of solutions to the given equation.EquationX = B*Sm(X)^A + Cwhere Sm(X) is the sum of digits of X.We need to count all the values of X such that it satisfies the above equation where X can be any number between 1 to 109.Let’s take an example to understand the problem, InputA = 3, B = 6, C = 4Output3Solution ApproachA solution to solve the problem is by counting the number of values of X. For this, the sum of digits plays ... Read More

Find the number of Islands Using Disjoint Set in C++

sudhir sharma
Updated on 11-Feb-2022 10:58:15

401 Views

In this problem, we are given a 2D binary matrix. Our task is to find the number of islands Using DFS.Island is a ground of 1 or more connected 1’s in the matrix.Let’s take an example to understand the problem, Inputbin[][] = {{ 1 0 0 0}    {0 1 0 1}    {0 0 0 0}    {0 0 1 0}}Output3 ExplanationIslands are :    bin00 - bin11    bin13    bin32Solution ApproachTo find the island from a binary matrix using a disjoint set data structure. To find island count, we will traverse the matrix and do union of ... Read More

Advertisements