Given a number, reverse it and add it to itself unless it becomes a palindrome or number of iterations becomes more than 5.
Example 1:
Input: n = 23
Output: 55 
Explanation: reverse(23) = 32,then 32+23
= 55 which is a palindrome. 
Example 2:
Input: n = 30
Output: 33
Explanation: reverse(30) = 3,then 3+30
= 33 which is palindrome. 
Your Task:  
You dont need to read input or print anything. Complete the function isSumPalindrome() which takes n as input parameter and returns that palindrome number if it becomes a palindrome else returns -1.
Expected Time Complexity: O(n*k),where k<=5.
Expected Auxiliary Space: O(1)
Constraints:
1<= n <=10^{4}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Mathematics

Starter code: #User function Template for python3



class Solution:

    def isSumPalindrome (self, n):

        # code here 

Solutions:
class Solution:

	def isSumPalindrome(self, n):
		if str(n) == str(n)[::-1]:
			return n
		for i in range(5):
			n += int(str(n)[::-1])
			if str(n) == str(n)[::-1]:
				return n
		return -1


def reverse_number(n):
	result = 0
	visited_not_zero = False
	while n > 0:
		if n % 10:
			visited_not_zero = True
		if visited_not_zero:
			result *= 10
			result += n % 10
		n //= 10
	return result

class Solution:

	def isSumPalindrome(self, n):
		for _ in range(6):
			number = reverse_number(n)
			if number == n:
				return number
			else:
				n += number
		return -1


class Solution:

	def isSumPalindrome(self, n):
		n = str(n)
		reverse = n[::-1]
		if n == reverse:
			return int(n)
		num = int(n) + int(reverse)
		for i in range(5):
			num = str(num)
			if num == num[::-1]:
				return num
			else:
				num = int(num) + int(num[::-1])
		return -1


class Solution:

	def isSumPalindrome(self, n):
		num = int(str(n)[::-1])
		if n == num:
			return n
		for i in range(5):
			num = int(str(n)[::-1])
			a = 0
			a = num + n
			b = int(str(a)[::-1])
			if a == int(b):
				return a
			else:
				n = a
		return -1


class Solution:

	def isSumPalindrome(self, n):

		def reverse(n):
			rev_num = 0
			while n > 0:
				rev_num = rev_num * 10 + n % 10
				n = n // 10
			return rev_num

		def isPalindrome(n):
			return reverse(n) == n
		count = 0
		while isPalindrome(n) == False and count < 5:
			k = reverse(n)
			n += k
			count += 1
		if isPalindrome(n):
			return n
		return -1


class Solution:

	def isSumPalindrome(self, n):
		r = int(str(n)[::-1])
		if n == r:
			return r
		for i in range(5):
			s = 0
			r = int(str(n)[::-1])
			s = r + n
			b = int(str(s)[::-1])
			if s == b:
				return s
			else:
				n = s
		return -1


class Solution:

	def isSumPalindrome(self, n):
		res = -1
		i = 0
		while i < 6:
			if self.isPalindrome(n):
				res = n
				break
			else:
				reverse = self.getReverse(n)
				n = reverse + n
			i = i + 1
		return res

	def getReverse(self, n):
		rev = 0
		temp = n
		while temp > 0:
			rem = temp % 10
			rev = rev * 10 + rem
			temp = int(temp / 10)
		return rev

	def isPalindrome(self, n):
		if n == self.getReverse(n):
			return True
		return False


class Solution:

	def reverse(self, n):
		x = n
		reverse = 0
		while n != 0:
			number = n % 10
			reverse = reverse * 10 + number
			n = n // 10
		return reverse

	def isSumPalindrome(self, n):
		k = 0
		while k <= 5:
			reverse = self.reverse(n)
			if n == reverse:
				return n
			n += reverse
			k += 1
		return -1


class Solution:

	def reverse(self, n):
		rev = 0
		while n > 0:
			rem = n % 10
			rev = rev * 10 + rem
			n //= 10
		return rev

	def isSumPalindrome(self, n):
		k = 0
		while k <= 5:
			rev = self.reverse(n)
			if n == rev:
				return n
			n += rev
			k += 1
		return -1


