Given a number N. You can perform an operation on it multiple times, in which you can change digit 5 to 6 and vice versa.
You have to return the sum of the maximum number and the minimum number which can be obtained by performing such operations.
Example 1:
Input: N = 35
Output: 71
Explanation: The maximum number which can be
formed is 36 and the minimum number which can
be formed is 35 itself. 
Example 2:
Input: N = 22
Output: 44
Explanation: The maximum number and minimum
number which can be formed is 22 itself.
Your Task:
You need not take any input or print anything. Your task is to complete the function performOperation() which takes a single Number as input and returns the sum of maximum and minimum number.
Expected Time Complexity: O(Log_{10}N)
Expected Auxiliary Space: O(Log_{10}N)
Constraints:
1 ≤ N ≤ 10^{18}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3

def performOperation(n):
    #code here

Solutions:
def performOperation(n):
	maxx = minn = str(n)
	while '5' in maxx:
		maxx = maxx.replace('5', '6')
	while '6' in minn:
		minn = minn.replace('6', '5')
	return int(maxx) + int(minn)


def performOperation(n):
	ste = str(n)
	if '5' not in ste and '6' not in ste:
		return int(n) * 2
	min_s = int(ste.replace('6', '5'))
	min_m = int(ste.replace('5', '6'))
	return min_s + min_m


def performOperation(n):
	N = str(n)
	a = ''
	b = ''
	for i in N:
		if i == '5':
			a += '6'
		else:
			a += i
		if i == '6':
			b += '5'
		else:
			b += i
	res = int(a) + int(b)
	return res


def performOperation(n):
	l = []
	a = n
	n = str(n)
	l[:0] = n
	b = 0
	for i in range(len(l)):
		if l[i] == '5':
			l[i] = '6'
		elif l[i] == '6':
			l[i] = '5'
		else:
			b = a
	b = int(''.join(l))
	return a + b


def performOperation(n):
	s = str(n)
	m = ''
	for i in s:
		if i == '5':
			m += '6'
		else:
			m += i
	mi = ''
	for i in s:
		if i == '6':
			mi += '5'
		else:
			mi += i
	return int(m) + int(mi)


def performOperation(n):
	s = str(n)
	c = 0
	if '5' not in s and '6' not in s:
		c += int(s) * 2
		return c
	mi = int(s.replace('5', '6'))
	ma = int(s.replace('6', '5'))
	return mi + ma


def performOperation(n):
	str1 = str(n)
	sum = 0
	if '5' not in str1 and '6' not in str1:
		sum += int(str1) * 2
		return sum
	min_sum = int(str1.replace('6', '5'))
	max_sum = int(str1.replace('5', '6'))
	return min_sum + max_sum


def performOperation(n):
	a = str(n)
	k = a.replace('5', '6')
	p = a.replace('6', '5')
	if '5' in a or '6' in a:
		return int(k) + int(p)
	else:
		return n + n


def performOperation(n):
	m = str(n)
	s = 0
	if str(5) not in m and str(6) not in m:
		s += int(m) * 2
		return s
	max_str = int(m.replace('5', '6'))
	min_str = int(m.replace('6', '5'))
	return max_str + min_str


def performOperation(n):
	item1 = int(str(n).replace('5', '6'))
	item2 = int(str(n).replace('6', '5'))
	return item1 + item2


def performOperation(n):
	res = 0
	s = str(n)
	s = s.replace('5', '6')
	res += int(s)
	s = str(n)
	s = s.replace('6', '5')
	res += int(s)
	return res


def performOperation(n):
	n1 = str(n)
	min = ''
	max = ''
	for i in range(len(n1)):
		if n1[i] == '5':
			min = min + '5'
			max = max + '6'
		elif n1[i] == '6':
			min = min + '5'
			max = max + '6'
		else:
			min += n1[i]
			max += n1[i]
	return int(min) + int(max)


def performOperation(n):
	c = ''
	for i in str(n):
		if i == '5':
			c += '6'
		elif i == '6':
			c += '5'
		else:
			c += i
	return int(n) + int(c)


def performOperation(n):
	s = list(str(n))
	smax = list(str(n))
	smin = list(str(n))
	for i in range(len(s)):
		if smax[i] == '5':
			smax[i] = '6'
		if smin[i] == '6':
			smin[i] = '5'
	return int(''.join(smax)) + int(''.join(smin))


