**Stock Maximize**

### Problem Statement :

Your algorithms have become so good at predicting the market that you now know what the share price of Wooden Orange Toothpicks Inc. (WOT) will be for the next number of days. Each day, you can either buy one share of WOT, sell any number of shares of WOT that you own, or not make any transaction at all. What is the maximum profit you can obtain with an optimum trading strategy? Example prices = [1,2] Buy one share day one, and sell it day two for a profit of 1. Return 1. prices = [2,1] No profit can be made so you do not buy or sell stock those days. Return 0. Function Description Complete the stockmax function in the editor below. stockmax has the following parameter(s): prices: an array of integers that represent predicted daily stock prices Returns int: the maximum profit achievable Input Format The first line contains the number of test cases t. Each of the next t pairs of lines contain: - The first line contains an integer n, the number of predicted prices for WOT. - The next line contains n space-separated integers prices[i], each a predicted stock price for day i. Constraints 1 <= t <= 10 1 <= n <= 5000 1 <= prices[i] <= 100000 Output Format Output t lines, each containing the maximum profit which can be obtained for the corresponding test case.

### Solution :

` ````
Solution in C :
In C++ :
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
long long int n,a[50005],i,idx,b[50005],sum=0;
scanf("%lld",&n);
for(i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
}
idx=n;
b[n]=n;
for(i=n-1;i>0;i--)
{
if(a[idx]<a[i])
idx=i;
b[i]=idx;
}
for(i=1;i<=n;i++)
{
if((a[b[i]]-a[i])>=0)
sum+=(a[b[i]]-a[i]);
}
cout<<sum<<endl;
}
}
In Java :
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int T = input.nextInt();
for (int t=0; t<T; t++) {
int N = input.nextInt();
int[] A = new int[N];
for (int n=0; n<N; n++) {
A[n] = input.nextInt();
}
boolean[] buy = new boolean[N];
int max = A[N-1];
for (int n=N-2; n>=0; n--) {
int value = A[n];
if (value > max) {
max = value;
} else {
buy[n] = true;
}
}
long money = 0;
long stock = 0;
for (int n=0; n<N; n++) {
if (buy[n]) {
money -= A[n];
stock++;
} else {
money += stock*A[n];
stock = 0;
}
}
System.out.println(money);
}
}
}
In C :
#include<stdio.h>
int maxarray(int *a,int start,int end)
{
int i,pos,big;
big=a[start];
pos=start;
for(i=start;i<=end;i++)
{
if(a[i]>big)
{
pos=i;
big=a[i];
}
}
return pos;
}
int main()
{
int t,n,i;
scanf("%d",&t);
while(t--)
{
long long pro=0;
int pos=0,start=0;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
while(pos!=(n-1))
{
pos=maxarray(a,start,n-1);
for(i=start;i<pos;i++)
{
pro=pro+(a[pos]-a[i]);
}
start=pos+1;
}
printf("%lld\n",pro);
}
return 0;
}
In Python3 :
test=int(input())
for i in range(test):
n=int(input())
a=list(map(int,input().split()))
c=0
i=len(a)-1
while(i>=0):
d=a[i]
l=i
p=0
while(a[i]<=d and i>=0):
p+=a[i]
i-=1
c+=(l-i)*a[l]-p
continue
print (c)
```

## View More Similar Problems

## Minimum Average Waiting Time

Tieu owns a pizza restaurant and he manages it in his own way. While in a normal restaurant, a customer is served by following the first-come, first-served rule, Tieu simply minimizes the average waiting time of his customers. So he gets to decide who is served first, regardless of how sooner or later a person comes. Different kinds of pizzas take different amounts of time to cook. Also, once h

View Solution →## Merging Communities

People connect with each other in a social network. A connection between Person I and Person J is represented as . When two persons belonging to different communities connect, the net effect is the merger of both communities which I and J belongs to. At the beginning, there are N people representing N communities. Suppose person 1 and 2 connected and later 2 and 3 connected, then ,1 , 2 and 3 w

View Solution →## Components in a graph

There are 2 * N nodes in an undirected graph, and a number of edges connecting some nodes. In each edge, the first value will be between 1 and N, inclusive. The second node will be between N + 1 and , 2 * N inclusive. Given a list of edges, determine the size of the smallest and largest connected components that have or more nodes. A node can have any number of connections. The highest node valu

View Solution →## Kundu and Tree

Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that

View Solution →## Super Maximum Cost Queries

Victoria has a tree, T , consisting of N nodes numbered from 1 to N. Each edge from node Ui to Vi in tree T has an integer weight, Wi. Let's define the cost, C, of a path from some node X to some other node Y as the maximum weight ( W ) for any edge in the unique path from node X to Y node . Victoria wants your help processing Q queries on tree T, where each query contains 2 integers, L and

View Solution →## Contacts

We're going to make our own Contacts application! The application must perform two types of operations: 1 . add name, where name is a string denoting a contact name. This must store name as a new contact in the application. find partial, where partial is a string denoting a partial name to search the application for. It must count the number of contacts starting partial with and print the co

View Solution →