Minimum Flips to Maximize a Number with K Set Bits in C++

Narendra Kumar
Updated on 23-Sep-2019 10:49:41

171 Views

Problem statementGiven two numbers n and k, we need to find the minimum number of flips required to maximize given number by flipping its bits such that the resulting number has exactly k set bits. Please note input must satify condition that k < number of bits in n.ExampleLet us suppose n = 9 and k = 2Binary representation of 9 is − 1001. It contains 4 bits.Largest 4 digit binary number with 2 set bits is − 1100 i.e. 12To convert 1001 to 1100 we have to flip highlighed 2 bitsAlgorithm1. Count the number of bits in n. Let ... Read More

Minimum Flips in Two Binary Arrays for Equal XOR

Narendra Kumar
Updated on 23-Sep-2019 10:41:53

210 Views

Problem statementGiven three arrays with 0’s and 1’s of size n, the task is to find minimum flip of bits in the first and second array such that the XOR of i’th index bit of first and second array is equal to i’th index bit of the third array.Please note that we can only flip at most p bits of array 1 and at most q bits of array 2. Also rearranging array elements is not allowed.Let us suppose p = 2 and q = 5arr1[] = {1, 0, 1, 1, 0, 1, 0} arr2[] = {0, 1, 0, 1, ... Read More

Check If a Matrix Is Symmetric in C++

Sunidhi Bansal
Updated on 23-Sep-2019 10:40:30

718 Views

In linear algebra a matrix M[][] is said to be a symmetric matrix if and only if transpose of the matrix is equal to the matrix itself. A transpose of a matrix is when we flip the matrix over its diagonal, which resultant switches its row and columns indices of the matrix.Below the example of Symmetric matrix −$$\begin{bmatrix} 1 & 4 & 7 \ 4 & 5 & 6 \ 7 & 6 & 9 \ \end {bmatrix} \Rightarrow \begin{bmatrix} 1 & 4 & 7 \ 4 & 5 & 6 \ 7 & 6 & 9 \ \end{bmatrix}$$The above ... Read More

Minimum Element in a Max Heap in C++

Narendra Kumar
Updated on 23-Sep-2019 10:34:50

411 Views

Problem statementFind the element with the least value in max heap.Let us consider below max heap.In max heap value of the root node is always greater than its childer. Because of this property, we can conclude that value will be present in one of the leaf nodes. If heap contains n nodes then there will be ceil(n/2) leaves.Max heap is a complete binary tree hence it can be represented in an array. In such heap the first leaf will be present after floor(n/2) index. So in our example, the first leave will be present at index 5.AlgorithmWe can use the ... Read More

Minimum Delete Operations to Make All Elements of Array Same in C++

Narendra Kumar
Updated on 23-Sep-2019 10:29:22

218 Views

Problem statementGiven an array of n elements such that elements may repeat. We can delete any number of elements from array. The task is to find a minimum number of elements to be deleted from array to make it equal.arr[] = {10, 8, 10, 7, 10, -1, -4, 12}We have to delete highlighted 5 elements to make all array elements the same.Algorithm1. Count frequency of each element 2. Find maximum frequecy among the frequencies. Let us call this as maxFrequncy 3. Elements to be deleted: n – maxFrequecy where n is size of an arrayExample#include #include #include ... Read More

Minimum and Maximum Prime Numbers of a Singly Linked List in C++

Narendra Kumar
Updated on 23-Sep-2019 10:25:58

191 Views

Problem statementGiven a linked list of n positive integers. We have to find the prime number with minimum and maximum value.If the given list is −10 -> 4 -> 1 -> 12 -> 13 -> 7 -> 6 -> 2 -> 27 -> 33 then minimum prime number is 2 and maximum prime number is 13Algorithm1. Find maximum number from given number. Let us call it maxNumber 2. Generate prime numbers from 1 to maxNumber and store them in a dynamic array 3. Iterate linked list and use dynamic array to find prime number with minimum and maximum valueExample#include ... Read More

Find Minimum and Maximum Prime Numbers in an Array Using C

Narendra Kumar
Updated on 23-Sep-2019 10:21:56

803 Views

Problem statementGiven an array of n positive integers. We have to find the prime number with minimum and maximum value.If the given array is −arr [] = {10, 4, 1, 12, 13, 7, 6, 2, 27, 33} then minimum prime number is 2 and maximum prime number is 13Algorithm1. Find maximum number from given number. Let us call it maxNumber 2. Generate prime numbers from 1 to maxNumber and store them in a dynamic array 3. Iterate input array and use dynamic array to find prime number with minimum and maximum valueExample#include #include #include #define SIZE(arr) (sizeof(arr) ... Read More

Check if a Matrix is a Binary Matrix in C++

Sunidhi Bansal
Updated on 23-Sep-2019 08:39:54

355 Views

A binary matrix is a matrix whose all elements are binary values i.e., 0 or 1. Binary matrix can be also called Boolean matrix, Relational Matrix, Logical matrix.Given below the example           $$\begin{bmatrix} 0 & 1 & 0\ 1 & 1 & 0\ 1 & 0 & 1\ \end {bmatrix}\:\:\:\:\:\:\:\:\:\begin{bmatrix}0 & 3 & 0\ 1 & 1 & 0\ 1 & 0 & 2\ \end{bmatrix}\\tiny This\:is\:a\:Binary\:Matrix\:\:\:\:\:\:\:This\:is\:not\:a\:binary\:matrix$$In Above figure first matrix on the left is a Binary matrix, the other matrix there are some values which are not Binary(0 or 1) are highlighted in red i.e., 3 and ... Read More

Program for Markov Matrix in C++

Sunidhi Bansal
Updated on 23-Sep-2019 08:18:42

484 Views

Given a matrix M[r][c] with ‘r’ number of rows and ‘c’ number of columns, we have to check that the given matrix is Markov matrix or not. If the input matrix is Markov matrix then print the output “It is a Markov matrix” and “it’s not an Markov matrix” if it is not a Markov matrix.Markov MatrixNow, what is Markov Matrix? A matrix M is a Markov matrix if and only if its sum of each row is equal to only 1.Like in the given example below −$$\begin{bmatrix}0.2 & 0.3 & 0.5 \0.1 & 0.7 & 0.2 \0.4 & 0.5 ... Read More

Program for Identity Matrix in C

Sunidhi Bansal
Updated on 23-Sep-2019 08:05:23

1K+ Views

Given a square matrix M[r][c] where ‘r’ is some number of rows and ‘c’ are columns such that r = c, we have to check that ‘M’ is identity matrix or not.Identity MatrixIdentity matrix is also known as Unit matrix of size nxn square matrix where diagonal elements will only have integer value one and non diagonal elements will only have integer value as 0Like in the given Example below −$$I1=\begin{bmatrix}1 \end{bmatrix}, \ I2=\begin{bmatrix}1 & 0 \0 & 1 \end{bmatrix}, \ I3=\begin{bmatrix}1 &0 & 0 \0 &1 & 0 \0 &0 &1 \end{bmatrix}, \In=\begin{bmatrix}1 &0 &0  &...&0 \0 &1 &0 ... Read More

Advertisements