1393. Capital Gain/Loss LeetCode Solution
In this guide, you will get 1393. Capital Gain/Loss LeetCode Solution with the best time and space complexity. The solution to Capital Gain/Loss problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Capital Gain/Loss solution in C++
- Capital Gain/Loss solution in Java
- Capital Gain/Loss solution in Python
- Additional Resources

Problem Statement of Capital Gain/Loss
Table: Stocks
+—————+———+
| Column Name | Type |
+—————+———+
| stock_name | varchar |
| operation | enum |
| operation_day | int |
| price | int |
+—————+———+
(stock_name, operation_day) is the primary key (combination of columns with unique values) for this table.
The operation column is an ENUM (category) of type (‘Sell’, ‘Buy’)
Each row of this table indicates that the stock which has stock_name had an operation on the day operation_day with the price.
It is guaranteed that each ‘Sell’ operation for a stock has a corresponding ‘Buy’ operation in a previous day. It is also guaranteed that each ‘Buy’ operation for a stock has a corresponding ‘Sell’ operation in an upcoming day.
Write a solution to report the Capital gain/loss for each stock.
The Capital gain/loss of a stock is the total gain or loss after buying and selling the stock one or many times.
Return the result table in any order.
The result format is in the following example.
Example not found
Constraints not found
Complexity Analysis
- Time Complexity: Google AdSense
- Space Complexity: Google Analytics
1393. Capital Gain/Loss LeetCode Solution in C++
SELECT
stock_name,
SUM(IF(operation = 'Buy', -price, price)) AS capital_gain_loss
FROM Stocks
GROUP BY 1;
/* code provided by PROGIEZ */
1393. Capital Gain/Loss LeetCode Solution in Java
N/A
// code provided by PROGIEZ
1393. Capital Gain/Loss LeetCode Solution in Python
N/A
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.