Problem: Consider a non-empty input array (inarr) containing non-zero positive Integer. From input array, identify the unique pairs of integers such that each of the integers in the pair have the same sum of the digits. Print outnum, the number of unique pairs identified satisfying the criteria. If no such pair of integers can be identified print -1.
Input format: Read the array inarr (input array) with the elements separated by ‘,’ (comma)
Output format: Print outnum or -1 accordingly
Sample Input:
34,89,6,321,53,45,2211,81
Sample Output:
4
Explanation: All the possible combinations are: (6, 321), (6, 2211), (321, 2211), (45, 81)
First combination: 6, 3+2+1 = 6
Second combination: 6, 2+2+1+1 = 6
Third combination: 3+2+1 = 6, 2+2+1+1 = 6
Fourth combination: 4+5 = 9, 8+1 = 6
Hence the output will be: 4 (Output)
Solution: We strongly recommend you to try the problem first before moving to the solution.
Python
# Python code for Unique Pairs and Digit Sum
# www.codewindow.in
import math
list_num=input().split(',')
sum_dig_list=[]
for num in list_num:
sum_dig=sum(list(map(int, num)))
sum_dig_list.append(sum_dig)
max_sum=max(sum_dig_list)
list_digit_count=[0]*(max_sum+1)
for num in sum_dig_list:
list_digit_count[num] += 1
total_pair=0
for count in list_digit_count:
if count>1:
total_pair += int(math.factorial(count)/(math.factorial(count-2)*2))
if total_pair==0:
print('-1')
else:
print(total_pair)
Input:
34,89,6,321,53,45,2211,81
Output:
4
Follow Us
You Missed
- Dunzo Off-Campusing Drive | Data Engineer | codewindow.in
- Verbal Ability Questions Solved | Synonyms Antonyms – codewindow.in
- Verbal Ability Questions Solved | Statement Correction – codewindow.in
- Verbal Ability Questions Solved | Spotting Error – codewindow.in
- Verbal Ability Questions Solved | Speech and Tenses – codewindow.in
- Try
- Verbal Ability Questions Solved | Sentence Arrangement – codewindow.in
- Verbal Ability Questions Solved | Prepositions – codewindow.in
- Verbal Ability Questions Solved | Grammar – codewindow.in
- Verbal Ability Questions Solved | Articles – codewindow.in
- Zoho Off Campus Drive | Product Marketing Associate / Specialist | codewindow.in
- BerryWorks Off Campus Recruitment Drive – Software Engineer – Codewindow.in
- Microsoft Off Campus Hiring Drive – Technical Support – Codewindow.in
- Infosys Pseudocede & Puzzle Solved – codewindow.in
- Infosys Verbal Questions Solved – codewindow.in
- Amazon Off Campus Drive | Trainee | codewindow.in
- Hawkins Off Campus Drive | Management trainees | codewindow.in
- Infosys Mathematical Questions Solved – codewindow.in
- Paypal Off Campus Recruitment Drive – Software Engineer – Codewindow.in
- IBM Off Campus Hiring Drive – Associate Systems Engineer – Codewindow.in
- Infosys Off Campus Hiring Drive – SP and DSE – Codewindow.in
- VIRTUSA Recruitment 2022 | Associate Engineer | codewindow.in
- Infosys Logical Questions solved – codewindow.in
- Google Off Campus Hiring Drive – IT Support Engineer – Codewindow.in
- Capgemini Pseudocode Solved | Set 4 – codewindow.in
- Capgemini Pseudocode Solved | Set 3 – codewindow.in
- Capgemini Pseudocode Solved | Set 2 – codewindow.in
- JECA 2022 Question Paper – 90+ Sample Questions Exposed – codewindow.in
- Capgemini Pseudocode Solved | Set 1 – codewindow.in
- Verizon Off Campusing | Software Developer | codewindow.in
- TCS Recruitment Off-Campusing | Service Desk Analyst | codwindow.in
- Amdocs Off-Campusing | Devops Engineer | codewindow.in