Found 7347 Articles for C++

Maximum subarray sum modulo m in C++

Narendra Kumar
Updated on 03-Jun-2020 08:06:42

312 Views

In this problem, we are given an array of size n and an integer m. our task is to create a program that will find the maximum subarray sum modulo m in C++.Program description − Here, we will find the maximum value obtained by dividing the sum of all elements of subarray divided by m.Let’s take an example to understand the problem, Input − array = {4, 9 ,2} m = 6Output − 5Explanation − All subarrays and their remainders on dividing{4}: 4%6 = 4 {9}: 9%6 = 3 {2}: 2%6 = 2 {4, 9}: 13%6 = 1 {9, 2}: ... Read More

Maximum subarray sum in O(n) using prefix sum in C++

Narendra Kumar
Updated on 30-Jan-2020 12:05:28

562 Views

Problem statementGiven an Array of Positive and Negative Integers, find out the Maximum Subarray Sum in that ArrayExampleIf input array is − {-12, -5, 4, -1, -7, 1, 8, -3} then output is 9AlgorithmCalculate the prefix sum of the input array.Initialize− min_prefix_sum = 0, res = -infiniteMaintain a loop for i = 0 to n. (n is the size of the input array).cand = prefix_sum[i] – miniIf cand is greater than res (maximum subarray sum so far), then update res by cand.If prefix_sum[i] is less than min_prefix_sum (minimum prefix sum so far), then update min_prefix_sum by prefix_sum[i].Return resExample Live Demo#include ... Read More

Maximum subarray sum in array formed by repeating the given array k times in C++

Narendra Kumar
Updated on 03-Jun-2020 08:08:33

155 Views

In this problem, we are given an array and a number k. Our task is to create a program that will find the maximum subarray sum in an array formed by repeating the given array k time in c++.Problem description − Here, we will find the maximum sum of the subarray formed from array formed by repeating the given array k times.Let’s take an example to understand the problem, Input − array = {3, 5, 1} k = 2Output − 18Explanation −array formed by repeating k times, array = {3, 5, 1, 3, 5, 1} Maximum subarray sum = 3+5+1+3+5+1 ... Read More

Maximum subarray sum by flipping signs of at most K array elements in C++

Narendra Kumar
Updated on 03-Jun-2020 08:11:19

325 Views

In this problem, we are given an array and an integer k. Our task is to create a program that will find the maximum subarray sum by flipping signs of at most k array elements in C++.Code description − Here, we will have to find at most k elements to flip in the array which will make the sum of subarray created from this array maximum.Let’s take an example to understand the problem, Input − array = {1, -2, 7, 0} k = 2Output − 10Explanation − we will flip only one element which is -2, it makes the array ... Read More

Maximum Subarray Sum after inverting at most two elements in C++

Narendra Kumar
Updated on 03-Jun-2020 08:13:04

159 Views

In this problem, we are given an array. Our task is to create a program that will find the maximum subarray sum after inverting at most two elements in C++.Problem description − Here, we can have to find the subarray that will produce the maximum sum on inverting the sign of any two numbers of the array.Let’s take an example to understand the problem, Input − array = {-5, 1, 3, 8, -2, 4, 7}Output − 30Explanation − we will consider elements from index 0 to 6 and invert values -5 and -2 to get the array with max sum.To ... Read More

Maximum steps to transform 0 to X with bitwise AND in C++

Narendra Kumar
Updated on 03-Jun-2020 08:14:40

83 Views

In this problem, we are given an integer X. Our task is to find the total number of steps that are taken to transform from 0 to X.Valid transformation − One step is counted when one transformation takes place from A to B. The condition for the transform to take place is A != B and A & B = A (& is bitwise AND). So, 1 step is transforming from A to B and we have to create a program that will count the maximum number of steps to transform 0 to X.Let’s take an example to understand the ... Read More

Divide Two Integers in C++

Arnab Chakraborty
Updated on 27-Apr-2020 12:51:59

1K+ Views

Suppose we have two integers dividend and divisor. We have to divide two integers without using multiplication, division, and mod operator. Return the quotient after dividing the dividend by divisor. The integer division should truncate toward zero. Both of the inputs are integersSo if the given inputs are dividend = 7, divisor = -3, then output will be -2.To solve this, we will follow these steps −Taking two arguments x and y it indicates x divides yif x < -Infinity and y = 1, then return infinitya := |x|, b := |y| and ans := 0while a – b >= ... Read More

Swap Nodes in Pairs in C++

Arnab Chakraborty
Updated on 27-Apr-2020 12:39:29

718 Views

Consider we have a linked list. We have to swap every two adjacent nodes and return its head. The constraint is that we cannot modify the value of the nodes, only the node itself can be changed. So if the list is like [1, 2, 3, 4], then the resultant list will be [2, 1, 4, 3]To solve this, we will follow these steps −if head is not present, then return headfirst := head, second := next of head, dummy is one new node with value -1next of dummy := first, and prev := dummywhile second is not nulltemp := ... Read More

4Sum in C++

Arnab Chakraborty
Updated on 27-Apr-2020 12:23:13

456 Views

Suppose we have an array of numbers. It stores n integers, there are four elements a, b, c and d in the array. We have another target value, such that a + b + c + d = target. Find all unique quadruplets in the array which satisfies the situation. So if the array is like [-1, 0, 1, 2, 0, -2] and target is 0, then the result will be [[-1, 0, 0, 1], [-2, -1, 1, 2], [-2, 0, 0, 2]]To solve this, we will follow these steps −The actual sum is being done using a function called ... Read More

Iswctype() function in C++ STL

Sunidhi Bansal
Updated on 30-Jan-2020 09:50:37

38 Views

In C++ standard template libraray(STL), iswctype() function is used to check if the given wide character has property specified by desc.Iswctype() is an in built function whose header file is “ctype.h”.Syntax of Iswctype() is as followsint iswctype(wint_t c, wctype_t desc); iswctype () / Checks whether whether c has the property specified by desc. /Synopsisint iswctype(wint_t c, wctype_t desc);ParametersC − To check the wide characters which are casted to the integral type wint_tDesc − It is a value which is returned by the call to wctype, which is a scalar type which is used as return type for wctype(Wide character type).Return ... Read More

Advertisements