Found 26504 Articles for Server Side Programming

Split Array Largest Sum in C++

Arnab Chakraborty
Updated on 01-Jun-2020 10:49:28

395 Views

Suppose we have an array of positive integers and one value m. We can divide this array into m number of contiguous subarrays. We have to devise an algorithm to minimize the largest sum among these m subarrays.So if the array is say [7, 2, 4, 10, 9], and m = 2, then the sum will be 19, as we can make two subarrays like [7, 2, 4] and [10, 9], then the subarray with largest sum is 19.To solve this, we will follow these steps −Define a function splitArray(), this will take an array v, m, n := size ... Read More

Frog Jump in C++

Arnab Chakraborty
Updated on 01-Jun-2020 10:47:29

2K+ Views

Suppose there is a frog that is crossing a river. The river is divided into x units and at each unit there may be a stone. The frog can jump on a stone, but not water. Here we have a list of stones' positions in sorted ascending order sequence, we have to check whether the frog is able to cross the river by landing on the last stone, or not. Initially, the frog is on the first stone and assume the first jump must be of 1 unit.When the frog's current jump was k units, then its next jump must ... Read More

Insert Delete GetRandom O(1) - Duplicates allowed in C++

Arnab Chakraborty
Updated on 01-Jun-2020 10:46:13

150 Views

Suppose, we want to make a data structure, that supports some operations, these operations must be preformed in O(1) amount of time. So let these operations are like −insert(x): insert x into the collectionremove(x): delete x from the collectiongetRandom(): This will find random element form that collection.To solve this, we will follow these steps −Make an array numsmake one map mDefine a function insert(), this will take val, ret := when val is not in minsert size of nums at the end of m[val]insert { val, size of m[val] – 1} pair at the end of numsreturn retDefine a function ... Read More

Max Sum of Rectangle No Larger Than K in C++

Arnab Chakraborty
Updated on 01-Jun-2020 10:43:20

137 Views

Suppose we have a 2D matrix, and an integer k. We have to find the max sum of a rectangle in the matrix, such that its sum is not greater than k. So, if the input is like −1010-32And k = 3, then the output will be 3, as the sum of marked rectangle is 3.To solve this, we will follow these steps −Define a function maxSumSubmatrix(), this will take one 2D array matrix and k, n := row number, m := column numberans := -inffor initialize l := 0, when l < m, update (increase l by 1), do ... Read More

Russian Doll Envelopes in C++

Arnab Chakraborty
Updated on 01-Jun-2020 10:40:55

526 Views

Suppose we have some envelops, these envelops has height and width values as pairs. We can put one envelop into another one if the height and width of second envelop both are smaller than the height and width of the first one. So, what will be the maximum number of envelops that we can put inside other. So, if the inputs are like [[5, 5], [6, 4], [6, 8], [2, 3]], then the output will be 3, as smallest envelop is [2, 3], then [5, 5], then [6, 8].To solve this, we will follow these steps −sort the array v ... Read More

Count of Range Sum in C++

Arnab Chakraborty
Updated on 01-Jun-2020 10:38:21

613 Views

Suppose we have an integer array nums, we have to find the number of range sums that lie in range [lower, upper] both inclusive. The range sum S(i, j) is defined as the sum of the elements in nums from index i to index j where i ≤ j.So if the input is like [-3, 6, -1], lower = -2 and upper = 2, then the result will be 2, as the ranges are [0, 2], the sum is 2, [2, 2], sum is -2.To solve this, we will follow these steps −Define a function mergeIt(), this will take array ... Read More

Create Maximum Number in C++

Arnab Chakraborty
Updated on 01-Jun-2020 10:35:46

235 Views

Suppose we have two arrays of length m and n with digits 0-9 representing two numbers. We have to create the maximum number of length k that is less than m + n from digits of the two. We have to keep in mind that the relative order of the digits from the same array must be preserved. We have to find the array of the k digits. So if the inputs are like [3, 4, 7, 5] and [9, 1, 3, 5, 8, 4], and k = 5, then the answer will be [9, 8, 7, 5, 4].To solve ... Read More

Program to find the angles of a quadrilateral in C++

Ayush Gupta
Updated on 16-Sep-2020 17:31:28

263 Views

In this problem, we are given a value d, which is the common difference of AP. This AP is all the angles of a quadrilateral. Our task is to create a program to find the angles of a quadrilateral in C++.Problem Description − Here, the angles of the quadrilateral are in the form of an AP with common difference d. And we need to find the angles.Let’s take an example to understand the problemInputd = 15Output67.5, 82.5, 97.5, 112.5ExplanationFirst angle is x Second angle is x + 15 Third angle is x + 30 Four angle is x + 45Sum ... Read More

Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + .. + n in C++

Revathi Satya
Updated on 22-May-2024 11:51:40

729 Views

In this article, we are given a number n that denotes the nth term of the series. Our task is to create a Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + .... + n in C++. This series is different from the other because it include terms that are repeated according to their values. The series 1 + 2 + 2 + 3 + 3 + 3 + … + n consists of repeated terms that are repeated infinitely only by their value. Such illustration 1 comes once, ’2 twice’, ... Read More

Program to find sum of series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n in C++

Ayush Gupta
Updated on 16-Sep-2020 17:39:13

4K+ Views

In this problem, we are given a number n. Our task is to create a program to find sum of series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n in C++.Code description − Here, we will find the sum of the series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n till nth term. The series is a harmonic progression series.Harmonic progression is a series whose inverse will be an arithmetic progression. I.e. if for a harmonic progression A1, A2, A3... An, there is an arithmetic progression 1/A1, 1/A2, 1/A3.First, let’s take an example to ... Read More

Advertisements