Server Side Programming Articles - Page 1910 of 2650

Maximum subset with bitwise OR equal to k in C++

Narendra Kumar
Updated on 27-Feb-2020 05:39:05

488 Views

Problem statementGiven an array of non-negative integers and an integer k, find the subset of maximum length with bitwise OR equal to k.ExampleIf given input array is = [1, 4, 2] and k = 3 then output is: [1, 2] The bitwise OR of 1 and 2 equals 3. It is not possible to obtain a subset of length greater than 2.AlgorithmBelow are the properties of bitwise OR −0 OR 0 = 0 1 OR 0 = 1 1 OR 1 = 1for all the positions in the binary representation of k with the bit equal to 0, the corresponding ... Read More

Maximum subarray sum modulo m in C++

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

488 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

762 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

208 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

473 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

322 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

163 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

Multiply Strings in C++

Aishwarya Naglot
Updated on 11-Nov-2024 15:46:46

9K+ Views

We have given two strings consisting of integers and can have lengths up to 200. both strings do not contain any leading 0, but 0 as the number itself can be present. We have to multiply integer strings, so we need to find a solution. Let's see how we can tackle this problem in an easier way. Suppose we have two numbers as strings. We need to multiply them and return the result, also in a string. For example, if the numbers are "26" and "12", then the result will be "312". Following are various ways to ... Read More

Valid Sudoku in Python

Arnab Chakraborty
Updated on 27-Apr-2020 13:08:03

5K+ Views

Suppose we have one 9x9 Sudoku board. We have to check whether that is valid or now. Only the filled cells need to be validated according to the following rules −Each row must contain the digits from 1-9 without repetition.Each column must contain the digits from 1-9 without repetition.Each of the 9 (3x3) sub-boxes of the grid must contain the digits from 1-9 without repetition.Suppose the Sudoku grid is like −537619598686348317266284195879This is valid.To solve this, we will follow these steps −for i in range 0 to 8create some empty dictionary called row, col and block, row_cube := 3 * (i ... Read More

Find First and Last Position of Element in Sorted Array in Python

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

1K+ Views

Suppose we have an array of integers A. This is sorted in ascending order, we have to find the starting and ending position of a given target value. When the target is not found in the array, return [-1, -1]. So if the array is like [2, 2, 2, 3, 4, 4, 4, 4, 5, 5, 6], and target is 4, then the output will be [4, 7]To solve this, we will follow these steps −Initially res := [-1, -1], set low := 0, high := length of array Awhile low < highmid := low + (high – low)/2if A[mid] ... Read More

Advertisements