Next Smallest Palindrome | InterviewBit Math | Logic Explained

preview_player
Показать описание
Next Smallest Palindrome is a tricky InterviewBit question. I explain the solution to this problem in detail using various examples and guiding through the mess of edge cases. The solution we reach to is quite simple, and perhaps even shockingly simple for a "Hard" question. This video covers the logic part, and the next video covers the code part.

Writeup (Summary + Code):

==================================================

Timestamps:
0:00 Introduction
0:25 Problem Discussion
1:09 Odd Even split
2:08 Odd cases
10:00 Even cases

==================================================

Stuff I use:

NOTE: These are affiliate links.
- Doesn't cost you anything
- Gives me a kickback if you buy this

==================================================

Donate:

==================================================

Follow:
#LeetCode #coding #programming #chaudhary1337

==================================================
Рекомендации по теме
Комментарии
Автор

We need more content and people like you. You explained it so simply. Great bro .

sgcreations
Автор

I wish I can someday meet you in person and thank you for this explanation and I adore you for being the best at explaining!!

The-masked-girl-songs
Автор

u explain really good, 😍, u got my subscription

anukulsahu
Автор

amazing solution and explanation. sadly I just failed my interview on this question today

yujhengfang
Автор

Hi Tanishq, I have doubt regarding the approach to deal with Polindrom string itself, having a larger number of 9's suppose my string is
(78 times 9) in this case, adding 1 will going out the memory exception?

sudhiryadav
Автор

Such a Nice Explanation...😍 You Stole my subscription😇

manpatel
Автор

Bro sorry to say this, u are wrong:
according to u if "left < right" mid+1;
but it does not satisfy for many cases like
{19263} => {19291}
here we are not adding 1 to mid number

chrishemsworth
Автор

Well, I should say this was a nice explanation- IIT'R-24

AMANKUMAR-twjt
Автор

Why does the trick of adding 1 to a palindrome work

amrithapatil
Автор

bro please try to provide code in C++ also
More than 70% viewers are expecting c++ code

tekbssync
Автор

This is Java version of the problem

import java.util.* ;
import java.io.*;
public class Solution {

public static boolean isPalindrome(String s){
return s.equals(new
}

public static String add1(String s){
StringBuilder num= new StringBuilder();
int carry=1;

for(int i=s.length()-1; i>=0; i--){
int
num.append(sum%10);
carry=sum/10;
}

if(carry!=0){
num.append(carry);
}
return num.reverse().toString();
}
public static int compare(String left, String right){
int i = 0;
while (i < left.length() && i < right.length()) {
int leftValue =
int rightValue =

if (leftValue != rightValue) {
if (leftValue > rightValue) {
return -1;
} else {
return 1;
}
}

i++;
}

// If the loop completes, it means the common prefix is the same.
// Check the lengths to determine the result.
return Integer.compare(left.length(), right.length());
}
public static String handleOdd(String s){
int mid=s.length()/2;
String left= s.substring(0, mid);
String right=s.substring(mid+1);
if(compare(new StringBuilder(left).reverse().toString(), right)== -1){
return left+s.charAt(mid)+ new
}else {
left=left+s.charAt(mid);
left=add1(left);

return left + new
}
}

public static String handleEven(String s){
int mid=s.length()/2;
String left= s.substring(0, mid);
String right=s.substring(mid);
if(compare(new StringBuilder(left).reverse().toString(), right)== -1){
return left+ new
}else {

left=add1(left);
return left+ new
}
}
public static String solve(String number) {
int length=number.length();
// Write your code here.
if (length == 1 && !number.equals("9")) {
int num = Integer.parseInt(number);
return Integer.toString(num + 1);
}


number=add1(number);
}

String ans= new String();
if(number.length()%2==0){
ans= handleEven(number);
}else{
ans=handleOdd(number);
}

return ans;
}
}

Badalkumar-meok