Python - Maximum Pair Summation in numeric String
Last Updated :
10 Apr, 2023
Sometimes, we might have a problem in which we require to get the maximum summation of 2 numbers from Strings but with a constraint of having the numbers in successions. This type of problem can occur while competitive programming. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using max() + zip() + list comprehension This problem can be solved using the combination of above three function in which max function can be used to get the max value, zip and list comprehension doing the task of extending the logic to the whole list.
Step-by-step approach:
- Initialize the test_string variable with a string of digits.
- Print the original string using the print() function.
- Convert the test_string to a list of individual digits using the list() function.
- Use list comprehension with zip() function to iterate over the test_string and its sliced version (with one position shift), such that each element of the sliced string is paired with the element in the corresponding position in the original string.
- In each iteration, convert the two digits to integers, add them and return the sum.
- Use the max() function to get the maximum sum of consecutive pairs of digits obtained in step 4.
- Print the result obtained in step 6 using the print() function.
Below is the implementation of the above approach:
Python3
# Python3 code to demonstrate
# Maximum Pair Summation in String
# using zip() + max() + list comprehension
# initializing string
test_string = '6543452345456987653234'
# printing original string
print("The original string : " + str(test_string))
# using zip() + max() + list comprehension
# Maximum Pair Summation in String
test_string = list(test_string)
res = max(int(a) + int(b) for a, b in zip(test_string, test_string[1:]))
# print result
print("The maximum consecutive sum is : " + str(res))
Output : The original string : 6543452345456987653234
The maximum consecutive sum is : 17
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using max() + map() + operator.add The above problem can also be solved using yet another combination of functions. In this combination, map functions performs the task of extending the logic to whole list and add operator is used to perform the addition.
Python3
# Python3 code to demonstrate
# Maximum Pair Summation in String
# using max() + map() + operator.add
from operator import add
# initializing string
test_string = '6543452345456987653234'
# printing original string
print("The original string : " + str(test_string))
# using max() + map() + operator.add
# Maximum Pair Summation in String
res = max(map(add, map(int, test_string), map(int, test_string[1:])))
# print result
print("The maximum consecutive sum is : " + str(res))
Output : The original string : 6543452345456987653234
The maximum consecutive sum is : 17
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 3: "Maximum Pair Summation in String using iteration".
One approach to solve the problem of finding the maximum consecutive sum in a string can be done by iterating over the string and comparing the current sum with the maximum sum seen so far.
Step-by-step approach:
- Initialize two variables res and cur_sum to zero.
- Iterate over the string using a for loop and range(n-1) to access consecutive pairs of characters in the string.
- Convert the consecutive pairs of characters to integers using int() function and add them to get the current sum.
- Find the maximum of the current sum and the previous maximum sum seen so far using max() function and update the result variable res.
- After the loop, the value of res variable will hold the maximum consecutive sum in the string.
- Print the maximum consecutive sum using the print() function.
Python3
# Python3 code to demonstrate
# Maximum Pair Summation in String
# using iteration
# initializing string
test_string = '6543452345456987653234'
# printing original string
print("The original string : " + str(test_string))
# using iteration
n = len(test_string)
res = 0
cur_sum = 0
for i in range(n-1):
cur_sum = int(test_string[i]) + int(test_string[i+1])
res = max(res, cur_sum)
# print result
print("The maximum consecutive sum is : " + str(res))
OutputThe original string : 6543452345456987653234
The maximum consecutive sum is : 17
Time complexity: O(n), where n is the length of the input string
Auxiliary space: O(1) - constant space
Method 4: Using a while loop
Step-by-step approach:
- Compute the length of the string using the len() function and store it in the variable n.
- Initialize the variables res, cur_sum, and i to 0.
- Enter a while loop that continues as long as i is less than n-1.
- Within the loop, compute the current pair summation by adding the current character and the next character together using the int() function to convert them to integers.
- Update the maximum sum so far using the max() function to compare res and cur_sum.
- Increment i by 1 to move on to the next pair of characters.
- When the loop finishes, print the maximum sum using the print() function and string concatenation operator +.
Python3
# Python3 code to demonstrate
# Maximum Pair Summation in String
# using a while loop
# initializing string
test_string = '6543452345456987653234'
# printing original string
print("The original string : " + str(test_string))
# using a while loop
n = len(test_string)
res = 0
cur_sum = 0
i = 0
while i < n-1:
cur_sum = int(test_string[i]) + int(test_string[i+1])
res = max(res, cur_sum)
i += 1
# print result
print("The maximum consecutive sum is : " + str(res))
OutputThe original string : 6543452345456987653234
The maximum consecutive sum is : 17
Time complexity: O(n), where n is the length of the input string, since we need to iterate over the string once to compute the maximum pair summation.
Auxiliary space: O(1), as we are only using a constant amount of extra space for storing the variables (res, cur_sum, and i). This means that the space required by the algorithm does not depend on the size of the input string.
Method 5: Using list comprehension and slicing
This method involves using a list comprehension and slicing to iterate through the string and calculate the maximum consecutive sum.
Step-by-step approach:
- Initialize the string
- Use list comprehension and slicing to create a list of consecutive pairs of characters in the string.
- Use a lambda function and map to convert each pair of characters to their sum.
- Use the built-in max() function to find the maximum consecutive sum in the list.
- Print the result.
Python3
# Python3 code to demonstrate
# Maximum Pair Summation in String
# using list comprehension and slicing
# initializing string
test_string = '6543452345456987653234'
# printing original string
print("The original string : " + str(test_string))
# using list comprehension and slicing
pairs = [test_string[i:i+2] for i in range(len(test_string)-1)]
pair_sums = list(map(lambda x: int(x[0])+int(x[1]), pairs))
res = max(pair_sums)
# print result
print("The maximum consecutive sum is : " + str(res))
OutputThe original string : 6543452345456987653234
The maximum consecutive sum is : 17
Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n), as a list of length n/2 is created.
Similar Reads
Python - Get summation of numbers in string list
Sometimes, while working with data, we can have a problem in which we receive series of lists with data in string format, which we wish to accumulate as list. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop + int() This is the brute force method to perform this
3 min read
Python | Embedded Numbers Summation in String List
Sometimes, while working with Python lists, we can have problem in which we need to concatenate embedded numbers in Strings list and perform its summation. This can have application in domains dealing with data. Lets discuss certain ways in which this task can be performed. Method #1 : Using join()
6 min read
Python - Maximum Quotient Pair in List
Sometimes, we need to find the specific problem of getting the pair which yields the maximum Quotient, this can be solved by sorting and getting the first and last elements of the list. But in some case, we donât with to change the ordering of list and perform some operation in a similar list withou
5 min read
Python | Strings length summation
Sometimes we receive data in the container that we need to process to handle it further for some essential utility. The magnitude of amount of data sometimes becomes important and needs to be known. This article discusses the total length of list of strings. Let's discuss certain ways in which this
5 min read
Python Program for Number of pairs with maximum sum
Write a python program for a given array arr[], count number of pairs arr[i], arr[j] such that arr[i] + arr[j] is maximum and i < j. Example: Input : arr[] = {1, 1, 1, 2, 2, 2}Output: 3Explanation: The maximum possible pair sum where i<j is 4, which is given by 3 pairs, so the answer is 3 the
3 min read
Python Program Maximum of Three Number using Lambda
Here we will create a lambda function to find the maximum among three numbers in Python. Example: Input: a=10, b=24, c=15Output: 24 # using LambdaFinding Maximum Among Three Numbers in PythonBelow are some of the ways by which we can find the maximum among three numbers in Python. Using lambda with
3 min read
Python | Pair summation of list elements
Sometimes, while working with Python list, one can have a problem in which one needs to find perform the summation of list in pair form. This is useful as a subproblem solution of bigger problem in web development and day-day programming. Let's discuss certain ways in which this problem can be solve
4 min read
Python - Maximum of String Integer list
Sometimes, while working with data, we can have a problem in which we receive a series of lists with data in string format, which we wish to find the max of each string list integer. Letâs discuss certain ways in which this task can be performed. Method #1 : Using loop + int() This is the brute forc
4 min read
Python | Maximum Difference in String
Sometimes, we might have a problem in which we require to get the maximum difference of 2 numbers from Strings but with a constraint of having the numbers in successions. This type of problem can occur while competitive programming. Letâs discuss certain ways in which this problem can be solved. Met
4 min read
Python - Maximum Scoring word
Given a String, the task is to write a Python program to compute maximum scoring words i.e words made of characters with a maximum positional summation. Examples: Input : test_str = 'geeks must use geeksforgeeks for cs knowledge'Output : geeksforgeeksExplanation : Sum of characters positional values
5 min read