Find a number such that sum of N with it is a Palindrome
Given a very large number N, [1 ≤ length of digit of N (n) ≤ 106], the task is to find some positive integer of the same length as N without leading zeroes, such that the sum of these two numbers is a palindrome.
Examples:
Input: N = 54321
Output: 45678
Explanation: 54321 + 45678 = 99999 which is a palindrome.
Input: N = 999
Output: 112
Approach: To solve the problem follow the below idea:
If the number does not start with 9, then make the palindrome of 9999…… of length of N and if the number starts with 9 then make the palindrome 1111….. of length of (n + 1).
- So, the resultant number if it does not starts with 9 is (9999….. to n – N).
- And, the resultant number if it starts with 9 is (11111….. to n+1 – N).
Follow the below steps to solve the problem:
- Find the number of digits present in N.
- If N starts with 9:
- Then consider the sum to be [1111…to (n+1)].
- Then calculate the number as mentioned above.
- If N does not start with 9:
- Then consider the sum to be [999…to n].
- Get the number following the process mentioned above.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
string findDiff(string str1, string str2)
{
string str = "" ;
int n1 = str1.length(), n2 = str2.length();
reverse(str1.begin(), str1.end());
reverse(str2.begin(), str2.end());
int carry = 0;
for ( int i = 0; i < n2; i++) {
int sub
= ((str1[i] - '0' ) - (str2[i] - '0' ) - carry);
if (sub < 0) {
sub = sub + 10;
carry = 1;
}
else
carry = 0;
str.push_back(sub + '0' );
}
for ( int i = n2; i < n1; i++) {
int sub = ((str1[i] - '0' ) - carry);
if (sub < 0) {
sub = sub + 10;
carry = 1;
}
else
carry = 0;
}
reverse(str.begin(), str.end());
return str;
}
string findPalin(string N)
{
ll n = N.size();
string str = "" ;
if (N[0] == '9' ) {
while (n--) {
str += '1' ;
}
str += '1' ;
return findDiff(str, N);
}
else {
for (ll i = 0; i < n; i++) {
str += ((9 - (N[i] - '0' )) + '0' );
}
return str;
}
}
int main()
{
string N = "54321" ;
cout << findPalin(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static String findDiff(String str1, String str2)
{
String str = "" ;
int n1 = str1.length(), n2 = str2.length();
str1 = reverse(str1);
str2 = reverse(str2);
int carry = 0 ;
for ( int i = 0 ; i < n2; i++) {
int sub
= ((str1.charAt(i) - '0' ) - (str2.charAt(i) - '0' ) - carry);
if (sub < 0 ) {
sub = sub + 10 ;
carry = 1 ;
}
else
carry = 0 ;
str+=(sub + '0' );
}
for ( int i = n2; i < n1; i++) {
int sub = ((str1.charAt(i) - '0' ) - carry);
if (sub < 0 ) {
sub = sub + 10 ;
carry = 1 ;
}
else
carry = 0 ;
}
str = reverse(str);
return str;
}
static String findPalin(String N)
{
long n = N.length();
String str = "" ;
if (N.charAt( 0 ) == '9' ) {
while (n-- > 0 ) {
str += '1' ;
}
str += '1' ;
return findDiff(str, N);
}
else {
for ( int i = 0 ; i < n; i++) {
str += String.valueOf( 9 - Integer.parseInt(String.valueOf(N.charAt(i))));
}
return str;
}
}
static String reverse(String input) {
char [] a = input.toCharArray();
int l, r = a.length - 1 ;
for (l = 0 ; l < r; l++, r--) {
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.valueOf(a);
}
public static void main(String[] args)
{
String N = "54321" ;
System.out.print(findPalin(N));
}
}
|
Python3
def findDiff(str1, str2):
str_ = ""
n1 = len (str1)
n2 = len (str2)
str1 = str1[:: - 1 ]
str2 = str2[:: - 1 ]
carry = 0
for i in range (n2):
sub = ( int (str1[i]) - int (str2[i]) - carry)
if (sub < 0 ):
sub = sub + 10
carry = 1
else :
carry = 0
str_ + = str_(sub)
for i in range (n2, n1):
sub = int (str1[i]) - carry
if (sub < 0 ):
sub = sub + 10
carry = 1
else :
carry = 0
str_ = str_[:: - 1 ]
return str_
def findPalin(N):
n = len (N)
str_ = ""
if (N[ 0 ] = = '9' ):
while n:
str_ + = '1'
n - = 1
str_ + = '1'
return findDiff(str_, N)
else :
for i in range (n):
str_ + = str ( 9 - int (N[i]))
return str_
N = "54321"
print (findPalin(N))
|
C#
using System;
class GFG {
static string findDiff( string str1, string str2)
{
string str = "" ;
int n1 = str1.Length, n2 = str2.Length;
str1 = reverse(str1);
str2 = reverse(str2);
int carry = 0;
for ( int i = 0; i < n2; i++) {
int sub
= ((str1[i] - '0' ) - (str2[i] - '0' ) - carry);
if (sub < 0) {
sub = sub + 10;
carry = 1;
}
else
carry = 0;
str+=(sub + '0' );
}
for ( int i = n2; i < n1; i++) {
int sub = ((str1[i] - '0' ) - carry);
if (sub < 0) {
sub = sub + 10;
carry = 1;
}
else
carry = 0;
}
str = reverse(str);
return str;
}
static string findPalin( string N)
{
long n = N.Length;
string str = "" ;
if (N[0] == '9' ) {
while (n-- >0) {
str += '1' ;
}
str += '1' ;
return findDiff(str, N);
}
else {
for ( int i = 0; i < n; i++) {
str += (9 - Int32.Parse(N[i].ToString())).ToString();
}
return str;
}
}
static string reverse( string input) {
char [] a = input.ToCharArray();
int l, r = a.Length - 1;
for (l = 0; l < r; l++, r--) {
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return a.ToString();
}
public static void Main()
{
string N = "54321" ;
Console.Write(findPalin(N));
}
}
|
Javascript
<script>
function findDiff(str1, str2)
{
let str = "" ;
let n1 = str1.length, n2 = str2.length;
str1 = str1.split( "" ).reverse().join( "" );
str2 = str2.split( "" ).reverse().join( "" );
let carry = 0;
for (let i = 0; i < n2; i++) {
let sub
= ((str1[i] - '0' ) - (str2[i] - '0' ) - carry);
if (sub < 0) {
sub = sub + 10;
carry = 1;
}
else
carry = 0;
str += sub.toString();
}
for (let i = n2; i < n1; i++) {
let sub = ((str1.charCodeAt(i) - '0' .charCodeAt(0)) - carry);
if (sub < 0) {
sub = sub + 10;
carry = 1;
}
else
carry = 0;
}
str = str.split().reverse().join( "" );
return str;
}
function findPalin(N)
{
let n = N.length;
let str = "" ;
if (N[0] == '9' ) {
while (n--) {
str += '1' ;
}
str += '1' ;
return findDiff(str, N);
}
else {
for (let i = 0; i < n; i++) {
str += (9 - parseInt(N[i])).toString();
}
return str;
}
}
let N = "54321" ;
document.write(findPalin(N), "</br>" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
18 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...