Plus One | Practice | GeeksforGeeks

Given a non-negative number represented as a list of digits, add 1 to the number (increment the number represented by the digits). The digits are stored such that the most significant digit is first element of array. 
 
Example 1:
Input: 
N = 3
arr[] = {1, 2, 4}
Output: 
1 2 5
Explanation:
124+1 = 125, and so the Output
Example 2:
Input: 
N = 3
arr[] = {9,9,9}
Output: 
1 0 0 0
Explanation:
999+1 = 1000, and so the output

Your Task:
You don't need to read input or print anything. You only need to complete the function increment() that takes an integer N, and an array arr of size N as input and returns a list of integers denoting the new number which we get after adding one to the number denoted by the array arr.

Expected Time Complexity:  O(N)
Expected Auxilliary Space: O(1)
 
Constraints:
1 <= N <= 105
0 <= arri <= 9
0
singhsourav02154 days ago
//Solution in java
    static ArrayList<Integer> increment(ArrayList<Integer> arr , int N) {
 	int i = N-1;
		if(arr.get(arr.size()-1) < 9) {
			arr.set(arr.size()-1, arr.get(N-1)+1);
			return arr;
		}
		else {
			while(i>=0 && arr.get(i) == 9) {
				arr.set(i, 0);
				if(i == 0)
					arr.add(0,1);
				i--;
				
			}
		}
		//System.out.println(i);
		if(i>-1)
			arr.set(i, arr.get(i)+1);
		return arr;

    }
+1
mohittpattel2 weeks ago
class Solution {
  public:
    vector<int> increment(vector<int> arr ,int N) {
        // code here
        //Copying the whole array ot the ans
        vector<int> ans(arr);
        
        for(int i = N-1; i >= 0; i--)
        {
            if(ans[i] == 9)
                ans[i] = 0;
            else
            {
                ans[i]++;
                return ans;
            }
        }
        
        ans[0] = 1;
        ans.push_back(0);
        
        return ans;
    }
};
0
joyrockok2 weeks ago
class Solution {   static ArrayList<Integer> increment(ArrayList<Integer> arr , int N) {       // code here ArrayList<Integer> subRslt = new ArrayList<Integer>(); ArrayList<Integer> result = new ArrayList<Integer>();  boolean olim = false;  for(int i=N-1; i>=0; i--) {  int value = arr.get(i);    if(i==N-1) {   value += 1;  } else if(olim) {   value += 1;  }     if(value >= 10) {       if(i==0) {           subRslt.add(value%10);           subRslt.add(1);       } else {           subRslt.add(value%10);           olim = true;       }   } else {        subRslt.add(value);        olim = false;   } }  for(int i=subRslt.size()-1; i>=0; i--) {  result.add(subRslt.get(i)); }  return result;   }};
+1
sagrikasoni3 weeks ago
EASY JAVA Solution using Array List .set function 

class Solution {
    static ArrayList<Integer> increment(ArrayList<Integer> arr , int N) {
       
     int j = arr.size()-1; boolean b = true;
     while(j>= 0 && b == true ){
         if(j== 0 && arr.get(j)==9){
            arr.set(j,1);
             arr.add(0);
             j--;
         }
         else if(arr.get(j)==9){
            arr.set(j,0);
             j--;
         }
         else {
               int temp = arr.get(j)+1;
               arr. set(j,temp);

               b = false;
         }
        
     }
      return arr;
}
}
 
+1
eswar99933 weeks ago
Python Solution:

    def increment(self, arr, N):
       # code here
       str1=""
       if arr[-1]!=9:
           arr[-1]=arr[-1]+1
           return arr
       else:
           for i in arr:
               str1+=str(i)
           res=int(str1)+1
           return str(res)
+1
swapniltayal4221 month ago
class Solution { public:   vector<int> increment(vector<int> arr ,int N) {       // code here       int carry = 1;       for (int i=N-1; i>-1; i--){           int s = arr[i] + carry;           arr[i] = (s)%10;           if (s > 10){               carry = arr[i] % 10;           }else if (s == 10){               carry = 1;           }else{               carry = 0;           }       }      if(arr[0]==0){          arr.push_back(0);          arr[0]=1;      }      return arr;   }};
0
pacificngu11 month ago
  vector<int> increment(vector<int> arr ,int N) {       vector<int> number;       int carry=0;     for(int i=N-1;i>=0;i--)     {
         if(arr[i]==9&&i==N-1&&N!=1)         {             number.push_back(0);             carry=1;         }       else if(arr[i]==9&&N==1)         {            number.push_back(0);             number.push_back(1);         }         else if(arr[i]==9&&carry==1)         {          number.push_back(0);          carry=1;         }         else if(i==N-1&&arr[i]!=9)         {          number.push_back(arr[i]+1);          carry=0;         }         else if(carry==1)         {             number.push_back(arr[i]+1);             carry=0;         }         else         {             number.push_back(arr[i]);         }              }     if(carry==1)     {         number.push_back(1);     }    reverse(number.begin(),number.end());  return number;   }
0
nandasobhan1 month ago
can anyone tell why my code is not running?
 
class Solution { public:   vector<int> increment(vector<int> arr ,int N) {       // code here       int no=0;       for(int i=0;i<N;i++){           no=no*10+arr[i];       }       no=no+1;       int p=N+1;        vector<int> v(p);      for(int i=p-1;i>=0;i--){                       v.push_back(no%10);          no=no/10;      }           arr=v;             return arr ;    }   };
+1
abera25831 month ago
 vector<int> increment(vector<int> arr ,int N) {       // code here       int n=arr.size();       for(int i=(n-1);i>=0;i--){           if(arr[i]!=9){               arr[i]=arr[i]+1;               break;           }           else{               arr[i]=0;           }       }       if(arr[0]==0){           arr.push_back(0);           arr[0]=1;       }       return arr;          }
0
raghav_maheshwari1 month ago
A very simple logic 
class Solution {
  public:
    vector<int> increment(vector<int> a ,int N) {
        a[N-1] +=1;
        int carry =0 ;
        if(a[N-1] > 9) {
            a[N-1]%= 10;
            ++carry;
        }
        for(int i = N-2 ; i>=0 ; --i) {
            if(a[i]+carry > 9) {
                a[i] = (a[i] + carry)%10;
                carry = 1;
            }
            else {
                a[i] = a[i] + carry;
                carry = 0;
                break;
            }
        }
        if(carry) {
            a.insert(a.begin() , 1);
        }
        return a;
    }
};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.