Found 7197 Articles for C++

Maximize the bitwise OR of an array in C++

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

832 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

772 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

928 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

371 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

455 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

187 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

313 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

Process Synchronization in C/C++

Sunidhi Bansal
Updated on 23-Dec-2019 11:33:09

7K+ Views

Process synchronization is the technique to overcome the problem of concurrent access to shared data which can result in data inconsistency. A cooperating process is the one which can affect or be affected by other process which will lead to inconsistency in processes data therefore Process synchronization is required for consistency of data.The Critical-Section ProblemEvery process has a reserved segment of code which is known as Critical Section. In this section, process can change common variables, update tables, write files, etc. The key point to note about critical section is that when one process is executing in its critical section, ... Read More

Program for power of a complex number in O(log n) in C++

Sunidhi Bansal
Updated on 23-Dec-2019 11:18:15

1K+ Views

Given a complex number in the form of x+yi and an integer n; the task is calculate and print the value of the complex number if we power the complex number by n.What is a complex number?A complex number is number which can be written in the form of a + bi, where a and b are the real numbers and i is the solution of the equation or we can say an imaginary number. So, simply putting it we can say that complex number is a combination of Real number and imaginary number.Raising power of a complex numberTo raise ... Read More

Advertisements