C++ Articles

Page 550 of 597

Find the longest path in a matrix with given constraints in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 607 Views

Suppose we have one square matrix of order n. It has all distinct elements. So we have to find the maximum length path, such that all cells along the path are in increasing order with a difference of 1. From one cell we can move to four directions. Left, Right, Top and Bottom. So if the matrix is like −129538467So the output will be 4. As the longest path is 6→7→8→ 9To solve this problem, we will follow this idea. We will calculate longest path beginning with every cell. Once we have got the longest for all cells, we return ...

Read More

Find the last non repeating character in string in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 290 Views

Suppose we have a string str. We have to find the last non-repeating character in it. So if the input string is like “programming”. So the first non-repeating character is ‘n’. If no such character is present, then return -1.We can solve this by making one frequency array. This will store the frequency of each of the character of the given string. Once the frequency has been updated, then start traversing the string from the last character one by one. Then check whether the stored frequency is 1 or not, if 1, then return, otherwise go for previous character.Example#include ...

Read More

Find minimum x such that (x % k) * (x / k) == n in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 246 Views

Given two positive integers n and k, and we have to find the positive integer x, such that (x % k)*(x / k) is same as n. So if the n and k are 4 and 6 respectively, then the output will be 10. So (10 % 6) * (10 / 6) = 4.As we know that the value of x % k will be in range [1 to k – 1] (0 is not included) Here we will find possible integer in the range that divides n and hence the given equation becomes: x = (n * k) / ...

Read More

Find minimum steps required to reach the end of a matrix in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 463 Views

Suppose we have a 2D matrix with positive integers. We have to find the minimum steps required to move from to the end of the matrix (rightmost bottom cell), If we are at cell (i, j), we can go to the cell (i, j+mat[i, j]) or (i+mat[i, j], j), We cannot cross the bounds. So if the matrix is like −212111111The output will be 2. Path will be (0, 0) →(0, 2) → (2, 2)Here we will use the Dynamic programming approach to solve this. Suppose we are at cell (i, j), we want to reach (n-1, n-1) cell. We ...

Read More

Find the first, second and third minimum elements in an array in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 305 Views

Suppose we have an array of n elements. We have to find the first, second and the third minimum elements in the array. First minimum is the minimum of the array, second min is minimum but larger than the first one, and similarly the third min is minimum but greater than second min.Scan through each element, then check the element, and relate the condition for first, second and third min elements conditions to solve this problem.Example#include using namespace std; int getThreeMins(int arr[], int n) {    int first = INT_MAX, sec = INT_MAX, third = INT_MAX;       for ...

Read More

Find all elements in array which have at-least two greater elements in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 407 Views

Suppose, we have an array of n numbers. We have to find all elements in array, which have at least two greater elements. If the array is like A = [2, 8, 7, 1, 5], then the result will be [2, 1, 5]To solve this, we will find second max element, then print all elements which is less than or equal to second max value.Example#include using namespace std; void searchElements(int arr[], int n) {    int first_max = INT_MIN, second_max = INT_MIN;    for (int i = 0; i < n; i++) {       if (arr[i] > first_max) ...

Read More

Find minimum area of rectangle with given set of coordinates in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 490 Views

Suppose we have an array of some points in XY plane. We have to find the minimum area of rectangle that can be formed from these points. The side of the rectangle should be parallel to the X and Y axes. If we cannot form the rectangle, then return 0. So if the array of points is like [(1, 1), (1, 3), (3, 1), (3, 3), (2, 2)]. The output will be 4. As the rectangle can be formed using the points (1, 1), (1, 3), (3, 1) and (3, 3).To solve this, give the points by x coordinates, so ...

Read More

Find maximum vertical sum in binary tree in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 196 Views

Suppose we have a binary tree. The task is to print maximum of the sum of all nodes in the vertical order traversal. So if the tree is like below −The vertical order traversal is like −4 2 1 + 5 + 6 = 12 3 + 8 = 11 7 9Here the maximum is 12. The approach is simple. We will perform the vertical order traversal, then find the sum and check for maximum.Example#include #include #include #include using namespace std; class Node {    public:    int key;    Node *left, *right; }; Node* getNode(int key){    Node* node ...

Read More

Find maximum power of a number that divides a factorial in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 309 Views

Suppose we have two numbers n and fact. We have to find the largest power of n, that divides fact! (factorial of fact). So if fact = 5, and n = 2, then output will be 3. So 5! = 120, and this is divisible by 2^3 = 8.Here we will use the Legendre’s formula. This finds largest power of a prime, that divides fact!. We will find all prime factors of n, then find largest power of it, that divides fact!.So if fact is 146, and n = 15, then prime factors of n are 5 and 3. Sofor ...

Read More

Why C++ is partially Object Oriented Language?

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 2K+ Views

As we know some basic features of an object oriented programming language are the Inheritance, Encapsulation, Polymorphism. Any language that supports these features completely are known as object oriented programming languages. Some languages like C++ supports these three but not fully, so they are partially object oriented language. Let us see the reason why C++ is not known as completely object oriented language.In C++, we need the main() function to start executing, but in C++, the main functions are not present inside the class. So we can also write code without using class in C++. Some OOP languages like JAVA, ...

Read More
Showing 5491–5500 of 5,962 articles
« Prev 1 548 549 550 551 552 597 Next »
Advertisements