class Solution:

	def isSumPalindrome(self, n):
		if str(n) == str(n)[::-1]:
			return n
		else:
			for i in range(5):
				res = 0
				res = n + int(str(n)[::-1])
				if str(res) == str(res)[::-1]:
					return res
				else:
					n = res
			else:
				return -1


class Solution:

	def isSumPalindrome(self, n):
		num = int(str(n)[::-1])
		if n == num:
			return num
		else:
			for i in range(5):
				num = int(str(n)[::-1])
				a = num + n
				b = int(str(a)[::-1])
				if a == b:
					return a
				else:
					n = a
		return -1
		iter = 5
		while iter:
			sum1 = n + rev(n)
			sum2 = rev(sum1)
			if sum1 == sum2:
				return sum1
			else:
				n = sum1
				sum1 = 0
				sum2 = 0
			iter += 1
		return -1


class Solution:

	def palindrome(self, n):
		sum = 0
		while n != 0:
			rem = n % 10
			sum = sum * 10 + rem
			n //= 10
		return sum

	def isSumPalindrome(self, num):
		if num == self.palindrome(num):
			return num
		reverse_number = num + self.palindrome(num)
		for i in range(5):
			if self.palindrome(reverse_number) == reverse_number:
				return reverse_number
			else:
				reverse_number = reverse_number + self.palindrome(reverse_number)
		return -1


def checkpalindrome(n):
	if str(n) != str(n)[::-1]:
		return False
	return True

def reverse(n):
	return int(str(n)[::-1])

class Solution:

	def isSumPalindrome(self, n):
		if checkpalindrome(n):
			return n
		sum = 0
		for i in range(5):
			rev = reverse(n)
			sum = n + rev
			if checkpalindrome(sum):
				return sum
			n = sum
		return -1


class Solution:

	def isSumPalindrome(self, n):
		if self.isPalindrome(n):
			return n
		for i in range(5):
			rev_n = int(str(n)[::-1])
			n += rev_n
			if self.isPalindrome(n):
				return n
		return -1

	def isPalindrome(self, num):
		return str(num) == str(num)[::-1]


class Solution:

	def isSumPalindrome(self, n):
		if self.checkPalindrome(n):
			return n
		for count in range(5):
			rev_numr = self.reversenumber(n)
			n += rev_numr
			if self.checkPalindrome(n):
				return n
		else:
			return -1

	def reversenumber(self, n):
		rev_num = 0
		while n != 0:
			rev_num = rev_num * 10 + n % 10
			n = n // 10
		return rev_num

	def checkPalindrome(self, n):
		return True if n == self.reversenumber(n) else False


class Solution:

	def isSumPalindrome(self, n):
		for i in range(0, 5):
			rev_n = str(n)
			if rev_n == rev_n[::-1]:
				return n
				break
			else:
				sum_n = n + int(rev_n[::-1])
				str_sum = str(sum_n)
				if str_sum == str_sum[::-1]:
					return int(str_sum)
					break
				else:
					n = int(str_sum)
		return -1


class Solution:

	def isSumPalindrome(self, n):
		for i in range(6):
			if str(n) == str(n)[::-1]:
				return n
			else:
				n = n + int(str(n)[::-1])
		return -1


class Solution:

	def reverse(self, n):
		rev = 0
		while n > 0:
			a = n % 10
			rev = rev * 10 + a
			n = n // 10
		return rev

	def IsPalindrome(self, n):
		temp = self.reverse(n)
		return temp == n

	def isSumPalindrome(self, n):
		if self.reverse(n) == n:
			return n
		for _ in range(5):
			n = self.reverse(n) + n
			if self.IsPalindrome(n):
				return n
		return -1


class Solution:

	def isSumPalindrome(self, n):
		nn = int(str(n)[::-1])
		if n == nn:
			return n
		for i in range(5):
			nn = int(str(n)[::-1])
			x = nn + n
			y = int(str(x)[::-1])
			if x == y:
				return x
			else:
				n = x
		return -1


