Found 7347 Articles for C++

Find the number of elements greater than k in a sorted array using C++

sudhir sharma
Updated on 14-Feb-2022 08:04:13

908 Views

In this problem, we are given an array arr[] consisting of N sorted integer values and an integer k. Our task is to Find the number of elements greater than k in a sorted array. Let’s take an example to understand the problem, Input arr[] = {1, 2, 5, 7, 8, 9} k = 4Output 4Explanation Elements greater than k = 4 are 5, 7, 8, 9Solution ApproachA simple solution to the problem is by using a loop over the array from 0 to N. And then stop at the first element greater than k. Then count the number of values ... Read More

Maximum occurred integer in n ranges using C++

sudhir sharma
Updated on 14-Feb-2022 07:59:13

358 Views

In this problem, we are given N ranges. Our task is to maximum occurred integer in n ranges.For the starting and ending value of all ranges. We need to find the value which occurs the most.Let’s take an example to understand the problem, Input S1 = 1, E1 = 3 S2 = 2, E2 = 6 S3 = 3, E3 = 4Output 2Solution ApproachA simple approach t o solve the problem is by using hashing, we will use a hash table to count all members and their count. We will traverse all ranges and store count in the hash table, ... Read More

Find the Nth term of the series 14, 28, 20, 40,….. using C++

sudhir sharma
Updated on 14-Feb-2022 07:52:06

246 Views

In this problem, we are given an integer value N.Our task is to find the nth term of the series −14, 28, 20, 40, 32, 64, 56, 112….Let’s take an example to understand the problem, InputN = 6Output64Solution ApproachTo find the Nth term of the series we need to find the general term of the series. For which we need to observe the series closely. I can see two different ways to solve the series.Method 1The series is a mixture of two different series at even and odd positions.At odd positions − 14, 20, 32, 56, ….T1 = 14 T3 ... Read More

C++ program to find Nth Non Fibonacci Number

sudhir sharma
Updated on 14-Feb-2022 07:43:34

646 Views

In this problem, we are given an integer value N. Our task is to use a C + + program to find the Nth Non Fibonacci Number.Fibonacci Series generates subsequent number by adding two previous numbers. The Fibonacci series starts from two numbers − F0 & F1. The initial values of F0 & F1 can be taken 0, 1 or 1, 1 respectively.Let’s take an example to understand the problem, Input N = 5Output 10Solution ApproachA simple solution to the problem is to find the Fibonacci numbers and then print the first n numbers which are not present in the ... Read More

Find the row with maximum number of 1s using C++

sudhir sharma
Updated on 14-Feb-2022 07:39:08

219 Views

In this problem, we are given a binary matrix where elements of each row are sorted. Our task is to find the row with maximum number of 1s.Let’s take an example to understand the problem, Inputmat[][] = {{ 0 1 1 1}    {1 1 1 1}    {0 0 0 1}    {0 0 1 1}}Output1ExplanationThe count of 1’s in each row of the matrix : Row 0 : 3 Row 1 : 4 Row 2 : 1 Row 3 : 2Solution ApproachA simple solution to the problem is by finding the row with the smallest index of the ... Read More

Find the repeating and the missing number using two equations in C++

sudhir sharma
Updated on 11-Feb-2022 13:06:01

235 Views

In this problem, we are given an array arr[] of size N. It consists of integer values ranging from 1 to N. And one element x from the range is missing whereas one element y in the array occurs double. Our task is to find the repeating and the missing number using two equations.Let’s take an example to understand the problem, Inputarr[] = {1, 2 , 3, 3}Outputmissing = 4, double = 3Solution ApproachA method to solve the problem is using two equations for the two values x and y. Then solve the equation to get the value for x ... Read More

Find the position of the last removed element from the array using C++

sudhir sharma
Updated on 11-Feb-2022 13:00:07

101 Views

In this problem, we are given an array arr[] of size N and an integer value M. Our task is to find the position of the last removed element from the array.The removal of values from the array is based on the operations −For an element in the array arr[i]. If arr[i] > M, pop the value and push arr[i] - M to the end of the array. Otherwise remove it from the array.Perform the operations till the array consists of elements.Let’s take an example to understand the problem, Inputarr[] = {5, 4, 8}, M = 3Output3ExplanationRemoving values using operations, ... Read More

Find the overlapping sum of two arrays using C++

sudhir sharma
Updated on 11-Feb-2022 12:51:25

193 Views

In this problem, we are given two arrays arr1[] and arr2[] consisting of unique values. Our task is to find the overlapping sum of two arrays.All elements of the arrays are distinct. And we need to return the sum of elements which are common for both arraysLet’s take an example to understand the problem, Inputarr1[] = {5, 4, 9, 2}, arr2[] = {6, 3, 9, 4}Output2ExplanationThe elements that are present in both arrays are 9 and 4. The sum is 9 + 9 + 4 + 4 = 26Solution ApproachA simple solution to the problem is traversing one array say ... Read More

Find the other end point of a line with given one end and mid using C++

sudhir sharma
Updated on 11-Feb-2022 12:42:55

122 Views

In this problem, we are given the coordinates of two points of a line starting point A(xA, yA) and midpoint M(xM, yM) .Our task is to find the other end point of a line with given one end and mid.Let’s take an example to understand the problem, InputA = [1, 2], M = [3, 0]Output[5, -2]ExplanationThe line is −Solution ApproachTo solve the problem, we will be using the concepts of geometry we have learned in mathematics. If you remember there is a midpoint formula for every line which is, mid(x) = (x1 + x2) / 2 mid(y) = (y1 + ... Read More

Find the only repetitive element between 1 to n-1 using C++

sudhir sharma
Updated on 11-Feb-2022 12:00:18

123 Views

In this problem, we are given an unordered array 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 repetitive element between 1 to n-1.Let’s take an example to understand the problem, Inputarr[] = {3, 5, 4, 1, 2, 1}Output1Solution ApproachA simple solution to the problem is traversing the array and for each value find whether the element exists somewhere else in the array. Return the value with double occurrence.Example 1Program to illustrate the working of our solution#include using namespace std; int findRepValArr(int arr[], ... Read More

Advertisements