Found 26504 Articles for Server Side Programming

Maximize the bitwise OR of an array in C++

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

833 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

773 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

372 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

C Program for Radix Sort

sudhir sharma
Updated on 24-Dec-2019 06:33:31

14K+ Views

A sorting algorithm is an algorithm that puts components of a listing in a certain order. The most-used orders are numerical order and lexicographic order.The Radix sort is a non-comparative sorting algorithm. The Radix sort algorithm is the most preferred algorithm for the unsorted list.It sorts the elements by initially grouping the individual digits of the same place value. The idea of Radix Sort is to do digit by digit sort starting from least significant digit(LSD) to the most significant digit(MSD), according to their increasing/decreasing order. Radix sort is a small method that is used several times when alphabetizing an ... Read More

C Program for Rabin-Karp Algorithm for Pattern Searching

sudhir sharma
Updated on 24-Dec-2019 06:26:19

3K+ Views

Pattern matching in C − We have to find if a string is present in another string, as an example, the string "algorithm” is present within the string "naive algorithm". If it is found, then its location (i.e. position it is present at) is displayed. We tend to create a function that receives 2character arrays and returns the position if matching happens otherwise returns -1.Input: txt = "HERE IS A NICE CAP"    pattern = "NICE" Output: Pattern found at index 10 Input: txt = "XYZXACAADXYZXYZX"    pattern = "XYZX" Output: Pattern found at index 0    Pattern found at index ... Read More

C Program for Naive algorithm for Pattern Searching

sudhir sharma
Updated on 24-Dec-2019 06:18:02

8K+ Views

Pattern matching in C− We have to find if a string is present in another string, as an example, the string "algorithm” is present within the string "naive algorithm". If it is found, then its location (i.e. position it is present at) is displayed. We tend to create a function that receives 2 character arrays and returns the position if matching happens otherwise returns -1.Input: txt = "HERE IS A NICE CAP"    pattern = "NICE" Output: Pattern found at index 10 Input: txt = "XYZXACAADXYZXYZX"    pattern = "XYZX" Output: Pattern found at index 0    Pattern found at ... Read More

Advertisements