Found 7347 Articles for C++

Bitwise AND of sub-array closest to K in C++

sudhir sharma
Updated on 05-Aug-2020 07:55:34

1K+ Views

In this problem, we are given an array arr[] of size n and an integer k. Our task is to find the subarray within from index i to j and compute bitwise AND of all its elements. After this print minimum value of abs(K- (bitwise AND of subarray)).Let’s take an example to understand the problem, Input − arr[] = {5, 1}, k = 2Output −To solve the problem, there can be a few methods.One simple solution will be using the direct method. By finding bitwise AND for all sub-arrays then finding the |K-X|.Step 1 − Find the bitwise AND for ... Read More

Bitwise AND of N binary strings in C++

sudhir sharma
Updated on 05-Aug-2020 07:53:08

566 Views

In this problem, we are given an array bin[] of size n of binary strings. Our task is to create a program to find the Bitwise AND (&) of N binary strings.Here, we will take all numbers and find the bitwise AND of them i.e. bin[0] & bin[1] &... bin[n-2] & bin[n]Let’s take an example to understand the problem, Input −bin[] = {“1001”, “11001”, “010101”}Output −000001Explanation − Bitwise AND of all binary string −(1001) & (11001) & (010101) = 000001To solve this problem, a direct and simple approach is to find the bitwise AND of two binary strings and then ... Read More

Bitwise and (or &) of a range in C++

sudhir sharma
Updated on 05-Aug-2020 07:49:12

133 Views

In this problem, we are given two integer values a and b. And our task is to find the bitwise and (&) of range from a to b. This means we will have to find the value of a & a+1 & a+2 & … b-1 & b.Let’s take an example to understand the problem, Input − a = 3 , b = 8Output − 0Explanation − 3 & 4 & 5 & 6 & 7 & 8 = 0To solve the problem, a simple solution is starting from a and find bitwise and of all numbers by increasing one ... Read More

Bits manipulation (Important tactics) in C++

sudhir sharma
Updated on 05-Aug-2020 07:47:30

3K+ Views

Let’s first recall the about bits and the bitwise operator is short.Bit is a binary digit. It is the smallest unit of data that is understandable by the computer. In can have only one of the two values 0 (denotes OFF) and 1 (denotes ON).Bitwise operators are the operators that work a bit level in the program.These operators are used to manipulate bits in the program.In C, we have 6 bitwise operators −Bitwise AND (&)Bitwise OR (OR)Bitwise XOR (XOR)Bitwise left Shift (>)Bitwise not (~)https://www.tutorialspoint.com/cprogramming/c_bitwise_operators.htmNow, let’s learn some important tactics i.e. things that can be helpful if you work with bits.Swap ... Read More

Bitonic Sort in C++

sudhir sharma
Updated on 05-Aug-2020 07:44:00

767 Views

The bitonic sort is a parallel sorting algorithm that is created for best implementation and has optimum usage with hardware and parallel processor array.It is not the most effective one though as compared to the merge sort. But it is good for parallel implementation. This is due to the predefined comparison sequence which makes comparisons independent of data that are to be sorted.For bitonic sort to work effectively the number of elements should be in a specific type of quantity i.e. the order 2^n.One major part of the bitonic sort is the bitonic sequence which is a sequence whose elements ... Read More

Bitmasking and Dynamic Programming in C++

sudhir sharma
Updated on 05-Aug-2020 07:41:03

3K+ Views

First, we will learn about bitmasking and dynamic programming then we will solve a problem related to it that will solve your queries related to the implementation.Bitmask also known as mask is a sequence of N -bits that encode the subset of our collection. The element of the mask can be either set or not set (i.e. 0 or 1). This denotes the availability of the chosen element in the bitmask. For example, an element i is available in the subset if the ith bit of mask is set. For the N element set, there can be a 2N mask ... Read More

Maximum elements which can be crossed using given units of a and b in C++

Sunidhi Bansal
Updated on 04-Aug-2020 13:02:27

47 Views

Given a binary array arr[] and two variables a and b with some initial values. To cross an element in the array arr[] there are two ways −If arr[i] == 1, then 1 unit can be used from a, with no change in b. If 1 unit is used from b, then a increases by 1 unit. (Note that the value of a cannot be incremented above its original value.)If arr[i] == 0, then 1 unit can be used from a or b.Let’s now understand what we have to do using an example −Inputarr[] = {0, 0, 0, 1, 1}, ... Read More

Maximum elements that can be made equal with k updates in C++

Sunidhi Bansal
Updated on 04-Aug-2020 12:53:47

231 Views

Given the task is to find the maximum number of elements that can be made equal in a given array after incrementing its elements by at-most k times.Let’s now understand what we have to do using an example −Inputa[] = {1, 3, 8}, k = 4Output2ExplanationHere we can obtain two fours by incrementing 1 three times and incrementing 3 four times, that makes a[] = {4, 4, 8}Inputarr = {2, 5, 9}, k = 2Output0Approach used in the below program as followsIn main() function initialize int a[], size and k to store the array elements, size of array and the ... Read More

Maximum distinct lowercase alphabets between two uppercase in C++

Sunidhi Bansal
Updated on 04-Aug-2020 12:50:31

230 Views

Given the task is to find the maximum number of distinct lower case alphabets that are present between two upper case alphabets in the given string.Let’s now understand what we have to do using an example −Inputstr = “JKyubDoorG”Output3Explanation“yub” is present between the two upper case alphabets K and D which makes the count 3.“oor” is also present between the two upper case alphabets D and G which makes the count 2 as ‘o’ is a repeating alphabet and we are looking for distinct alphabets.Therefore, the output is 3.Inputstr = “ABcefsTaRpaep”Output4Approach used in the below program as followsIn function Max() ... Read More

Maximum difference of zeros and ones in binary string in C++

Sunidhi Bansal
Updated on 04-Aug-2020 12:45:15

240 Views

Given the task is to find a sub-string from a given binary string and then the maximum difference between the number of zeroes and the ones.Let’s now understand what we have to do using an example −Inputstr = “100100110”Output2ExplanationIn the sub-array from the position 1 to 5 (“00100”), the difference between the zeros and ones = 4 – 1 = 3 which is the maximum that can be found.Inputstr = “00000”Output5Approach used in the below program as followsIn main() function create a string str to store the binary string. Also initialize a variable int size to store the size of ... Read More

Advertisements