class Solution:

	def isSumPalindrome(self, n):
		i = 0
		item = str(n)
		while i < 6 and item != item[::-1]:
			item = str(int(item) + int(item[::-1]))
			i += 1
		return item if i < 6 else -1


class Solution:

	def isSumPalindrome(self, n):
		i = 0
		while i < 6:
			n = str(n)
			if n == n[::-1]:
				return n
			else:
				x = int(n[::-1])
				n = x + int(n)
				i += 1
		return -1


class Solution:

	def isSumPalindrome(self, n):
		num = 0
		while num < 5:
			a = str(n)[::-1]
			if n == int(a):
				return n
			else:
				b = int(n) + int(a)
				c = str(b)[::-1]
				if b == int(c):
					return b
				else:
					n = c
					num += 1
		else:
			return -1


class Solution:

	def isSumPalindrome(self, n):
		if self.isPalindrome(n):
			return n
		for i in range(0, 5):
			s = [i for i in str(n)]
			s.reverse()
			r = int(''.join(s))
			a = n + r
			if self.isPalindrome(a):
				return a
			n = a
		return -1

	def isPalindrome(self, a):
		b = [i for i in str(a)]
		br = list(reversed(b))
		if b == br:
			return True
		return False


class Solution:

	def rev(self, n):
		str_n = str(n)
		(lis, out) = ([], '')
		for i in str_n:
			lis.append(i)
		for i in reversed(lis):
			out += i
		return int(out)

	def ispalin(self, n):
		if n == self.rev(n):
			return True
		return False

	def isSumPalindrome(self, n):
		if self.ispalin(n):
			return n
		i = 0
		sum = n
		while i < 5:
			sum += self.rev(sum)
			if self.ispalin(sum):
				return sum
			i += 1
		return -1


class Solution:

	def ispali(self, n):
		if len(str(n)) == 1 or len(str(n)) == 0:
			return 1
		elif len(str(n)) > 1:
			if str(n)[0] == str(n)[-1] and self.ispali(str(n)[1:len(str(n)) - 1]):
				ans = 1
			else:
				return 0
		return ans

	def isSumPalindrome(self, n):
		if self.ispali(n):
			return n
		isp = 0
		for i in range(5):
			n = n + int(str(n)[::-1])
			isp = self.ispali(n)
			if isp == 1:
				return n
		return -1


class Solution:

	def isSumPalindrome(self, n):
		s1 = str(n)
		s2 = s1[::-1]
		if s1 == s2:
			return n
		n1 = int(s1)
		n2 = int(s2)
		res = 0
		for i in range(5):
			res = str(n1 + n2)
			if res == res[::-1]:
				return int(res)
			else:
				s1 = str(res)
				s2 = s1[::-1]
				n1 = int(s1)
				n2 = int(s2)
		return -1


class Solution:

	def isSumPalindrome(self, N):
		n = N

		def reverse(N):
			rev = 0
			while N:
				res = N % 10
				rev = rev * 10 + res
				N //= 10
			return rev
		if n == reverse(n):
			return n
		for i in range(5):
			n = n + reverse(n)
			if n == reverse(n):
				return n
			else:
				n = n
		return -1


class Solution:

	def isSumPalindrome(self, n):
		n = str(n)
		c = 0
		while n != n[::-1] and c < 5:
			n = str(int(n) + int(n[::-1]))
			c += 1
		return n if n == n[::-1] else -1


