Final Value of Variable After Performing Operations
EasyPrompt
There is a programming language with only four operations and one variable X:
++XandX++increments the value of the variableXby1.--XandX--decrements the value of the variableXby1.
Initially, the value of X is 0.
Given an array of strings operations containing a list of operations, return the final value of X after performing all the operations.
Example 1:
Input: operations = ["--X","X++","X++"]
Output: 1
Explanation: The operations are performed as follows:
Initially, X = 0.
--X: X is decremented by 1, X = 0 - 1 = -1.
X++: X is incremented by 1, X = -1 + 1 = 0.
X++: X is incremented by 1, X = 0 + 1 = 1.Example 2:
Input: operations = ["++X","++X","X++"]
Output: 3
Explanation: The operations are performed as follows:
Initially, X = 0.
++X: X is incremented by 1, X = 0 + 1 = 1.
++X: X is incremented by 1, X = 1 + 1 = 2.
X++: X is incremented by 1, X = 2 + 1 = 3.Example 3:
Input: operations = ["X++","++X","--X","X--"]
Output: 0
Explanation: The operations are performed as follows:
Initially, X = 0.
X++: X is incremented by 1, X = 0 + 1 = 1.
++X: X is incremented by 1, X = 1 + 1 = 2.
--X: X is decremented by 1, X = 2 - 1 = 1.
X--: X is decremented by 1, X = 1 - 1 = 0.
Constraints:
1 <= operations.length <= 100operations[i]will be either"++X","X++","--X", or"X--".
Approaches
2 approaches with complexity analysis and trade-offs.
This is a straightforward approach where we iterate through the array of operations. For each operation, we use full string comparison to determine whether to increment or decrement the variable X.
Algorithm
- Initialize an integer variable,
x, to 0. - Iterate through the
operationsarray from the first to the last element. - For each
operationstring:- If the string is equal to
"++X"or"X++", incrementxby 1. - Otherwise, it must be a decrement operation (
"--X"or"X--"), so decrementxby 1.
- If the string is equal to
- After the loop completes, return the final value of
x.
Walkthrough
The core idea is to simulate the process described in the problem. We start with a variable X initialized to 0 and process each operation one by one. We use if-else conditions or a switch statement to match the operation string and update X accordingly.
Here is a code snippet demonstrating this approach:
class Solution { public int finalValueAfterOperations(String[] operations) { int x = 0; for (String op : operations) { if (op.equals("++X") || op.equals("X++")) { x++; } else { x--; } } return x; }}A switch statement can also be used for clarity:
class Solution { public int finalValueAfterOperations(String[] operations) { int x = 0; for (String op : operations) { switch (op) { case "++X": case "X++": x++; break; case "--X": case "X--": x--; break; } } return x; }}Complexity
Time
O(N), where N is the number of operations. We iterate through the array once. Although string comparison takes time proportional to the string length, the length is constant (3) in this problem, making each comparison an O(1) operation.
Space
O(1), as we only use a single integer variable to store the current value of X, requiring constant extra space regardless of the input size.
Trade-offs
Pros
Easy to understand and implement.
Directly follows the problem statement, making the logic clear.
Cons
Slightly less performant in practice compared to checking a single character due to the overhead of
String.equals()method calls which compare all characters in the string.
Solutions
Solution
class Solution {public int finalValueAfterOperations(String[] operations) { int ans = 0; for (var s : operations) { ans += (s.charAt(1) == '+' ? 1 : -1); } return ans; }}Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.