Found 33676 Articles for Programming

Maximum rational number (or fraction) from an array in C++

Ayush Gupta
Updated on 15-Sep-2020 14:14:32

273 Views

In this problem, we are given a 2-D array that contains rational numbers (one in each row). Our task is to create a program to calculate the maximum rational number (or fraction) from an array in C++.Problem Description − The 2-D array is of the form [n][2]. Each row has two integer values that denote the value of a and b in the equation of rational number, a/b. We need to find the greatest number out of all these rational numbers.Let’s take an example to understand the problem, Inputrat[][] = {    {3, 2},    {5, 7},    {1, 9}, ... Read More

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

Ayush Gupta
Updated on 15-Sep-2020 14:15:16

844 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 product subset of an array in C++

Ayush Gupta
Updated on 03-Jul-2020 07:53:26

205 Views

In this tutorial, we will be discussing a program to find maximum product subset of an array.For this we will be provided with an array containing positive and negative values. Our task is to find the maximum product for a subset of the array.Example Live Demo#include using namespace std; int maxProductSubset(int a[], int n) {    if (n == 1)       return a[0];       int max_neg = INT_MIN;       int count_neg = 0, count_zero = 0;       int prod = 1;    for (int i = 0; i < n; i++) { ... Read More

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

Ayush Gupta
Updated on 15-Sep-2020 14:16:48

199 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 Product Subarray - Using Two Traversals in C++

Ayush Gupta
Updated on 15-Sep-2020 14:17:28

206 Views

In this problem, we are given an array arr[] of integers. Our task is to create a program to find the Maximum Product Subarray - Using Two Traversals in C++.Problem description − Here in the array, we will find the maximum product subarray using two traversals one from index 0 and another index (n-1).Let’s take an example to understand the problem, Inputarr[] = {4, -2, 5, -6, 0, 8}Output240ExampleSubarray = {4, -2, 5, -6} Maximum product = 4 * (-2) * 5 * (-6) = 240Solution ApproachTo solve this problem using two traversals. Here, we will find the maximum product ... Read More

Maximum product quadruple (sub-sequence of size 4) in array in C++

Ayush Gupta
Updated on 15-Sep-2020 14:18:33

392 Views

In this problem, we are given an array arr[]. Our task is to create a program to find the Maximum product quadruple (sub-sequence of size 4) in array in C++.Code description − Here, we need to find a quadruple (sub-sequence of size 4) such that the product of all elements is maximum.Let’s take an example to understand the problem, Inputarr[] = {4, -2, 5, -6, 8}Output840ExplanationThe quadruple, (-3, 5, -7, 8) with product 840.Solution ApproachThere can be multiple solutions to a given problem.One simple solution is using the direct method by traversing the array. Then finding all possible quadruples in ... Read More

Maximum product of two non-intersecting paths in a tree in C++

Ayush Gupta
Updated on 15-Sep-2020 14:19:33

289 Views

In this problem, we are given an undirected connected tree T with n nodes. Our task is to create a program to find the Maximum product of two nonintersecting paths in a tree in C++.Problem Description − To find the maximum product of two nonintersecting paths in a tree. We will find all the non-interesting paths and then find the product of their lengths.Let’s take an example to understand the problem, InputGraph −Output8ExplanationThe non-intersecting paths that are considered are C-A-B and F-E-D-G-H.The lengths are 2 and 4. Product = 8.Solution ApproachThe solution to this problem is by traversing the tree ... Read More

Maximum product of subsequence of size k in C++

Ayush Gupta
Updated on 15-Sep-2020 14:20:26

493 Views

In this problem, we are given an array arr[] of integers and a number k. Our task is to create a program to find the Maximum product of subsequence of size k in C++.Problem Description − Here, we need to find the subsequence of size k, 1

PHP program to calculate the sum of square of first n natural numbers

AmitDiwan
Updated on 02-Jul-2020 07:14:49

650 Views

To calculate the sum of square of first n natural numbers in PHP, the code is as follows −Example Live DemoOutputThe sum of square of first 5 natural numbers is 125A function named ‘sum_of_squares’ is defined that takes the limit up to which square of natural number needs to be found. In this function, a sum value is initialized to 0. A ‘for’ loop is run over the elements ranging from 1 to the limit. Every time, to the ‘sum’ variable, square of the iterated value is added. When the control reaches the end, the value of sum is returned as ... Read More

PHP program to calculate the repeated subtraction of two numbers

AmitDiwan
Updated on 02-Jul-2020 07:13:50

263 Views

To calculate the repeated subtraction of two numbers, the code is as follows −Example Live DemoOutputThe repeated subtraction results in 18A function named ‘repeated_sub’ is defined that checks to see if two values divide each other completely, and if this is true, it divides the numbers and gives the floor value of the quotient. Otherwise, it gives the floor value of quotient and the value computed by calling the ‘repeated_sub’ function on the second value, and the remainder when the values are divided.Outside the function, values are given to both the variables and the function is called by passing these values ... Read More

Advertisements