def performOperation(n):
	minnum = n
	maxnum = n
	tempmin = ''
	tempmax = ''
	for i in str(n):
		if i == '5':
			tempmax += '6'
		else:
			tempmax += i
		if i == '6':
			tempmin += '5'
		else:
			tempmin += i
	if tempmin != '':
		minnum = int(tempmin)
	if tempmax != '':
		maxnum = int(tempmax)
	return minnum + maxnum


def performOperation(n):
	min = ''
	max = ''
	for i in str(n):
		if i == '5' or i == '6':
			min += '5'
			max += '6'
		else:
			min += i
			max += i
	return int(min) + int(max)


def performOperation(n):
	num1 = int(str(n).replace('5', '6'))
	num2 = int(str(n).replace('6', '5'))
	return num1 + num2


def performOperation(n):
	minstring = str(n).replace('6', '5')
	maxstring = str(n).replace('5', '6')
	return int(minstring) + int(maxstring)


def performOperation(n):
	i = 0
	s = 0
	m = n
	while n > 0:
		t = n % 10
		if t == 6:
			s = s + 5 * pow(10, i)
		elif t == 5:
			s = s + 6 * pow(10, i)
		else:
			s = s + t * pow(10, i)
		i = i + 1
		n = n // 10
	return m + s


def performOperation(n):
	l = str(n)
	m = n = ''
	m = l.replace('5', '6')
	n = l.replace('6', '5')
	return int(m) + int(n)


def performOperation(n):
	s = str(n)
	mmi = mma = ''
	for i in range(len(s)):
		if s[i] == '6':
			mmi += '5'
		else:
			mmi += s[i]
	for i in range(len(s)):
		if s[i] == '5':
			mma += '6'
		else:
			mma += s[i]
	return int(mmi) + int(mma)


def performOperation(n):
	n = str(n)
	min_num_formed = 0
	max_num_formed = 0
	for i in n:
		if i == '5':
			min_num_formed = min_num_formed * 10 + 5
			max_num_formed = max_num_formed * 10 + 6
		elif i == '6':
			min_num_formed = min_num_formed * 10 + 5
			max_num_formed = max_num_formed * 10 + 6
		else:
			min_num_formed = min_num_formed * 10 + int(i)
			max_num_formed = max_num_formed * 10 + int(i)
	return min_num_formed + max_num_formed


def performOperation(n):
	z = str(n)
	y = z.replace('5', '6')
	x = z.replace('6', '5')
	w = int(y) + int(x)
	return w


def performOperation(n):
	str_num = list(str(n))
	max_num = ''
	min_num = ''
	for i in str_num:
		if i == '5':
			max_num += '6'
			min_num += i
		elif i == '6':
			min_num += '5'
			max_num += i
		else:
			min_num += i
			max_num += i
	sum_num = int(max_num) + int(min_num)
	return sum_num


def performOperation(n):
	x = ''
	ans = 0
	s = str(n)
	for i in range(len(s)):
		if s[i] == '5':
			x += '6'
		else:
			x += str(s[i])
	ans += int(x)
	x = ''
	for i in range(len(s)):
		if s[i] == '6':
			x += '5'
		else:
			x += str(s[i])
	ans += int(x)
	return ans


def performOperation(n):
	n = str(n)
	mn = ''
	mx = ''
	for i in n:
		if i == '5':
			mx = mx + '6'
			mn += i
		elif i == '6':
			mn = mn + '5'
			mx += i
		else:
			mn += i
			mx += i
	return int(mn) + int(mx)


def performOperation(n):
	n = str(n)
	if '5' not in n and '6' not in n:
		return 2 * int(n)
	mn = ''
	mx = ''
	for i in n:
		if i == '5':
			mx = mx + '6'
			mn += i
		elif i == '6':
			mn = mn + '5'
			mx += i
		else:
			mn += i
			mx += i
	return int(mn) + int(mx)


def performOperation(n):
	a = str(n)
	b = ''
	for i in a:
		if i == '5':
			b += '6'
		elif i == '6':
			b += '5'
		else:
			b += i
	return int(a) + int(b)


def performOperation(n):
	n1 = int(str(n).replace('5', '6'))
	n2 = int(str(n).replace('6', '5'))
	return n1 + n2


def performOperation(n):
	m = str(n)
	mn = m.replace('6', '5')
	m = str(n)
	mx = m.replace('5', '6')
	return int(mx) + int(mn)


def performOperation(n):
	n1 = str(n)
	n1 = int(n1.replace('6', '5'))
	n2 = str(n)
	n2 = int(n2.replace('5', '6'))
	m = max(n, n1, n2)
	mi = min(n, n1, n2)
	return m + mi


