Sum of all numbers in an integer

It is a common interview question that is frequently asked.
In this the interviewer is looking for some basics like iterating the loops etc.

If given integer is 987 then the output of the program should return 24.
Let's look at its algorithm, dry run, implementation, and complexity.

Algorithm:
  1. Step 1: Declare a variable “a” and initialize it with the given value ‘987’.
    1. a = 987;
  2. Step 2: Assign the integer value to a temporary variable for further operation on it. 
    1. temp = 987;
  3. Step 3: Declare a variable “sum” to assign the final sum and initialize it with 0(zero). 
    1. sum = 0;
  4. Step 4: Using while loop, Iterate the next two steps until the variable temp becomes 0(zero).
  5. Step 5: Get the rightmost digit of the number with the modulus/remainder ‘%’ operator. Add the rightmost digit to sum variable.
  6. Step 6: Divide the number by 10 with ‘/’ operator.
  7. Step 7: Print the sum value ‘24’.

 Dry Run:
Iteration 1: Start the loop
temp = 987; temp != 0 -> Condition true
sum = 0 + (987 % 10); (step 5: sum = sum + temp % 10)
sum = 0 + 7;
sum = 7;
temp = 987 / 10; (step 6: temp = temp / 10)
temp = 98;
Iteration 2:
temp = 98; temp != 0 -> Condition true
sum = 7 + (98 % 10);
sum = 7 + 8;
sum = 15;
temp = 98 / 10;
temp = 9;
Iteration 3:
temp = 9; temp != 0 -> Condition true
sum = 15 + (9 % 10);
sum = 15 + 9;
sum = 24;
temp = 9 / 10;
temp = 0;
Iteration 4:
temp = 0; temp !=0 -> Condition false.
Terminate the loop.

Sample output: 
Sum = 24



Implementation:
#include <iostream>

#include <conio.h>

int main()
{
int a = 987;
int temp = a;
int sum = 0;
int temp2 = 0;


while (temp != 0)
{
sum = sum + temp % 10;
temp2 = sum;
temp = temp / 10;
}


std::cout << "Sum:" << sum << std::endl;
getch();
return 0;

}

Algorithm complexity
O(log n)


Find if subsum is zero

Algorithm:


Step 1: Declare and initialize an array ‘a’ with the given values 1, -2, -3, -4, -5, 1.
Step 2: Declare a variable ‘subsum’ and initialize it to 0.
Step 3: Using for loop, iterate the array elements.
For each element repeat the next two steps.
Step 4: Add array elements to subsum one per the iteration.
Step 5: Check if subsum is equal to zero. If true, then output “Zero subsum found”.
Step 6: Otherwise continue the loop(go to step 4) and return the function.

Dry Run:


   0        1 2        3 4 5   
1
-2
-3
-4
-5
1



Iteration 1:  Start the loop
i=0 and 0<6 -> condition true
  0       1 2        3 4 5   
1
-2
-3
-4
-5
1


a[0] = 1;   
subsum = 0 + 1; (step 4: subsum += a[i])
subsum = 1;
If 1 == 0 -> condition false. Continue the loop. (step 5: subsum==0)
i = 1; (incremented by one)


Iteration 2: 
i=1 and 1<6 -> condition true
  0       1 2        3 4 5   
1
-2
-3
-4
-5
1


a[1] = -2;
subsum = 1 + (-2); (step 4: subsum += a[i])
subsum = -1;
If -1 == 0 -> condition false. Continue the loop. (step 5: subsum==0)
i = 2; (incremented by one)


Iteration 3:
i=2 and 2<6 -> condition true
  0       1 2        3 4 5   
1
-2
-3
-4
-5
1


a[2] = -3;
subsum = -1 + (-3); (step 4: subsum += a[i])
subsum = -4;
If -4 == 0 -> condition false. Continue the loop. (step 5: subsum==0)
i = 3; (incremented by one)
Iteration 4:
i=3 and 3<6 -> condition true
  0       1 2        3 4 5   
1
-2
-3
-4
-5
1


a[3] = -4
subsum = (-4) + (-4); (step 4: subsum += a[i])
subsum = -8;
If -8 == 0 -> condition false. Continue the loop. (step 5: subsum==0)
i = 4; (incremented by one)


Iteration 5:
i=4 and 4<6 -> condition true
  0       1 2        3 4 5   
1
-2
-3
-4
-5
1


a[4] = -5;
subsum = (-8) + (-5); (step 4: subsum += a[i])
subsum = -13;
If -13 == 0 -> condition false. Continue the loop. (step 5: subsum==0)
i = 5; (incremented by one)


Iteration 6:
i=5 and 5<6 -> condition true
  0       1 2        3 4 5   
1
-2
-3
-4
-5
1


a[5] = 1;
subsum = (-13) + 1; (step 4: subsum += a[i])
subsum = -12;
If -12 == 0 -> condition false. Continue the loop. (step 5: subsum==0)
i = 6; (incremented by one)


Iteration 7:
i=6 and 6<6 -> condition false
Terminate the loop.


Implementation:
#include <iostream>
#include <conio.h>
#include <stdio.h>

using namespace std;

int main()
{
int a[] = {1,-2,-3,-4,-5,1}, subsum=0;

for(int i=0;i<6;i++)
{
subsum += a[i];

if(subsum == 0)
cout<<"Zero subsum found";
}
getch();
return 0;
}


Algorithm complexity
O(n) here, n=6 so O(6)

NoSQL

NoSQL Data models: key-value  Aggregate model.  key or id is used to get the data.  Lookup is based on the key.   document Aggre...