✅ [JAVA] : Simple | O(max(n, logk)) | No Reverse | Efficient | Explained
Please UPVOTE if you find this post useful :)\n\nRefer to the following github repsitory for more leetcode solutions\nhttps://github.com/Akshaya-Amar/LeetCodeSo
# **Please UPVOTE if you find this post useful :)**\n\n<u><strong>Refer to the following github repsitory for more leetcode solutions<strong></u>\nhttps://github.com/Akshaya-Amar/LeetCodeSolutions\n\n* <u>**COMPLEXITY**</u>\n\t* **Time: O(max(n, log<sub>10</sub>(k)))**, where **n** is the **length of the array** and **log<sub>10</sub>(k)** is the **number of digits** present in variable `k`.\n\t* **Space: O(max(n, log<sub>10</sub>(k)))**, not an in-place as we need space equal to the given k or length of array, whichever is maximum between the two, to store the elements.\n\n* <u>**BASIC IDEA**</u>\nBasic Idea behind this implementation is to add the num array element one by one with the k\neg:\n`num[] = {1, 2, 3}` and `k = 45;`\n\n\t* **1<sup>st</sup> Iteration:**\n`k += num[len--]` --> k = k + num[2] --> k = 45 + 3 --> k = 48\n`list.addFirst(k % 10)` --> list.addFirst(48 % 10) --> list.addFirst(8) --> [8]  \n`k /= 10` --> k = k / 10 --> 48 / 10 --> k = 4\n\n\t* **2<sup>nd</sup> Iteration:**\n`k += num[len--]` --> k = k + num[1] --> k = 4 + 2 --> k = 6\n`list.addFirst(k % 10)` --> list.addFirst(6 % 10) --> list.addFirst(6) --> [6, 8]\n`k /= 10` --> k = k / 10 --> 6 / 10 --> k = 0\n\n\t* **3<sup>rd</sup> Iteration:**\n`k += num[len--]` --> k = k + num[0] --> k = 0 + 1 --> k = 1\n`list.addFirst(k % 10)` --> list.addFirst(1 % 10) --> list.addFirst(1) --> [1, 6, 8]  **<--- Desired Output**\n`k /= 10` --> k = k / 10 --> 1 / 10 --> k = 0\n\n\t**Here, the loop will be executed 3 times as (length of num) > (number of digits in k) i.e. O(max(n, log<sub>10</sub>(k))), where n is the length of the array and log<sub>10</sub>(k) is the number of digits**\n\n<iframe src="https://leetcode.com/playground/UiX43koL/shared" frameBorder="0" width="100%" height="400"></iframe>\n\n<u><strong>Refer to the following github repsitory for more leetcode solutions<strong></u>\nhttps://github.com/Akshaya-Amar/LeetCodeSolutions\n\n# **Please UPVOTE if you find this post useful :)**
['Java']