Find the sum of digits of the smallest number in an array


Given an array of integers, find the sum of digits of the smallest number in the array.


Assumptions


- array.length >= 1

- Array is unsorted

- Array can contain duplicate numbers


Example


Input: [20,15,92,44,105]

Output: 6

Explanation: Smallest number in array is 15. Sum of digits is 1+5 = 6


Solution 1


1. Define an int variable - min - to store the smallest number in the array of numbers.

2. Traverse Array and for each element - if min is greater than element, set min to element.

4. After smallest number - min - is identified, determine the sum of digits of min - by continuously dividing min by 10, and the summing the remainder modulus 10.


Code



Complexity


Time Complexity O(N) - Since we are traversing the array once to determine the smallest number the time complexity is O(N).

Space Complexity O(1) - No copies of the array elements are needed, hence extra space is not needed, so space complexity is O(1).

 
Subscribe to our Newsletter

 
 
RECOMMENDED RESOURCES
Behaviorial Interview
Top resource to prepare for behaviorial and situational interview questions.

STAR Interview Example