C++ Articles

Page 568 of 597

Minimum element in a max heap in C++.

Narendra Kumar
Narendra Kumar
Updated on 23-Sep-2019 470 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
Narendra Kumar
Updated on 23-Sep-2019 276 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
Narendra Kumar
Updated on 23-Sep-2019 241 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

Program to check if a matrix is Binary matrix or not in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 23-Sep-2019 433 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 ...

Read More

Program for Markov matrix in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 23-Sep-2019 539 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 Matrix Now, 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 ...

Read More

Program to check if matrix is upper triangular in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 23-Sep-2019 925 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 upper triangular matrix or not. Upper Triangular Matrix Upper triangular matrix is a matrix in which the elements above the main diagonal(including the main diagonal) are not zero and below elements are zero only. Like in the given Example below − In above figure the red highlighted elements are lower elements from the main diagonal which are zero and rest elements are non-zero. Example ...

Read More

Program to check if matrix is lower triangular in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 23-Sep-2019 637 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 lower triangular matrix or not. Lower Triangular Matrix − Lower triangular matrix is a matrix in which the elements below the main diagonal(including the main diagonal) are not zero and above elements are zero only. Like in the given Example below − In above figure the red highlighted elements are upper elements from the main diagonal which are zero and rest elements are non-zero. Example ...

Read More

Program to check Involutory Matrix in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 23-Sep-2019 224 Views

Given a matrix M[r][c], 'r' denotes number of rows and 'c' denotes number of columns such that r = c forming a square matrix. We have to check whether the given square matrix is an Involutory matrix or not. Involutory Matrix A matrix is called Involutory matrix if and only if, when a matrix gets multiplied with itself and its result is an identity matrix. A matrix I is Identity matrix if and only if its main diagonal is one and other elements than the main diagonal are zero. So, we can say a matrix is Involutory matrix ...

Read More

Program to check idempotent matrix in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 23-Sep-2019 419 Views

Given a matrix M[r][c], 'r' denotes number of rows and 'c' denotes number of columns such that r = c forming a square matrix. We have to check whether the given square matrix is an Idempotent matrix or not. Idempotent Matrix A matrix 'M' is called Idempotent matrix if and only the matrix 'M' multiplied by itself returns the same matrix 'M' i.e. M * M = M. Like in the given example below − We can say that the above matrix is multiplied by itself and returns the same matrix; hence the matrix ...

Read More

Program to check diagonal matrix and scalar matrix in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 23-Sep-2019 693 Views

Given a matrix M[r][c], 'r' denotes number of rows and 'c' denotes number of columns such that r = c forming a square matrix. We have to find whether the given square matrix is diagonal and scalar matrix or not, if it is diagonal and scalar matrix then print yes in the result. Diagonal matrix A square matrix m[][] will be diagonal matrix if and only if the elements of the except the main diagonal are zero. Like in the given figure below − Here, the elements in the red are main diagonal which ...

Read More
Showing 5671–5680 of 5,962 articles
« Prev 1 566 567 568 569 570 597 Next »
Advertisements