Skip to content

Commit e4207bf

Browse files
authored
Merge pull request Dijkstra-Edu#5 from SJ-Cipher/PlusOne
Solution Dijkstra-Edu#66 - SJ-Cipher/Edited - 02/02/2025 - @SJ-Cipher
2 parents 689737b + b6f5cf6 commit e4207bf

File tree

5 files changed

+68
-1
lines changed

5 files changed

+68
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Explanation:
2+
3+
The array/list 'digits' given to us is the digits of a number. Our objective is to be able to return the array with the value of the next highest integer i.e number+1. Now there are many methods which we can use here eg: We can convert the array to an integer and converting back it back to an array after incementing by 1. But that would require going through the array/list multiple times, once for conversion and again for incrementation. So let us try to perform operations on the list/array itself.
4+
5+
Now we have to be careful when we are operating on lists since it is easy to commit IndexErrors. So first let us start with a loop accessing the last element of the list. Now if this integer is less than 9 we can just simply increment it and return it. But if it is 9 we can simply change it to zero and continue the loop. Now it will check for the second last character and so on until it finds a digit less than 9 and return it. Now if all the integers are 9, the whole list will become 0 and loop will end. If that happens, we just insert a 1 at the beginning of the list and return the list.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
4+
//SOLUTION FUCTION
5+
int* plusOne(int* digits, int digitsSize, int* returnSize) {
6+
int i=0;
7+
for(i=digitsSize-1;i>=0;i--){
8+
if(digits[i]<9){
9+
digits[i]+=1;
10+
break;
11+
}
12+
digits[i]=0;
13+
}
14+
if(i==-1){
15+
int *ret_arr=malloc(sizeof(int)*(digitsSize+1));
16+
*returnSize = digitsSize+1;
17+
ret_arr[0]=1;
18+
for(int i=1;i<*returnSize;i++){
19+
ret_arr[i]=digits[i-1];
20+
}
21+
return ret_arr;
22+
}
23+
else{
24+
int *ret_arr = malloc(sizeof(int)*digitsSize);
25+
*returnSize=digitsSize;
26+
for(int i=0;i<digitsSize;i++){
27+
ret_arr[i]=digits[i];
28+
}
29+
return ret_arr;
30+
}
31+
32+
}
33+
34+
int main(){
35+
int arr[]={9,9,9};
36+
int size=sizeof(arr)/sizeof(arr[0]);
37+
int returnSize;
38+
int *ret_arr=plusOne(arr,size,&returnSize);
39+
printf("[");
40+
for(int i=0;i<returnSize-1;i++){
41+
printf("%d,",ret_arr[i]);
42+
}
43+
printf("%d]",ret_arr[returnSize-1]);
44+
free(ret_arr);
45+
}

Arrays & Strings/L - Plus One.java renamed to Arrays & Strings/#66 - Plus One - Easy/Plus One - Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ public int[] plusOne(int[] digits) {
2323
Memory Usage: 42.6 MB
2424
2525
Simple and really efficient problem - O(n)
26-
*/
26+
*/
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import List
2+
3+
#SOLUTION
4+
class Solution:
5+
def plusOne(self, digits: List[int]) -> List[int]:
6+
for i in range(len(digits)-1,-1,-1): #iterating the list from the end to beginning
7+
if digits[i] < 9:
8+
digits[i]+=1
9+
return digits # if digit is less than 9 we add one and return
10+
else:
11+
digits[i]=0 #else make it zero and continue iterating
12+
return [1] + digits # if all are 0 (i.e all 9's), we add one at the start and return
13+
14+
if __name__ == "__main__":
15+
print(Solution().plusOne([9,9,9,9,9])) # Example input
16+
17+
#Time Complexity : O(n)
40.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)