I was trying out my hand at solving problems and came across this HackerRank problem. I have written a solution which passes for a few test cases but times out for most others (time limit - 3 seconds). I'd like to know the best way possible to achieve the same.
Any pointers as to how the code may be optimized, any approaches/algorithms I should look at, anything that would help me improve the code and optimize the performance would be much appreciated.
Reproducing the problem statement below in case the link stops working.
Jesse has started stock trading and loves it. He knows the prices of a share of a particular company over the next N days. He wants to analyze this data to build a model which can predict the best day to buy and sell the shares. Given an amount, he wants to know if the given desirable amount of profit can be made. If yes, he wants to know the minimum number of days in which it can be made. If there are multiple ways of buying and selling to achieve that profit, he wants to know the way which happens the earliest.
Note: Jesse can buy only 1 share and not more. He always has to buy before he can sell the share.
Input Format
The first line contains two integers N and D, where N is the number of days for which he knows the share values and D is the number of amounts for which he needs the answer. The next line contains N space separated integers, where Ni is the value of the share on the i+1th day. The next D lines contain a single integer Di, where Di is the profit that needs to be made.
Constraints
1 <= N <= 2x105
1 <= D <= 10
1 <= Ni, Di <= 108
Output Format
For each amount of profit given as a query, print in a new line containing two space separated integers ‐ the day on which the share was bought and the day on which the share was sold if an answer exists. If it is not possible to achieve the amount of profit, print -1.
Sample Input
6 2
3 1 2 1 4 5
3
2
Sample Output
4 5
3 5
Explanation
To achieve a profit of 3, he can either buy on day 2 or day 4 and sell on day 5 or he can buy on day 3 and sell on day 6. The one that takes the minimum number of days is the one where he buys on day 4 and sells on day 5. So the answer is 4 5.
My Code
struct Transaction
{
public int startDay;
public int endDay;
public int difference;
}
class Solution
{
static void Main(string[] args)
{
//get initial inputs
string inputs = Console.ReadLine();
string[] parsedInput = inputs.Split(' ');
int numberOfDays = Convert.ToInt32(parsedInput[0]);
int expectedProfitCases = Convert.ToInt32(parsedInput[1]);
//get share prices
string prices = Console.ReadLine();
string[] parsedPrices = prices.Split(' ');
IList<int> sharePrices = new List<int>();
foreach (string parsedPrice in parsedPrices)
{
sharePrices.Add(Convert.ToInt32(parsedPrice));
}
if (numberOfDays != sharePrices.Count)
{
throw new Exception("INVALID INPUT!");
}
//get expected profits
IList<int> expectedProfits = new List<int>();
for (int i = 0; i < expectedProfitCases; i++)
{
expectedProfits.Add(Convert.ToInt32(Console.ReadLine()));
}
IList<Transaction> transactions;
Transaction minimumDaysTransaction = new Transaction();
//loop through each expected profit, printing result at the end
foreach (int expectedProfit in expectedProfits)
{
//find all transactions which would yield the desired profit
transactions = new List<Transaction>();
for (int i = numberOfDays - 1; i >= 0; i--)
{
for (int j = 0; j < i; j++)
{
if ((sharePrices[i] - sharePrices[j]) == expectedProfit)
{
transactions.Add(new Transaction { startDay = j + 1, endDay = i + 1, difference = i - j });
}
}
}
//print -1 if no transactions yield desired profit
if (transactions.Count == 0)
{
Console.WriteLine("-1");
continue;
}
//find the transaction which takes least amount of days
if (transactions.Count > 1)
{
minimumDaysTransaction = transactions.OrderBy(x => x.difference).First();
}
else if(transactions.Count == 1)
{
minimumDaysTransaction = transactions[0];
}
Console.WriteLine(minimumDaysTransaction.startDay + " " + minimumDaysTransaction.endDay);
}
Console.ReadLine();
}
}