- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Find a Number Such that Sum of N with it is a Palindrome
In this problem, we will find the string of length equal to the given string so that when we sum both strings, we get the palindromic strings.
Here, we can find another string such that the sum of both becomes 99999…, the largest palindromic string of the same length. If the given string starts with the ‘9’, we can find another string such that the sum of both becomes ‘11111…’.
Problem statement – We have given a num string containing the numeric digits. We need to find the numeric string of the same length without leading zeros so that sum of both strings can be a palindromic string.
Sample examples
Input
num = "9865";
Output
1246
Explanation − When we do the sum of 9856 and 12446, we get the 11111, which is a palindromic string.
Input
num = "54534";
Output
45465
Explanation − The sum of 54534 + 45465 is 99999.
Input
num = “5”
Output
4
Explanation − The sum of the 5 + 4 is 9.
Approach 1
Any string containing a single character will always be a palindromic string. Here, we need to find another string of the same length. So, we can find the string such that we get the largest palindromic string of the length N or the smallest palindromic string of length N + 1.
Whenever the given string starts with the ‘1’ to ‘8’ digits, we will find another string to get the resultant sum ‘9999..’ with a length equal to N. When the given string starts with the ‘9’, we find another string so that the resultant sum becomes ‘1111…’ of length N + 1.
Algorithm
Step 1 − Initialise the ‘temp’ string to store the resultant string.
Step 2 − If the first character of the given string is ‘9’, append string length + 1 number of ‘1’ characters to the temp string. After that, execute the getDifference() function to find the difference between the ‘temp’ and given string and return its values.
Step 2.1 − In the getDIfference() function, initialize the ‘diff’ string to store the difference.
Step 2.2 − Use the reverse() method to reverse the num1 and num2 strings.
Step 2.3 − Initialize the ‘car’ with 0 to store the carry while subtracting.
Step 2.4 − Start traversing the num2 string. Subtract the num2[p] and ‘car’ values from the num1[p] character, and store them in the ‘sub’ variable.
Step 2.5 − If the sub value is negative, add 10 to the sub and update ‘car’ with 1. Otherwise, update ‘car’ with 0.
Step 2.6 − Push the ‘sub’ value in the ‘diff’ string.
Step 2.7 − Finally, reverse the ‘diff’ string and return its value.
Step 3 − If the first character is between ‘0’ to ‘8’, we need to find another string such that the sum of it with the given string becomes ‘9999….’.
Step 4 − Traverse the given string, and append the resultant character to the ‘temp’ string after subtracting the current character from ‘9’.
Step 5 − Return the ‘temp’ string at the end.
Example
#include <bits/stdc++.h> using namespace std; #define ll long long string getDifference(string num1, string num2) { string diff = ""; // To store difference of num1 and num2 int len1 = num1.length(), len2 = num2.length(); // Reverse strings reverse(num1.begin(), num1.end()); reverse(num2.begin(), num2.end()); int car = 0; // Traverse numeric strings for (int p = 0; p < len2; p++) { // Getting a difference int sub = ((num1[p] - '0') - (num2[p] - '0') - car); // Basic subtraction logic if (sub < 0) { sub = sub + 10; car = 1; } else car = 0; // Push the sub in the string diff.push_back(sub + '0'); } // Reverse the final string reverse(diff.begin(), diff.end()); return diff; } string getNum(string num) { ll len = num.size(); string temp = ""; // When string starting with '9' if (num[0] == '9') { // Get string containing len + 1 '1' digits while (len--) { temp += '1'; } temp += '1'; return getDifference(temp, num); } // In other cases else { for (ll p = 0; p < len; p++) { // Get a string such that we can form the '999999999..' string temp += ((9 - (num[p] - '0')) + '0'); } return temp; } } int main() { string num = "53242"; cout << "We can get palindrome number by adding " << num << " and " << getNum(num) << ".\n"; return 0; }
Output
We can get palindrome number by adding 53242 and 46757.
Time complexity − O(N) to get the difference between two strings.
Space complexity − O(N) to store the resultant string.
We have learned to find any string in the output so that sum of the string with the given string becomes palindromic. However, we used basic mathematics for the subtraction of two strings. Programmers can try to solve the problem of finding the nearest string to the given string such that the sum of both strings becomes palindromic.