Found 7197 Articles for C++

First element greater than or equal to X in prefix sum of N numbers using Binary Lifting in C++

sudhir sharma
Updated on 01-Feb-2022 10:56:48

320 Views

In this problem, we are given an array arr[] consisting of N numbers and an integer value x. Our task is to create a program for finding the first element greater than or equal to X in the prefix sum of N numbers using Binary Lifting.Prefix Sum of elements of an array is an array whose each element is the sum of all elements till that index in the initial array.Example − array[] = {5, 2, 9, 4, 1}prefixSumArray[] = {5, 7, 16, 20, 21}Let's take an example to understand the problem, Input: arr[] = {5, 2, 9, 4, 1}, ... Read More

Floor of every element in same array in C++

sudhir sharma
Updated on 01-Feb-2022 10:55:14

235 Views

In this problem, we are given an array arr[] of integer elements. Our task is to create a program to find the Floor of every element in the same array. If the floor of an element exists, we will print the floor otherwise print -1.Floor of an element in array is the closest element which is smaller than or equal to the element in the array.Let’s take an example to understand the problemInput: arr[] = {3, 1, 5 ,7, 8, 2} Output: 2 -1 3 5 7 1Solution ApproachAn approach to solve the problem is by using nested loops. One ... Read More

Floor in a Sorted Array in C++

sudhir sharma
Updated on 01-Feb-2022 10:44:34

551 Views

In this problem, we are given a sorted array arr[] and an integer value x. Our task is to create a program to find the floor in a sorted array.Floor of X in sorted array arr is the largest element of the array arr[] which is smaller than or equal to x.Let’s take an example to understand the problemInput: arr[] = {2, 5, 6, 8, 9, 12, 21, 25}, x = 10 Output: 9Explanation − In the above array 9 is the largest number which is smaller than or equal to 10.Solution ApproachA simple solution to the problem is directly ... Read More

Finding the path from one vertex to rest using BFS in C++

sudhir sharma
Updated on 01-Feb-2022 10:51:38

591 Views

In this problem, we are given a directed graph represented as an adjacency list. Our task is to create a program for finding the path from one vertex to rest using BFS.BFS(Breadth First Search) is an algorithm that traverses a graph in a breadthward motion and uses a queue to remember to get the next vertex to start a search, when a dead end occurs in any iteration.Let's take an example to understand the problem,Input −OutputSA

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

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

482 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

665 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

589 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

485 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

3K+ 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

819 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

Advertisements