Found 7347 Articles for C++

Find the number of islands Using DFS in C++

sudhir sharma
Updated on 11-Feb-2022 10:27:51

190 Views

In this problem, we are given a 2D binary matrix. Our task is to Find the number of islands Using DFS.Island is a ground of 1 or more connected 1’s in the matrix.Let’s take an example to understand the problem, Input : bin[][] = {{ 1 0 0 0}    {0 1 0 1}    {0 0 0 0}    {0 0 1 0}} Output : 3 ExplanationIslands are −bin00 - bin11bin13bin32 Solution ApproachTo solve the problem using DFS, we will use the DFS technique for exploring all the neighbours (maximum possible 8 of a number in the matrix) and ... Read More

Program to find Nth Even Fibonacci Number in C++

sudhir sharma
Updated on 11-Feb-2022 07:17:27

516 Views

In this problem, we are given an integer value N. Our task is to find Nth Even Fibonacci Number.Fibonacci Series generates subsequent number by adding two previous numbers. The Fibonacci series starts from two numbers − F0 & F1. The initial values of F0 & F1 can be taken 0, 1 or 1, 1 respectively.Let’s take an example to understand the problem, Input : N = 4 Output : 144Solution ApproachA simple solution to the problem is using the fact that every third number in the fibonacci sequence is even and the sequence of even numbers also follows the recursive ... Read More

First X vowels from a string in C++

sudhir sharma
Updated on 01-Feb-2022 11:29:38

173 Views

In this problem, we are given string str[] of size N and an integer X. Our task is to create a program to print First X vowels from a string.We will print first X vowels from the string and if less than X vowels are present, print -1.Let's take an example to understand the problem, Input: str = "learn C programming language", X = 5 Output: e, a, o, a, i Vowels are a, e, i, o, uSolution ApproachA simple solution to the problem is by traversing the string character by charter. And storing all the vowels of the string ... Read More

First string from the given array whose reverse is also present in the same array in C++

sudhir sharma
Updated on 01-Feb-2022 11:25:56

119 Views

In this problem, we are given an array of string str[] of size N. Our task is to create a program for finding the first string from the given array whose reverse is also present in the same array.Let's take an example to understand the problem, Input: str[] = ["python", "program", "C#", "language", "#C"] Output: C#Solution ApproachOne way to solve the problem is by directly traversing each element of the string array and checking for revere of the string in the remaining array. Return the string if the reverse is found. If the whole array is traversed and no string ... Read More

First non-repeating in a linked list in C++

sudhir sharma
Updated on 01-Feb-2022 11:19:56

159 Views

In this problem, we are given a linked list LL of size N. Our task is to create a program for finding non-repeating in linked list.Linked list is a sequence of data structures, which are connected together via links.Let's take an example to understand the problem, Input: LL = 4 => 6 => 2 => 4 => 1 => 2 => 6 => 5 Output: 1Explanation −The elements with a single occurrence frequency are 1 and 6. Out of these 1 occurred first in the linked list. Solution ApproachAn approach to solve the problem is by creating a hash table ... Read More

First negative integer in every window of size k in C++

sudhir sharma
Updated on 01-Feb-2022 11:16:06

847 Views

In this problem, we are given an array arr[] consisting of N integer values and a window of size N. Our task is to create a program for finding the first negative integer in every window of size k. We will be printing the first negative number if it exists otherwise print 0, denoting no negative exists.Let's take an example to understand the problem, Input: arr[] = {-2, 2, -1, 4, 3, -6}, k = 2 Output: -2, -1, -1, 0, -6Explanation −Size of window k = 2, {-2, 2}, first negative is -2 {2, -1}, first negative is -1 ... Read More

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

196 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

146 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

358 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

472 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

Advertisements