Found 7347 Articles for C++

Finding the maximum square sub-matrix with all equal elements in C++

sudhir sharma
Updated on 01-Feb-2022 10:37:40

386 Views

In this problem, we are given a N*N matrix mat[]. Our task is finding the maximum square sub-matrix with all equal elements.In this problem, we need to find the maximum size of a sub-matrix from the given matrix whose all elements are the same.Let's take an example to understand the problem, Input: mat[][] = {{1, 2, 1}, {1, 2, 2}, {2, 2, 2}} Output: 2Explanation −matrix a11, a12, a21, a22 is of size 2X2 and forms a sub-matrix with all equal elements. Solution ApproachA simple solution to the problem is by traversing all the elements of the matrix and then ... Read More

Friends Pairing Problem in C++

sudhir sharma
Updated on 01-Feb-2022 10:21:49

459 Views

In this problem, we are given a positive integer N denoting the number of friends in a group. Our task is to create a program to solve the Friends Pairing Problem.Each friend of the group can either remain single or can pair up with one other friend. The pairing of each friend of the group can be done only once.Let’s take an example to understand the problemInput: n = 3 Output: 4 Explanation: Let’s say the 3 members of the group are A, B and C. The pairing can be done as : {A}, {B}, {C} {A, B}, {C} {A, ... Read More

Finding n-th number made of prime digits (2, 3, 5 and 7) only in C++

sudhir sharma
Updated on 01-Feb-2022 10:09:35

453 Views

In this problem, we are given a number N. Our task is to finding n-th number made of prime digits (2, 3, 5 and 7) only.The series consisting of prime digits only (2, 3, 5, 7) is, 2, 3, 5, 7, 22, 23, 25, 27, 32, 33...Let's take an example to understand the problem, Input: N = 6 Output: 23Solution ApproachA simple approach to solving the problem is by finding the number at the given index i.e. by finding the term of the series, for this we will be observing the series.We have four different prime numbers so the series ... Read More

Finding Median in a Sorted Linked List in C++

sudhir sharma
Updated on 01-Feb-2022 10:05:39

347 Views

In this problem, we are given a sorted linked list consisting of N elements. Our task is to finding Median in a Sorted Linked List.Sorted Linked List is a simple linked list in which all elements are sorted in a specific order. Example − 4 -> 6 -> 7 -> 9 -> NULLMedian is the middle elements of the linked list. It can be found as if N is odd : median is (n/2)th elementif N is even −s median is average of (n/2)th element and (n/2 + 1)th element.Let's take an example to understand the problem, Input: 2 -> ... Read More

Finding inverse of a matrix using Gauss Jordan Method in C++

sudhir sharma
Updated on 01-Feb-2022 10:01:21

2K+ Views

In this problem, we are given an 2D matrix mat[][]. Our task is to find inverse of a matrix using Gauss Jordan Method.Now, lets understand the basics of the problem, MATRIX is a two dimensional array of numbers.Example$\begin{bmatrix}2&5&4 \1&6&7 \9&3&8\end{bmatrix}$Inverse of Matrix [A-1] −It is an operation performed on square matrix. The following are the properties that are required for a matrix to have an inverse −Initial matrix should be square matrix.It must be non-singular matrix.An identity matrix I exist for the matrix A such that, $$AA^{-1} = A^{-1}.A = I$$Their is a formula that can be used to find ... Read More

Format String Vulnerability and Prevention with Example in C

sudhir sharma
Updated on 01-Feb-2022 10:08:09

704 Views

Format String − It is an ASCII string that is used for formatting strings. It is an ASCII string consisting of texts and formatting parameters.For formatting, the program’s output, there are various format strings in C.FORMAT STRING VULNERABILITIESThese are bugs that arise due to errors in programming that might be made easily by the programmer. If any such error-prone code blog is passed to output functions like printf, sprintf, etc. Then the write operation is performed to an arbitrary memory address.Example#include #include int main(){    char buffer[100];    strncpy(buffer, "Hii ", 5);    printf(buffer);    return 0; ... Read More

First and Last Three Bits in C++

sudhir sharma
Updated on 01-Feb-2022 09:51:49

163 Views

In this problem, we are given a number N. Our task is to find the decimal conversion of the fist three and last three bits for the given integer values N.Let's take an example to understand the problem, Input : 57 Output : 71Solution ApproachA simple solution is by changing the number n into its binary equivalent and then saving the bits in an array. After this, we will convert the first three and last three values from the array into numbers individually. The decimal conversion of both the sets of bits is our result.For example, take the number 80.The ... Read More

Finding the power of prime number p in n! in C++

sudhir sharma
Updated on 01-Feb-2022 09:48:43

168 Views

In this problem, we are given a number n and a prime number p. Our task is to find the power of prime number p in n!Let's take an example to understand the problem, Input : n = 6, p = 2 Output : 4Solution ApproachA simple solution to the problem is by simply finding the values of n!. And the factorize it, and find the power of prime number p in the factorization.Here, the number can be represented as the power factorization of 2 in 5! = 30 is 3.The value of n factorial is$$n!\:=\:n^*(n-1)^*(n-2)^*(n-3)\dotso{^*}2^*1$$ $$n!\:=\:3^*2^*1\:=\:6$$Let take n = 6 ... Read More

Finding n-th term of series 3, 13, 42, 108, 235... in C++

sudhir sharma
Updated on 01-Feb-2022 09:45:10

126 Views

In this problem, we are given a number n. Our task is to find the n-th term of series 3, 13, 42, 108, 235...Let's take an example to understand the problem, Input : 5 Output : 235Solution ApproachThe series can be represented as the sum of cubes of first n natural numbers. The formula for that is (n*(n+1)/2)2. Also if we add 2* to it we will get the required series.The formula for sum of the series is (n*(n+1)/2)2+2*n.For n = 5 sum by the formula is(5 * (5 + 1 ) / 2)) ^ 2 + 2*5= (5 * ... Read More

Form the smallest number using at most one swap operation in C++

sudhir sharma
Updated on 01-Feb-2022 10:01:59

286 Views

In this problem, we are given a positive integer. Our task is to create a program to form the smaller number using at most one swap operation.We will be creating a new number using the digits of the existing number. The smallest number formed can have only one digit swapped from the existing number.Let’s take an example to understand the problemInput: n = 63519 Output: 36519Solution ApproachOne method to solve the problem is by finding all the numbers created by swapping pair of digits of the given number. Out of all these swapped digit numbers, the smallest one is returned. ... Read More

Advertisements