Special Sum of Array Problem Statement
Given an array 'arr' containing single-digit integers, your task is to calculate the total sum of all its elements. However, the resulting sum must also be a single-digit number. To achieve this, repeatedly sum the digits of the result until a single-digit number is obtained.
Example:
Input:
[5, 8, 4, 9]
Output:
8
Explanation:
The sum of the elements is 26
. The sum of the digits 2 + 6 = 8
is a single-digit number.
Constraints:
1 <= T <= 100
1 <= N <= 10^3
0 <= arr[i] <= 9
- The sum of 'N' across all test cases does not exceed
10^5
.
Time Limit: 1 sec
.
Anonymous
17d
def num(n): if n < = 9 : return n while len(str(n)) > 1: string=str(n) sum=0 for i in range(len(string)): sum=sum+int(string[i]) if len(str(sum))==1: return sum else: return num(sum)
Help your peers!
Add answer anonymously...
Top Bajaj Finserv Data Scientist interview questions & answers
Popular interview questions of Data Scientist
Top HR questions asked in Bajaj Finserv Data Scientist
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app