How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focusโor scroll down to preview all options.
Visualize the algorithm step-by-step with interactive animations in real time.
Read the full explanation, examples, and starter code at your own pace.
Drag and arrange the algorithm steps in the correct execution order.
๐ง Select Active to activate
Follow every state change, comparison, and transformation as the execution unfolds in real time.
๐ Select Passive to activate
Difficulty: ๐ข Easy
Topics: Arrays, Math
You are given a large integer represented as an array of digits called digits.
digits[i] represents a single digit.Increment the number by one and return the resulting array of digits.
Input: [1, 2, 3]
Output: [1, 2, 4]
Explanation:
The array represents the number 123.
After incrementing: 123 + 1 = 124
Input: [4, 3, 2, 1]
Output: [4, 3, 2, 2]
Explanation:
The array represents the number 4321.
After incrementing: 4321 + 1 = 4322
Input: [9]
Output: [1, 0]
Explanation:
The array represents the number 9.
After incrementing: 9 + 1 = 10
1 โค digits.length โค 1000 โค digits[i] โค 9โจ Tip:
Think about how carry works when the last digit is 9!
public class Main {
public static int[] plusOne(int[] digits) {
int n = digits.length;
// Traverse from last digit
for (int i = n - 1; i >= 0; i--) {
if (digits[i] < 9) {
digits[i]++; // simple increment
return digits; // done
}
digits[i] = 0; // carry forward
}
// If all digits were 9 (e.g., 9, 99, 999)
int[] result = new int[n + 1];
result[0] = 1; // leading 1
return result;
}
public static void main(String[] args) {
int[] digits = {9, 9, 9};
int[] result = plusOne(digits);
// Print result
for (int digit : result) {
System.out.print(digit + " ");
}
}
}
โ Written by Saurabh Patil โข B.Tech CSE โข Software Developer๐ฏ Select Challenge to activate
Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.
The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.
Understand Below AlgorithmGreen text means the instruction is placed in the correct position.
Red text means the instruction is in the wrong position.
Instructions with the same background color indicate particular blocks start and end.
A tick mark means the instruction is correct and locked.
๐ Locked steps cannot be moved. Only unlocked steps are draggable.
๐ Enable sound for swap feedback and completion effects.