class Solution:

	def isSumPalindrome(self, n):
		count = 0
		while count <= 5:
			sum1 = 0
			num = n
			str_n = str(n)
			if len(str_n) % 2 == 0:
				str_n_first = str_n[:len(str_n) // 2]
				str_n_end = str_n[len(str_n) // 2:]
				str_n_end = str_n_end[::-1]
			else:
				str_n_first = str_n[:len(str_n) // 2]
				str_n_end = str_n[len(str_n) // 2 + 1:]
				str_n_end = str_n_end[::-1]
			if str_n_first == str_n_end:
				break
			else:
				while n > 0:
					remainder = n % 10
					sum1 = sum1 * 10 + remainder
					n //= 10
				n = sum1 + num
			count += 1
		if count > 5:
			return -1
		else:
			return n


class Solution:

	def isSumPalindrome(self, n):
		if n == int(str(n)[::-1]):
			return n
		else:
			m = n + int(str(n)[::-1])
			count = 0
			while count < 5:
				reverse = int(str(m)[::-1])
				if m == reverse:
					return m
				else:
					m = m + reverse
				count += 1
		return -1


class Solution:

	def reverse(self, n):
		ans = 0
		while n != 0:
			ans *= 10
			ans += n % 10
			n //= 10
		return ans

	def isSumPalindrome(self, n):
		k = 0
		while k < 5:
			rev = self.reverse(n)
			if n == rev:
				return n
			sumAns = rev + n
			if sumAns == self.reverse(sumAns):
				return sumAns
			n = sumAns
			k += 1
		return -1


class Solution:

	def isSumPalindrome(self, n):
		temp = og = n
		for i in range(6):
			palin = 0
			for j in range(len(str(og))):
				rem = temp % 10
				palin = palin * 10 + rem
				temp = temp // 10
			if palin == og:
				return og
			reversed = 0
			temp = og
			for i in range(len(str(og))):
				remainder = temp % 10
				reversed = reversed * 10 + remainder
				temp = temp // 10
			temp = og = reversed + og
		return -1


class Solution:

	def isSumPalindrome(self, n):
		if self.checkPalindrome(n):
			return n
		for i in range(5):
			n = n + int(str(n)[::-1])
			if self.checkPalindrome(n):
				return n
		return -1

	def checkPalindrome(self, num):
		num1 = num
		num2 = ''
		while num:
			num2 += str(num % 10)
			num = int(num / 10)
		if num1 == int(num2):
			return True
		else:
			return False


class Solution:

	def isSumPalindrome(self, n):
		c = 0
		while str(n) != str(n)[::-1] and c < 5:
			rev = 0
			m = len(str(n))
			an = n
			while n > 0:
				r = n % 10
				n = n // 10
				rev += r * 10 ** (m - 1)
				m = m - 1
			c = c + 1
			n = an + rev
		if str(n) == str(n)[::-1]:
			return n
		return -1


class Solution:

	def isSumPalindrome(self, n):
		k = n
		s = n
		if str(n) == str(n)[::-1]:
			return n
		for i in range(5):
			rev = 0
			rev1 = 0
			while k:
				rem = k % 10
				rev = rev * 10 + rem
				k = k // 10
			s += rev
			l = s
			while l:
				rem1 = l % 10
				rev1 = rev1 * 10 + rem1
				l = l // 10
			if s == rev1:
				return s
			else:
				k = s
		else:
			return -1


class Solution:

	def isSumPalindrome(self, n):
		sum1 = str(n)
		i = 0
		while sum1 != sum1[::-1] and i < 5:
			b = sum1[::-1]
			sum1 = str(int(sum1) + int(b))
			i += 1
		if sum1 == sum1[::-1]:
			return int(sum1)
		else:
			return -1


class Solution:

	def isSumPalindrome(self, n):
		for i in range(5):
			n = str(n)
			q = n[::-1]
			n = int(n)
			q = int(q)
			if n == q:
				return n
			else:
				w = q + n
				w = str(w)
				e = w[::-1]
				if e == w:
					return w
				else:
					n = w
		return -1


class Solution:

	def isSumPalindrome(self, n):
		if str(n) == str(n)[::-1]:
			return n
		for i in range(5):
			a = int(str(n)[::-1]) + n
			if str(a) == str(a)[::-1]:
				return a
				break
			else:
				n = a
		return -1


class Solution:

	def isSumPalindrome(self, n):
		if str(n) == str(n)[::-1]:
			return n
		for _ in range(5):
			x = n + int(str(n)[::-1])
			if str(x) == str(x)[::-1]:
				return x
			n = x
		return -1


class Solution:

	def reverse(self, num):
		reversed_num = 0
		while num:
			digit = num % 10
			reversed_num = reversed_num * 10 + digit
			num = num // 10
		return reversed_num

	def check_palindrome(self, num):
		return self.reverse(num) == num

	def isSumPalindrome(self, n):
		count = 0
		while not self.check_palindrome(n) and count < 5:
			n = n + self.reverse(n)
			count += 1
		if self.check_palindrome(n):
			return n
		return -1


class Solution:

	def isSumPalindrome(self, n):

		def reverse(n):
			ans = 0
			while n > 0:
				rem = n % 10
				ans = ans * 10 + rem
				n = n // 10
			return ans
		count = 0
		while count <= 5:
			if n == reverse(n):
				return n
				break
			n = n + reverse(n)
			count += 1
		return -1


class Solution:

	def isSumPalindrome(self, n):
		i = 0
		ans = n
		while i <= 5:
			if str(ans) == str(ans)[::-1]:
				return ans
			n = ans
			res = 0
			while n > 0:
				rem = n % 10
				res = res * 10 + rem
				n //= 10
			ans = ans + res
			i += 1
		else:
			return -1


class Solution:

	def isSumPalindrome(self, n):

		def total(n):
			org = n
			reverse = 0
			temp = 0
			while n != 0:
				temp = n % 10
				reverse = reverse * 10 + temp
				n = int(n / 10)
			return reverse + org

		def palindrome(number):
			num = list(str(number))
			for i in range(int(len(num) / 2)):
				if num[i] != num[len(num) - 1 - i]:
					return False
			return True
		for i in range(6):
			if palindrome(n):
				return n
			else:
				n = total(n)
		return -1


class Solution:

	def isSumPalindrome(self, n):
		if n == int(str(n)[::-1]):
			return n
		for i in range(5):
			n = str(n)
			r = n[::-1]
			s = int(n) + int(r)
			s = str(s)
			s1 = s[::-1]
			if s == s1:
				return int(s)
				break
			else:
				n = s
		return -1


class Solution:

	def isSumPalindrome(self, n):
		if self.palindrome(n):
			return n
		for i in range(5):
			sum = n + int(self.reverse(n))
			if self.palindrome(sum):
				return sum
			n = sum
		return -1

	def reverse(self, num):
		return str(num)[::-1]

	def palindrome(self, num):
		temp = str(num)
		return str(num) == temp[::-1]


class Solution:

	def isPalindrome(word):
		temp = str(word)
		return temp == temp[::-1]

	def reverse(x):
		return str(x)[::-1]

	def isSumPalindrome(self, n):
		if Solution.isPalindrome(n):
			return n
		for i in range(5):
			sum = n + int(Solution.reverse(n))
			if Solution.isPalindrome(sum):
				return sum
			n = sum
		return -1


class Solution:

	def rev(self, n):
		r = 0
		while n > 0:
			num = n % 10
			n = n // 10
			r = r * 10 + num
		return r

	def isSumPalindrome(self, N):
		i = 1
		while i <= 5:
			a = self.rev(N)
			if a == N:
				return N
			N = N + a
			b = self.rev(N)
			if b == N:
				return N
				break
			i += 1
		return -1


class Solution:

	def isSumPalindrome(self, n):
		sum = 0
		a = str(n)
		m = a[::-1]
		x = 0
		if a == a[::-1]:
			return a
		for i in range(5):
			sum = int(m) + n
			sum2 = str(sum)
			if sum2 == sum2[::-1]:
				x = 1
				break
			n = sum
			m = sum2[::-1]
		if x == 0:
			return -1
		else:
			return sum


class Solution:

	def isSumPalindrome(self, n):
		for i in range(5):
			n = str(n)
			if n == n[::-1]:
				return n
			rn = n[::-1]
			t = int(n) + int(rn)
			t = str(t)
			if t == t[::-1]:
				return t
			else:
				n = t
		return -1


class Solution:

	def reverse(self, num):
		rev_num = 0
		while num > 0:
			rev_num = rev_num * 10 + num % 10
			num = num // 10
		return rev_num

	def isPolindrom(self, num):
		return self.reverse(num) == num

	def isSumPalindrome(self, n):
		if self.isPolindrom(n):
			return n
		rev_num = n
		loop = 0
		while loop < 5:
			rev_num += self.reverse(rev_num)
			if self.isPolindrom(rev_num):
				return rev_num
			loop += 1
		return -1