Found 7347 Articles for C++

Find maximum (or minimum) sum of a subarray of size k in C++

sudhir sharma
Updated on 25-Jan-2021 05:21:14

313 Views

In this problem, we are given an  array arr[] and a number k. Our task is to Find the maximum (or minimum) sum of a subarray of size k. Let’s take an example to understand the problem,  Input: arr[] = {55, 43, 12, 76, 89, 25, 99} , k = 2Output: 165Explanation:The subarray of size 2 has sum = 76 + 89 = 165Solution ApproachA simple approach to solve the problem is by finding all k sized subarrays and then return the sum with maximum value.Another Approach is using the sliding window,  we will find the sum of k sized subarrayes. For this, the ... Read More

Find maximum (or minimum) in Binary Tree in C++

sudhir sharma
Updated on 25-Jan-2021 05:20:42

830 Views

In this problem, we are given a binary tree. Our task is to Find maximum (or minimum) in Binary Tree. Problem Description: We need to find the nodes of the binary tree that have maximum and minimum value in the binary tree.Let’s take an example to understand the problem,  Input: Output: max = 9 , min = 1Solution ApproachWe need to find the max node of the binary tree. We will do this by traversing the pointer until we reach the leaf node and then find the maximum node of the tree.Program to illustrate the working of our solution, ExampleLive Demo#include using namespace ... Read More

Find max of two Rational numbers in C++

sudhir sharma
Updated on 25-Jan-2021 05:12:19

137 Views

In this problem, we are given two Rational Numbers. Our task is to find max of two Rational numbers. Here, the rational numbers are in the form of p/q.Let’s take an example to understand the problem,  Input: rat1 = 5/4, rat2 = 3/2Output: 3/2Explanation: 5/4 = 1.253/2 = 1.5Solution Approach −A simple solution to the problem is by using a method similar to the one we used to perform in school.For this, we will find the L.C.M of the denominator. And then multiply the numerator based on the denominators value. Then for the common denominator, the rational number with maximum numerator value is the ... Read More

Find m-th summation of first n natural numbers in C++

sudhir sharma
Updated on 25-Jan-2021 05:11:42

381 Views

In this problem, we are given two integers m and n. Our task is to Find m-th summation of the first n natural numbers. Problem Description: we will find sum of sum of n natural numbers m times. The sum is given by the formula, if (m > 1),           sum(n, m) = sum( sum(n, (m-1)), 1 )if (m = 1)          sum(n, m) = sum(n, 1) = sum of n natural numbersLet’s take an example to understand the problem,  Input: m = 4, n = 2Output: 231Explanation: sum(2, 4)     = sum ( sum(2, 3), 1 ... Read More

Find m-th smallest value in k sorted arrays in C++

sudhir sharma
Updated on 25-Jan-2021 05:11:04

151 Views

In this problem, we are given k different arrays of different sizes. Our task is to find m-th smallest value in k sorted arrays. Problem Description: Here, we need to find m-th smallest element of the merged array of all k arrays.Let’s take an example to understand the problem,  Input:         m = 4                   arr[][] = { {4 , 7},                                     {2, 5, 6},                       ... Read More

Find M-th number whose repeated sum of digits of a number is N in C++

sudhir sharma
Updated on 25-Jan-2021 05:13:26

108 Views

In this problem, we are given two positive numbers N and M. Our task is to find the M-th number whose repeated sum of digits of a number is N. Problem description: Here, we need to find the Mth number whose sum of digits till the sum becomes single digit is equal to N.Let’s take an example to understand the problem,  Input: N = 4 M = 6Output: 49Solution ApproachA simple solution of the problem, is by finding all numbers and count the number whose sum of digits is N, and return m-th number.Another solution to the problem is using formula to find M-th ... Read More

Find longest length number in a string in C++

sudhir sharma
Updated on 25-Jan-2021 05:13:50

323 Views

In this problem, we are given a string str consisting of character and alphabets only. Our task is to find the longest length number in a string. Problem Description: we need to find the length of the number i.e. consecutive numerical characters in the string.Let’s take an example to understand the problem,  Input: str = “code001tutorials34124point”Output: 34124Explanation:  Numbers in the string are001 - size 334124 - size 5Solution ApproachA simple solution to the problem is by traversing the sting and finding the number’s length and its starting index. We will store the starting position and count of characters in the string for each number ... Read More

Find letter's position in Alphabet using Bit operation in C++

sudhir sharma
Updated on 25-Jan-2021 05:12:08

647 Views

In this problem, we are given a string str consisting of english alphabets. Our task is to find the letter's position in the Alphabet using the Bit operation. Problem Description: Here, we will return the position of each character of the string as it is in english alphabets.The characters of the string are case-insensitive i.e. “t” and “T” are treated the same.Let’s take an example to understand the problem,  Input: str = “Tutorialspoint”Output: 20 21 20 15 18 9 1 12 19 16 15 9 14 20 Solution ApproachA simple solution to find a character's position is by finding its logical AND operation with 31.Program ... Read More

Find length of the largest region in Boolean Matrix in C++

sudhir sharma
Updated on 25-Jan-2021 05:05:38

549 Views

In this problem, we are given a 2-D matrix of size nXm consisting of 0’s and 1’s only. Our task is to find the length of the largest region in the Boolean Matrix. Problem Description: If a cell contains 1, it is a filled Cell. We need to find the length of connected cells which are connected adjacent to each other horizontally or vertically or diagonally. Let’s take an example to understand the problem,  Input: matrix[4][5]{ {0, 1, 1, 0, 1},    {0, 0, 1, 1, 1},    {1, 0, 0, 0, 0},    {1, 0, 1, 0, 1} }Output: 6Explanation:  The number of connected filled ... Read More

Find length of loop in linked list in C++

sudhir sharma
Updated on 25-Jan-2021 05:04:42

382 Views

In this problem, we are given a linked list that might contain loops. Our task is to find the length of the loop in the linked list. Problem Description: we need to count the number of nodes in the loop if it contains a loop otherwise return -1.Let’s take an example to understand the problem,  Input: linked-list :Output: 8Solution ApproachTo solve the problem, we first need to check whether the linked list contains a loop. An approach to check this is using Floyd’s Cycle Finding Algorithm. In Floyd’s Cycle Finding Algorithm,  we will traverse the linked list using two pointers. One slowPointer that increases by 1 ... Read More

Advertisements