C++ Articles

Page 170 of 597

Maximum Product Subarray | Added negative product case in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 255 Views

In this problem, we are given an array of integers(positive as well as negative). Our task is to create a program to calculate the Maximum Product Subarray in C++.Problem Solution − Here, we have an array that contains positive, negative, and zero numbers. We need to find the product of subarrays created by elements of the array. And maximize the product of the subarray.Let’s take an example to understand the problem, Inputarr[] = {-1, 2, -7, -5, 12, 6}Output5040ExplanationThe subarray with the maximum product is {2, -7, -5, 12, 6}Product = 5040Solution ApproachTo solve this problem, we are given an ...

Read More

Maximum size of sub-array that satisfies the given condition in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 254 Views

In this tutorial, we will be discussing a program to find maximum size of sub-array that satisfies the given condition.For this we will be provided with an array of integers. Our task is to find the maximum length subset of that array satisfying either of arr[k] > arr[k + 1] when k is odd and arr[k] < arr[k + 1] when k is even, arr[k] > arr[k + 1] when k is even and arr[k] < arr[k + 1] when k is odd.Example#include using namespace std; //comparing values of a and b int cmp(int a, int b) {    return ...

Read More

Maximum profit after buying and selling the stocks in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 935 Views

In this problem, we are given an array stkprice[] that denotes the price of a certain stock on i-th day. Our task is to create a program to calculate Maximum profit after buying and selling the stocks in C++.Problem Description − Here, we need to check when can be bought and sell the stock to gain profit. To gain profit, we need to buy the stock at a low price and sell it when the price goes up. And repeat the same when the drop is encountered again.Let’s take an example to understand the problem, Inputstkprice[] = {120, 310, 405, ...

Read More

Maximum size rectangle binary sub-matrix with all 1s in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 290 Views

In this tutorial, we will be discussing a program to find maximum size rectangle binary sub-matrix with all 1s.For this we will be provided with 2D matrix containing zeroes and ones. Our task is to find the largest 2D matrix subset containing only ones.Example#include using namespace std; #define R 4 #define C 4 //finding the maximum area int maxHist(int row[]) {    stack result;    int top_val;    int max_area = 0;    int area = 0;    int i = 0;    while (i < C) {       if (result.empty() || row[result.top()]

Read More

Maximum score after flipping a Binary Matrix atmost K times in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 392 Views

In this problem, we are given a 2-D array arr[] consisting of boolean values (i.e 0’s and 1’s) and an integer K. Our task is to create a program to find the Maximum score after flipping a Binary Matrix atmost K times in C++.Problem Description − Here, for the 2-D array, and the k moves, we need to find the number that is created by the elements of the array. In each move, we will take a row or column and flip all the elements of the row or column. The choice will be done to keep in mind that ...

Read More

Longest Continuous Increasing Subsequence in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 923 Views

Suppose we have an array of integers; we have to find the length of longest continuous increasing subarray.So, if the input is like [2,4,6,5,8], then the output will be 3. As the longest continuous increasing subsequence is [2,4,6], and its length is 3.To solve this, we will follow these steps −if size of nums

Read More

Second Minimum Node In a Binary Tree in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 673 Views

Suppose there is a non-empty special binary tree with some non-negative value, here each node in this tree has exactly two or zero children. If the node has two children, then this node's value is the smaller value among its two children. In other words, we can say that [root.val = minimum of root.left.val, root.right.val]. If we have such binary tree, we have to find the second minimum value in the set made of all the nodes' value in the whole tree. If there is no such element, then return -1 instead.So, if the input is likethen the output will ...

Read More

2 Keys Keyboard in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 276 Views

Suppose we have only one character 'A' in a text editor. We can perform two operations on this letter for each step −Copy All − We can copy all the characters present on the notepadPaste − We can paste the characters which are copied last time.Now suppose we have a number n. We have to get exactly n 'A' on the notepad by performing the minimum number of steps permitted. We have to find the result in the minimum number of steps to get n 'A'. So if the given n is 3, then answer will be 3, so initially ...

Read More

Assign Cookies in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 811 Views

Suppose we are trying to distribute some cookies to children. But, we should give each child at most one cookie. Now each child i has a greed factor gi, which is the minimum size of a cookie that the child will be content with; and each cookie j has a size sj. When sj >= gi, we can assign the cookie j to the child i, and the child i will be content. Our goal is to maximize the number of content children and output the maximum number.So, if the input is like [1, 2], [1, 2, 3], then the ...

Read More

24 Game in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 2K+ Views

Suppose we have four cards; these cards are holding some number from 1 to 9. We have to check whether they could operate through some operators like +, -, *, /, to get 24. So if we have some numbers like [4,9,2,6], then we can get 24 by (4 * 9) – (2 * 6), answer will be true.To solve this, we will follow these steps −epsilon := 10.0^-5Define a function solve(), this will take an array v,if size of v is same as 1, then −return true when |v[0] - 24.0|

Read More
Showing 1691–1700 of 5,962 articles
« Prev 1 168 169 170 171 172 597 Next »
Advertisements