Found 7347 Articles for C++

Maximize the median of an array in C++

Narendra Kumar
Updated on 24-Dec-2019 07:05:41

173 Views

Problem statementGiven an array arr[] of N elements and an integer K where K < N. The task is to insert K integer elements to the same array such that the median of the resultant array is maximizedIf input array is {1, 3, 2, 5} and k = 3 then−Sorted array becomes {1, 2, 3, 5}Insert 3 elements that are greater than 5. After this operation array becomes {1, 2, 3, 5, 6, 6, 6}Median of the new array is 5Algorithm1. In order to maximize the median of the resultant array, all the elements that need to be inserted must ... Read More

Maximize the maximum subarray sum after removing at most one element in C++

Narendra Kumar
Updated on 24-Dec-2019 07:03:04

205 Views

Problem statementGiven an array arr[] of N integers. The task is to first find the maximum sub-array sum and then remove at most one element from the sub-array. Remove at most a single element such that the maximum sum after removal is maximized.If given input array is {1, 2, 3, -2, 3} then maximum sub-array sum is {2, 3, -2, 3}. Then we can remove -2. After removing the remaining array becomes−{1, 2, 3, 3} with sum 9 which is maximum.Algorithm1. Use Kadane’s algorithm to find the maximum subarray sum. 2. Once the sum has beens find, re-apply Kadane’s algorithm ... Read More

Maximize the bitwise OR of an array in C++

Narendra Kumar
Updated on 24-Dec-2019 06:59:33

681 Views

Problem statementGiven an array of N integers. The bitwise OR of all the elements of the array has to be maximized by performing one task. The task is to multiply any element of the array at-most k times with a given integer xIf input array is {4, 3, 6, 1}, k = 2 and x = 3 then maximum value can be obtained is 55Algorithm1. multiply an array element with (x^k) and do bitwise OR it with the bitwise OR of all previous elements 2. Multiply an array element with bitwise OR of all next elements 3. Return the maximum ... Read More

Maximize number of 0s by flipping a subarray in C++

Narendra Kumar
Updated on 24-Dec-2019 06:57:49

656 Views

Problem statementGiven a binary array, find the maximum number of zeros in an array with one flip of a subarray allowed. A flip operation switches all 0s to 1s and 1s to 0sIf arr1= {1, 1, 0, 0, 0, 0, 0}If we flip first 2 1’s to 0’s, then we can get subarray of size 7 as follows −{0, 0, 0, 0, 0, 0, 0}Algorithm1. Consider all subarrays and find a subarray with maximum value of (count of 1s) – (count of 0s) 2. Considers this value be maxDiff. Finally return count of zeros in original array + maxDiff.Example Live Demo#include ... Read More

Maximize elements using another array in C++

Narendra Kumar
Updated on 24-Dec-2019 06:55:18

836 Views

Problem statementGiven two arrays with size n, maximize the first array by using the elements from the second array such that the new array formed contains n greatest but unique elements of both the arrays giving the second array priority i.e. all elements of the second array appear before the first array. The order of appearance of elements should be kept the same in output as in inputIf arr1[] = {12, 15, 10} and arr2[] = {16, 17, 5} then {16, 17, 15} are the maximum elements from both array by maintaining order.Algorithm1. Create temporary array of size 2 * ... Read More

Maximize array sum after K negation in C++

Narendra Kumar
Updated on 24-Dec-2019 06:51:55

288 Views

Problem statementGiven an array of size n and a number k. We have to modify an array k number of times.Modify array means in each operation we can replace any array element arr[i] by negating it i.e. arr[i] = -arr[i]. The task is to perform this operation in such a way that after k operations, the sum of an array must be maximum.If input arr[] = {7, -3, 5, 4, -1} then maximum sum will be 20First negate -3. Now array becomes {7, 3, 5, 4, -1}Negate -1. Now array becomes {7, 3, 5, 4, 1}Algorithm1. Replace the minimum element ... Read More

Maximize array elements up to given numbers in C++

Narendra Kumar
Updated on 24-Dec-2019 06:47:07

368 Views

Problem statementGiven an array of integers, a number and a maximum value, the task is to compute the maximum value that can be obtained from the array elements. Every value on the array traversing from the beginning can be either added to or subtracted from the result obtained from the previous index such that at any point the result is not less than 0 and not greater than the given maximum value. For index 0 take the previous result equal to the given number. In case of no possible answer print -1.If arr[] = {3, 10, 6, 4, 5}, number ... Read More

Maximize a given unsigned number by swapping bits at its extreme positions in C++

Narendra Kumar
Updated on 24-Dec-2019 06:43:22

121 Views

Problem statementGiven a number maximize it by swapping bits at its extreme positions i.e. at first and last position, second and second last position and so on.If the input number is 8 then its binary representation is−00000000 00000000 00000000 00001000After swapping bits at extreme positions number becomes −00010000 00000000 00000000 00000000 and its decimal equivalent is: 268435456Algorithm1. Create a copy of the original number 2. If less significant bit is 1 and more significant bit is 0 then swap the bits in the bit from only, continue the process until less significant bit’s position is less than more significant bit’s ... Read More

Maximal Disjoint Intervals in C++

Narendra Kumar
Updated on 24-Dec-2019 06:39:21

201 Views

DescriptionGiven a set of N intervals, the task is to find the maximal set of mutually disjoint intervals. Two intervals [i, j] & [k, l] are said to be disjoint if they do not have any point in commonIf intervals are {{10, 20} {23, 35}, {15, 21}, {37, 41}} then maximum non-overlapping disjoint pairs are −{10, 20} {23, 35} {37, 41}Note that we cannot include {15, 21} as it will overlap with {10, 20}Algorithm1. Sort the intervals, with respect to their end points. 2. Traverse the all intervals, if we get two overlapping intervals, then choose the interval with lower ... Read More

Priority queue of pairs in C++ (Ordered by first)

Sunidhi Bansal
Updated on 23-Dec-2019 11:40:06

3K+ Views

Priority queue is an abstract data type for storing a collection of prioritized elements that supports insertion and deletion of an element based upon their priorities, that is, the element with first priority can be removed at any time. The priority queue doesn’t stores elements in linear fashion with respect to their locations like in Stacks, Queues, List, etc. The priority queue ADT(abstract data type) stores elements based upon their priorities.Priority Queue supports the following functions −Size() − it is used to calculate the size of the priority queue as it returns the number of elements in it.Empty() − it return ... Read More

Advertisements