def performOperation(n):
	n = str(n)
	g = ''
	h = ''
	for i in n:
		if i == '5':
			g = g + '6'
		else:
			g = g + i
	for j in n:
		if j == '6':
			h = h + '5'
		else:
			h = h + j
	h = int(h)
	g = int(g)
	x = h + g
	return x
if __name__ == '__main__':
	T = int(input())
	for tcs in range(T):
		n = int(input())
		print(performOperation(n))


def performOperation(n):
	minx = ''
	maxx = ''
	for i in str(n):
		if i == '5':
			maxx += '6'
			minx += i
		elif i == '6':
			minx += '5'
			maxx += i
		else:
			minx += i
			maxx += i
	return int(minx) + int(maxx)
if __name__ == '__main__':
	T = int(input())
	for tcs in range(T):
		n = int(input())
		print(performOperation(n))


def performOperation(n):

	def maxi(num):
		temp = str(num)
		max = 0
		for i in temp:
			if i == '5':
				max = max * 10 + 6
			else:
				max = max * 10 + int(i)
		return max

	def mini(num):
		temp = str(num)
		min = 0
		for i in temp:
			if i == '6':
				min = min * 10 + 5
			else:
				min = min * 10 + int(i)
		return min
	a = maxi(n)
	b = mini(n)
	return a + b
if __name__ == '__main__':
	T = int(input())
	for tcs in range(T):
		n = int(input())
		print(performOperation(n))


def performOperation(n):
	max1 = ''
	min1 = n
	n = str(n)
	l = list(n)
	for i in l:
		if i == '5':
			j = '6'
		elif i == '6':
			j = '5'
		else:
			j = i
		max1 += j
	lol = ''.join(max1)
	lol = int(lol)
	sum1 = lol + min1
	return sum1
if __name__ == '__main__':
	T = int(input())
	for tcs in range(T):
		n = int(input())
		print(performOperation(n))


def performOperation(n):
	lst = str(n)
	max_num = ''
	min_num = ''
	for ele in lst:
		if ele == '5':
			max_num += '6'
			min_num += ele
		elif ele == '6':
			max_num += ele
			min_num += '5'
		else:
			max_num += ele
			min_num += ele
	res = int(max_num) + int(min_num)
	return res
if __name__ == '__main__':
	T = int(input())
	for tcs in range(T):
		n = int(input())
		print(performOperation(n))


def performOperation(n):
	num1 = int(str(n).replace('5', '6'))
	num2 = int(str(n).replace('6', '5'))
	return num1 + num2
if __name__ == '__main__':
	T = int(input())
	for tcs in range(T):
		n = int(input())
		print(performOperation(n))


def performOperation(n):
	n = str(n)
	ma = n.replace('5', '6')
	mi = n.replace('6', '5')
	return int(ma) + int(mi)
if __name__ == '__main__':
	T = int(input())
	for tcs in range(T):
		n = int(input())
		print(performOperation(n))


def performOperation(n):
	maxnum = 0
	minnum = 99999
	n = str(n)
	xr = ''
	yr = ''
	for i in n:
		if i == '5':
			xr += '6'
		else:
			xr += i
	for i in n:
		if i == '6':
			yr += '5'
		else:
			yr += i
	return int(xr) + int(yr)
if __name__ == '__main__':
	T = int(input())
	for tcs in range(T):
		n = int(input())
		print(performOperation(n))


def performOperation(n):
	n = str(n)
	k = n.replace('5', '6')
	l = n.replace('6', '5')
	return int(l) + int(k)


def performOperation(n):
	s1 = int(str(n).replace('5', '6'))
	s2 = int(str(n).replace('6', '5'))
	return s1 + s2


def performOperation(n):
	n = str(n)
	mn = ''
	mx = ''
	for ch in n:
		if ch == '5':
			mx += '6'
		else:
			mx += ch
		if ch == '6':
			mn += '5'
		else:
			mn += ch
	return int(mn) + int(mx)


def performOperation(n):
	S = str(n)
	smin = S.replace('6', '5')
	smax = S.replace('5', '6')
	return int(smin) + int(smax)


def performOperation(n):
	low = str(n)
	high = str(n)
	low = low.replace('6', '5')
	high = high.replace('5', '6')
	ans = int(low) + int(high)
	return ans


