text: Sum of two numbers where one number is represented as array of digits | C # implementation of the approach ; Function to return the vector containing the answer ; ArrayList v is to store each digits sum and ArrayList ans is to store the answer ; No carry in the beginning ; Start loop from the end and take element one by one ; Array index and last digit of number ; Maintain carry of summation ; Push the digit value into the array ; K value is greater then 0 ; Push digits of K one by one in the array ; Also maintain carry with summation ; Reverse the elements of vector v and store it in vector ans ; Driver code ; Print the answer
code: using System ; using System . Collections ; class GFG { static ArrayList addToArrayForm ( ArrayList A , int K ) { ArrayList v = new ArrayList ( ) ; ArrayList ans = new ArrayList ( ) ; int rem = 0 ; int i = 0 ; for ( i = A . Count - 1 ; i >= 0 ; i -- ) { int my = ( int ) A [ i ] + K % 10 + rem ; if ( my > 9 ) { rem = 1 ; v . Add ( my % 10 ) ; } else { v . Add ( my ) ; rem = 0 ; } K = K / 10 ; } while ( K > 0 ) { int my = K % 10 + rem ; v . Add ( my % 10 ) ; if ( my / 10 > 0 ) rem = 1 ; else rem = 0 ; K = K / 10 ; } if ( rem > 0 ) v . Add ( rem ) ; for ( int j = v . Count - 1 ; j >= 0 ; j -- ) ans . Add ( ( int ) v [ j ] ) ; return ans ; } static void Main ( ) { ArrayList A = new ArrayList ( ) ; A . Add ( 2 ) ; A . Add ( 7 ) ; A . Add ( 4 ) ; int K = 181 ; ArrayList ans = addToArrayForm ( A , K ) ; for ( int i = 0 ; i < ans . Count ; i ++ ) Console . Write ( ( int ) ans [ i ] ) ; } }