Java Code for ROI (TCS Codevita) | PrepInsta
ROI
TCS CodeVita is a coding competition organized by TCS every year, in search of world’s best coder. This is a global level coding competition in which coders from all around the world compete for the title of World’s Best Coder. ROI is one of the sample problem of this year TCS CodeVita season 11 competition.
Question -: You must be aware of the concept of Stocks Portfolio. A stock portfolio is a collection of stock(s) that you invest into with an objective of making profit.
Stocks are bought and sold. Selling price minus buying price is realized profit or loss. In case a stock is not sold yet, if buying price is more than or less than the current stock market price, then it is termed as unrealized profit or loss, respectively.
Given information in form of <Quantity of Stock bought, time of purchase, time of sell, array of prices>, calculate the realized P/L and unrealized P/L at the given time.
Constraints
1<= No. Of Stocks (N) <=10^2
1<= Price of Stock <= 2*10^4
1<= M <= 365
1 <= Time of Purchase <= Time of Sell <= Length of list
Input
First line contains an integer N which denotes the number of stocks in the portfolio.
Next N lines contain a space separate tuple of 3 integers which denote < Quantity Bought, Time of Purchase, Time of Sell > for each stock. If the stock has not been sold, the Time of Sell will be 0.
The N+1 line contain an integer M which denotes number of days for which price of stock is provided
Then the next N lines contain M integers which denote the stock price from time T1 to TM.
The last line will be the time instance at which the the P/L needs to be computed.
Output
Print realized P/L on first line
Print unrealized P/L on the second line
Time Limit (secs)
1
Examples
Example 1
Input
3
10 4 20
10 1 11
100 6 0
22
113 115 112 113 115 112 113 115 112 113 115 112 113 115 112 113 115 112 113 115 112 117
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
5
Output
0
60
Explanation
From input we know the following
- Portfolio contains of 3 stocks. Quantity, time of purchase and time of selling is known for each stock
- Stock prices of all 3 stocks in portfolio is given for 22 days
- We also know that first line of the stock prices belong to first stock, second line to second stock, so on and so forth
- We are interested in P/L position at the end of 5th day
Example 2
Input
3
10 4 20
10 1 11
100 6 0
22
113 115 112 113 115 112 113 115 112 113 115 112 113 115 112 113 115 112 113 115 112 117
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
20
Output
120
1400
Explanation
Till day 20 we have brought all the above listed stocks and only two have been matured that is stock number 1 and 2 therefore the total realized profit is 20 + 100 = 120 and stock number 3 has not been sold therefore the total unrealized profit is 1400.
From input we know the following
- Portfolio contains of 3 stocks. Quantity, time of purchase and time of selling is known for each stock
- Stock prices of all 3 stocks in portfolio is given for 22 days
- We also know that first line of the stock prices belong to first stock, second line to second stock, so on and so forth
- We are interested in P/L position at the end of 20th day
Example 3
Input
3
10 4 6
10 1 11
100 6 0
22
113 115 112 113 115 112 113 115 112 113 115 112 113 115 112 113 115 112 113 115 112 117
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
10
Output
-10
490
Explanation
From input we know the following
- Portfolio contains of 3 stocks. Quantity, time of purchase and time of selling is known for each stock
- Stock prices of all 3 stocks in portfolio is given for 22 days
- We also know that first line of the stock prices belong to first stock, second line to second stock, so on and so forth
- We are interested in P/L position at the end of 10th day
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.*;
public class StockAnalysis {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
List< Tuple< Integer, Integer, Integer>> stocks = new ArrayList<>();
for (int i = 0; i < n; ++i) {
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
stocks.add(new Tuple<>(a, b, c));
}
int m = scanner.nextInt();
int real = 0, unreal = 0;
List< List< Integer>> placementlelo = new ArrayList<>();
for (int i = 0; i < n; ++i) {
List< Integer> arr = new ArrayList<>();
for (int j = 0; j < m; ++j) {
arr.add(scanner.nextInt());
}
placementlelo.add(arr);
}
int day = scanner.nextInt();
for (int i = 0; i < n; ++i) {
Tuple< Integer, Integer, Integer> stock = stocks.get(i);
int a = stock.first;
int b = stock.second;
int c = stock.third;
if (b > day) {
continue;
} else if (c > day || c == 0) {
unreal += a * (placementlelo.get(i).get(day - 1) - placementlelo.get(i).get(b - 1));
} else {
real += a * (placementlelo.get(i).get(c - 1) - placementlelo.get(i).get(b - 1));
}
}
System.out.println(real);
System.out.print(unreal);
}
}
class Tuple< A, B, C> {
public final A first;
public final B second;
public final C third;
public Tuple(A first, B second, C third) {
this.first = first;
this.second = second;
this.third = third;
}
}