def performOperation(n):
	a = list(str(n))
	b = list(str(n))
	for i in range(len(a)):
		if a[i] == '5':
			a[i] = '6'
	a = ''.join(a)
	maxi = a
	for j in range(len(b)):
		if b[j] == '6':
			b[j] = '5'
	b = ''.join(b)
	mini = b
	return int(maxi) + int(mini)


def performOperation(n):
	m1 = ''
	m2 = ''
	s = str(n)
	for ch in s:
		if ch == '6':
			m1 += '5'
		if ch == '6':
			m2 += ch
		if ch == '5':
			m1 += ch
		if ch == '5':
			m2 += '6'
		if ch != '5' and ch != '6':
			m1 += ch
			m2 += ch
	ans = int(m1) + int(m2)
	return ans


def performOperation(n):
	s = []
	l = []
	while n:
		r = n % 10
		if r == 5 or r == 6:
			s.append(str(5))
			l.append(str(6))
		else:
			s.append(str(r))
			l.append(str(r))
		n = n // 10
	s.reverse()
	l.reverse()
	s = ''.join(s)
	l = ''.join(l)
	return int(s) + int(l)


def performOperation(n):
	a = int(str(n).replace('5', '6'))
	b = int(str(n).replace('6', '5'))
	return a + b


def performOperation(n):
	s = str(n)
	o = ''
	for i in s:
		if i == '5':
			o += '6'
		elif i == '6':
			o += '5'
		else:
			o += i
	return int(s) + int(o)


def performOperation(n):
	num = str(n)
	if '5' not in num and '6' not in num:
		return n * 2
	min_num = num
	max_num = num
	for i in range(len(num)):
		char = num[i]
		if char == '6':
			min_num = min_num[:i] + '5' + min_num[i + 1:]
		if char == '5':
			max_num = max_num[:i] + '6' + max_num[i + 1:]
	return int(min_num) + int(max_num)


def performOperation(n):
	maximum = ''
	minimum = ''
	for i in str(n):
		if i == '5':
			maximum += '6'
		else:
			maximum += i
	for i in str(n):
		if i == '6':
			minimum += '5'
		else:
			minimum += i
	return int(minimum) + int(maximum)


def performOperation(n):
	return int(str(n).replace('6', '5')) + int(str(n).replace('5', '6'))


def performOperation(n):
	num = str(n)
	number = str(n)
	for char in num:
		if char == '5':
			num = num.replace(char, '6')
	max_number = int(num)
	for char in number:
		if char == '6':
			number = number.replace(char, '5')
	min_number = int(number)
	sum = min_number + max_number
	return sum


def performOperation(n):
	minsum = n
	maxsum = n
	if '5' in str(n):
		x = str(n).replace('5', '6')
		if int(x) > maxsum:
			maxsum = int(x)
		else:
			minsum = int(x)
	if '6' in str(n):
		x = str(n).replace('6', '5')
		if int(x) > maxsum:
			maxsum = int(x)
		else:
			minsum = int(x)
	return minsum + maxsum


def performOperation(n):
	n = str(n)
	one = n.replace('6', '5')
	two = n.replace('5', '6')
	return int(one) + int(two)


def performOperation(n):
	tmp = n
	n1 = ''
	while n > 0:
		r = n % 10
		if r == 5:
			r = 6
		elif r == 6:
			r = 5
		n1 += str(r)
		n = n // 10
	return int(n1[::-1]) + tmp


def performOperation(n):
	ns = str(n)
	minn = ''
	maxn = ''
	for i in ns:
		if i == '5':
			maxn = maxn + '6'
		else:
			maxn = maxn + i
		if i == '6':
			minn = minn + '5'
		else:
			minn = minn + i
	sum = int(minn) + int(maxn)
	return sum


def performOperation(n):
	a = 0
	b = 0
	x = 0
	while n > 0:
		if n % 10 == 5 or n % 10 == 6:
			a += 5 * pow(10, x)
			b += 6 * pow(10, x)
			x += 1
			n //= 10
		else:
			a += n % 10 * pow(10, x)
			b += n % 10 * pow(10, x)
			x += 1
			n //= 10
	return a + b


def performOperation(n):
	s = str(n)
	str1 = ''
	for i in s:
		if i != '6':
			str1 += i
		else:
			str1 += '5'
	str2 = ''
	for j in s:
		if j != '5':
			str2 += j
		else:
			str2 += '6'
	min1 = int(str1)
	max1 = int(str2)
	return min1 + max1


def performOperation(n):
	s = str(n)
	if '5' or '6' in s:
		return int(s.replace('5', '6')) + int(s.replace('6', '5'))
	else:
		return 2 * n