- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find sum of concatenated pairs of all each element in a list in Python?n
Suppose we have a list of numbers called nums. We have to find the sum of every concatenation of every pair of numbers in nums. Here the pair (i, j) and pair (j, i) are considered different.
So, if the input is like nums = [5, 3], then the output will be 176, as We have the following concatenations: (nums[0] + nums[0]) = (5 concat 5) = 55, (nums[0] + nums[1]) = (5 concat 3) = 53, (nums[1] + nums[0]) = (3 concat 5) = 35, (nums[0] + nums[0]) = (3 concat 3) = 33, then the sum is 55 + 53 + 35 + 33 = 176
To solve this, we will follow these steps:
memo := a new map nums1 := nums temp := 0 c := sum of all elements in nums1 a := size of nums for i in range 0 to a, do if nums[i] is same as 0, then temp := temp + c otherwise, if nums[i] is present in memo, then temp := temp + memo[nums[i]] otherwise, b := 0 for j in range 0 to a, do b := b + integer of (nums[i] concatenate nums1[j]) memo[nums[i]] := b temp := temp + memo[nums[i]] return temp
Let us see the following implementation to get better understanding:
Example
class Solution: def solve(self, nums): memo = {} nums1 = nums temp = 0 c = sum(nums1) a = len(nums) for i in range(a): if nums[i] == 0: temp += c else: if nums[i] in memo: temp += memo[nums[i]] else: b = 0 for j in range(a): b += int(str(nums[i]) + str(nums1[j])) memo[nums[i]] = b temp += memo[nums[i]] return temp ob = Solution() nums = [5, 3] print(ob.solve(nums))
Input
[5, 3]
Output
176
- Related Articles
- Program to find sum of concatenated pairs of all each element in a list in Python?
- Python program to find sum of absolute difference between all pairs in a list
- Program to find XOR sum of all pairs bitwise AND in Python
- Python Program to find the cube of each list element
- Program to find sum of the minimums of each sublist from a list in Python
- Program to find sum of widths of all subsequences of list of numbers in Python
- Program to find replicated list by replicating each element n times
- Program to find max number of K-sum pairs in Python
- Python program to find sum of elements in list
- Program to find kpr sum for all queries for a given list of numbers in Python
- Python program to find the group sum till each K in a list
- Program to find sum of all elements of a tree in Python
- Program to find length of concatenated string of unique characters in Python?
- Program to Find K-Largest Sum Pairs in Python
- Python program to find Cumulative sum of a list
- Python program to get the indices of each element of one list in another list

Advertisements