Add 1 to a Number in Array Form

🚀 Day 8 / 50 – Wild Coding Kickoff Contest Today’s problem: Plus One (LeetCode #66) Problem Summary: You’re given a number in the form of an array. Each element represents a digit. The goal is simple — add 1 to the number and return the updated array. Example 1: Input: digits = [1,2,3] Output: [1,2,4] Explanation: The array represents the integer 123. Incrementing by one gives 123 + 1 = 124. Thus, the result should be [1,2,4]. Example 2: Input: digits = [9] Output: [1,0] Explanation: The array represents the integer 9. Incrementing by one gives 9 + 1 = 10. Thus, the result should be [1,0]. Approach (from code): Traverse the array from right to left. If the current digit is less than 9, simply increment it and return the array. If the digit is 9, convert it to 0 and continue (carry forward). If all digits become 0 (like 9 → 10, 99 → 100), create a new array with size +1 and set the first element as 1. #Coding #Programming #Developer #SoftwareDeveloper #Tech #CodeNewbie #100DaysOfCode #50DaysOfCode #DataStructures #Algorithms #DSA #LeetCode #CodingInterview #InterviewPreparation #ProblemSolving

  • text

To view or add a comment, sign in

Explore content categories