Maximum Value K for Array Elements in C++

Ayush Gupta
Updated on 09-Oct-2020 09:48:49

315 Views

In this problem, we are given an array arr. Our task is to create a program to find the Maximum value K such that the array has at-least K elements that are >= K in C++.Problem DescriptionWe need to find a value K, that fulfills the condition that there are K or more elements in the array that are greater than or equal to K.Let’s take an example to understand the problem, Input:arr[] = {3, 5, 1, 7, 6, 6, 4, 8}Output:5ExplanationElements in the array that are greater than or equal to 5: 5, 6, 6, 7, 8.Solution ApproachA simple ... Read More

Maximum Value of an Integer for Factorial Calculation in C++

Ayush Gupta
Updated on 09-Oct-2020 09:47:58

220 Views

In this problem, we need to create a program to find Maximum value of an integer for which factorial can be calculated on a machine in C++.Factorial of a number is a huge value, as it is the product of all values preceding it. And C++ can handle large values only upto a certain value by using its inbuilt function. We need to find this restriction.Solution ApproachWe will simply use the property of data types which is when the numbers exceed the maximum value a negative number is returned.We will use long long int which is the largest basic data ... Read More

Maximum Weight Difference in C++

Ayush Gupta
Updated on 09-Oct-2020 09:46:05

239 Views

In this problem, we are given an array arr[] and a number M. Our task is to create a program to calculate the Maximum Weight Difference in C++.Problem StatementWe will find M elements from the array such that the absolute difference between the sum and the sum of rest elements is maximum.Let’s take an example to understand the problem, Input: arr[] = {3, 1, 6, 9, 4} M = 3Ouput:15ExplanationWe will consider 4, 6, 9. The sum is 19. The absolute difference with the sum of rest numbers is|19 - 4| = 15Solution ApproachThe solution to the problem is based ... Read More

Queries for Characters in a Repeated String in C++

Ayush Gupta
Updated on 09-Oct-2020 09:43:56

191 Views

In this problem, we are given a string str and Q queries consisting of two values a and b. Our task is to create a program to solve Queries for characters in a repeated string in C++.Problem DescriptionTo solve each query, we need to check whether the characters at index a and b are the same and return the value accordingly.Let’s take an example to understand the problem, Input: str = “tutorialspoint”Q = 2Query = {{0, 2}, {4, 7}}Output:RepeatedNot RepeatedExplanationFor Query 1, the character at index 0 is t, and the character at index 2 is t. Both are the ... Read More

Maximum Value with Choice of Dividing or Considering in C++ Program

Ayush Gupta
Updated on 09-Oct-2020 09:34:55

228 Views

In this problem, we are given a number N. Our task is to create a program to find to Maximum value with the choice of either dividing or considering as it is in C++.Problem DescriptionTo find the maximum, we can consider any two values, either by take the value as it is or we could get the maximum value by dividing.The value could be extracted as F(N/2) + F(N/3) + F(N/4) + F(N/5).Let’s take an example to understand the problem, Input:N = 8Output:9ExplanationF(8) =F(8/2) + F(8/3) + F(8/4) + F(8/5) = F(4) + F(2) + F(2) + F(1) = 4 ... Read More

Maximums from Array When the Maximum Decrements After Every Access in C++

Ayush Gupta
Updated on 09-Oct-2020 09:33:33

94 Views

In this problem, we are given an array arr[]and an integer M. Our task is to create a program to find Maximums from array when the maximum decrements after every access in C++.Problem DescriptionTo find the maximum, we will find the maximum element from the array and after every retrieval and decrease it by -1, M times.Let’s take an example to understand the problem, Input: arr[] = {3, 6, 8, 9} M = 2Ouput:17Explanation1st iteration, maximum = 9, sum = 9, updated arr = {3, 6, 8, 8}2nd iteration, maximum = 8, sum = 9+8 = 17, updated arr = ... Read More

Queries for Counts of Multiples in an Array in C++

Ayush Gupta
Updated on 09-Oct-2020 09:30:02

376 Views

In this problem, we are given an arr[] and Q queries each consisting of a value m. Our task is to create a program to solve the Queries for counts of multiples in an array in C++.Problem DescriptionFor solving the queries, we need to count all the numbers that are multiples of m. For this we will check for elements divisible by m.Let’s take an example to understand the problem, Input:arr[] = {4, 7, 3, 8, 12, 15}Q = 3 query[] = {2, 3, 5}Ouput:3 3 1ExplanationQuery 1: m = 2, multiples in the array = 4, 8, 12. Count ... Read More

Count Array Elements within Given Range in C++

Ayush Gupta
Updated on 09-Oct-2020 09:21:35

662 Views

In this problem, we are given an array arr[] and Q queries, each can be one of the two types, {1, L, R}− For the count of array elements in the range [L, R].{2, index, val}− For updating the element at index with val.Our task is to create a program to solve the Queries for counts of array elements with values in given range in C++.Let’s take an example to understand the problem, Input:arr[] = {1, 5, 2, 4, 2, 2, 3, 1, 3}Q = 3Query = { {1, 4, 8}, {2, 6, 5}, {1, 1, 4}}Ouput:3 7ExplanationQuery 1: count ... Read More

Find Distance Between Two Nodes of a Binary Tree in C++

Ayush Gupta
Updated on 09-Oct-2020 09:14:45

294 Views

In this problem, we are given a binary tree and two nodes. Our task is to create a program to Find distance between two nodes of a Binary Tree.Problem DescriptionWe need to find the distance between two nodes which is the minimum number of edges that will be traversed when we go from one node to another node.Let’s take an example to understand the problem, Input: binary treeNode1 = 3 ,Node2 = 5Output: 3ExplanationThe path from node 3 to node 5 is 3 -> 1 -> 2 -> 5. There are 3 edges traversed that make distance 3.Solution ApproachA simple ... Read More

X-th Smallest Substring Lexicographically in C++

Ayush Gupta
Updated on 09-Oct-2020 09:13:05

184 Views

In this problem, we are given a string str and Q queries. Each Query has a number X. Our task is to create a program to solve the Queries to answer the X-th smallest sub-string lexicographically in C++.Problem DescriptionWe need to find the Xth lexicographically smallest substring for each query i.e. based on alphabetical order sorting we will have to find Xth substring.Let’s take an example to understand the problem, Input: str = “point”Q = 4 query = {4, 7, 2, 13}Output:n, oi, in, poinExplanationAll substrings of str in lexicographical order are−i, in, int, n, nt, o, oi, oin, oint, ... Read More

Advertisements