previous greatest palindrome numbers before a given palindrome number - c++

The Question is:
You are given a number 'N' in the form of a string 'S', which is a palindrome. You need to find the greatest number strictly less than 'N' which is also a palindrome.
I tried to solve this question but is giving wrong answer for some test cases. can anyone help me to correct my code.
Below is my code:
string nextSmallerPalindrome(string &s)
{
int n = s.length();
string ans = "";
if(n == 1)
{
s[0]--;
return s;
}
if(s == "11")
{
return "9";
}
// For Handling odd cases
if(n % 2 != 0)
{
int idx = n / 2;
int diff = 0;
if(s[idx] == '0')
{
s[idx] = '9';
diff = 1;
}
else
{
s[idx]--;
}
idx--;
while(idx >= 0 && diff == 1)
{
if(s[idx] == '0')
{
s[idx] = '9';
idx--;
}
else
{
s[idx]--;
diff = 0;
break;
}
}
int i = 0;
while(i < n && s[i] == '0')
{
i++;
}
for(; i < n; i++)
{
ans = ans + s[i];
}
int new_n = ans.length();
int j = 0;
int k = new_n - 1;
while(j < k)
{
if(ans[j] == ans[k])
{
j++;
k--;
}
else
{
ans[k] = ans[j];
j++;
k--;
}
}
return ans;
}
else // For handling even cases
{
int idx = n / 2 - 1;
int diff = 0;
if(s[idx] == '0')
{
s[idx] = '9';
diff = 1;
}
else
{
s[idx]--;
}
idx--;
while(idx >= 0 && diff == 1)
{
if(s[idx] == '0')
{
s[idx] = '9';
idx--;
}
else
{
s[idx]--;
diff = 0;
break;
}
}
int i = 0;
while(i < n && s[i] == '0') // For ignoring Zeros from front of the string
{
i++;
}
for(; i < n; i++) //storing all the string s in new string ans after ignoring front 0
{
ans = ans + s[i];
}
int new_n = ans.length();
int j = 0;
int k = new_n - 1;
while(j < k) // checking and changing the last half into first half
{
if(ans[j] == ans[k])
{
j++;
k--;
}
else
{
ans[k] = ans[j];
j++;
k--;
}
}
return ans;
}
}
Input Format:
The first line of the input contains an integer T denoting the number of test cases.
The first and the only line of each test case contains a string 'S', denoting the number whose next smaller palindrome is to be found.
Test Case:
19
7
77
101
1001
1221
144441
3444444443
57855875
10000001
11
1
111
101
1001
11011
1110111
1190911
20002
10011001

Excuse me, but I will not fix the code. I will rather describe how I would solve it. If you like the idea, then you will have a very easy time doing it.
A palindrome has n digits. n may be pair or odd. The first n / 2 digits (rounded upwards) is a number. Get that number (cut down the digits in the second half) and subtract 1.
Look at the result and the number of its digits. If subtracting 1 decreases the number of digits of your half-number, then handle that accordingly. Should be easy if you have the right idea.

Seems like using the same expression for idx in even cases as odd cases works better.
(It still doesn't handle the case for 7, though.)
Change this:
else // For handling even cases
{
int idx = n / 2 - 1;
to this:
else // For handling even cases
{
int idx = n / 2;
Results:

here's a working code
#include <bits/stdc++.h>
string nextSmallerPalindrome(string &s) {
// Write your code here.
string ans = s ;
if(s=="11"){
return "9";
}
int c = 0 ;
if(s.size()%2==1){
int k = s.size()/2 ;
//string ans = s ;
while(k>=0){
if(k==0 && ans[k]=='1' && ans.size()>1){
// cout<<"hola\n";
ans = ans.substr(1,ans.size()-2);
ans=ans+'9';
break;
}
if(ans[k]-'0'!=0){
int temp = ans[k]-'0';
temp-= 1 ;
ans[k] = (temp) +'0';
ans[s.size()-1-k] = (temp) +'0';
return ans;
}
else{
ans[k] = (9) +'0';
ans[s.size()-1-k] = (9) +'0';
k--;
}
}
}
else{
int k = s.size()/2 ;
while(k>=0){
if(k==0 && ans[k]=='1' && ans.size()>1){
ans = ans.substr(1,ans.size()-2);
ans=ans+'9';
break;
}
if(ans[k]-'0'!=0){
int temp = ans[k]-'0';
temp-= 1 ;
ans[k] = (temp) +'0';
ans[s.size()-1-k] = (temp) +'0';
return ans ;
}
else{
ans[k] = (9) +'0';
ans[s.size()-1-k] = (9) +'0';
if(c==0){
c++;
k-=2;
}
else k--;
}
}
}
return ans ;
}

Related

how to check repetition of a certain number of zeroes following by an equal number of ones in a string?

If user enters a string, i want to make a function to check if a repetition of a certain number of zeroes follows an equal number of ones. Example: 001101 (correct), 01(correct),001010 (incorrect).
I've tried to store the string in 2 separate strings and compare the size but my 2nd while loop isn't stopping.
void check(string num) {
string st0 = "", st1 = "";
int n = num.length();
int k = 0;
while (k < n) {
int i = 0;
while (num[i] == num[i+1]) {
st0.push_back(num.back());
num.pop_back();
k++;
i++;
}
st0.push_back(num.back());
num.pop_back();
k++;
int j = 0;
while (num[j] == num[j+1]) {
st1.push_back(num.back());
num.pop_back();
k++;
j++;
}
st1.push_back(num.back());
num.pop_back();
k++;
if (st0.size() != st1.size()) {
cout << "incorrect \n";
}
st0.clear();
st1.clear();
}
}
Here you are testing the elements from left to right and you're adding to st0 the last elements from num string. For example:
"111010": after the first while num = "111" and st0 = "010"
while (num[i] == num[i+1]) {
st0.push_back(num.back());
num.pop_back();
k++;
i++;
}
And to fix you should add the first element tested not the last.
while( num[i] == num[i+1] && i+1 < n ){
st0.push_back(num[i]);
i++;
}
st0.push_back(num[i]);
i++;
You did the same mistake here
while (num[j] == num[j+1]) {
st1.push_back(num.back());
num.pop_back();
k++;
j++;
}
Fix:
while( num[i] == num[i+1] && i+1 < n){
st1.push_back(num[i]); // same here
i++;
}
st1.push_back(num[i]);
i++;
I used the same i here so that we continue looping the string from where we left off in the first while.
But there is a more optimized solution if you're interested:
void check(string num)
{
int cnt0 = 0 , cnt1 = 0;
int n = num.length();
int i=0;
bool test = true ;
while (i<n){
/* we can use two variables and increment them instead of strings and compare them cauz we are not using the elements we just need the size of them */
while( num[i] == num[i+1] && i+1 < n ){
cnt0++ ;
i++;
}
cnt0++;
i++;
while( num[i] == num[i+1] && i+1 < n) {
cnt1++;
i++;
}
cnt1++;
i++ ;
if ( cnt1 != cnt0 ){
test = false ;
break;
}
cnt1 = 0;
cnt0 = 0;
}
if( test ) {
cout << "correct \n" ;
}
else {
cout << "incorrect \n" ;
}
}
In the above algorithm, as you can see, you only need the length of the string st0 and st1 so you could just use 2 int variables and test the difference between the two, this way is more memory efficient as well as slightly faster run times.

How to find the nearest prime number?

The goal of this method is to find the nearest prime number less than the value passed. Any value less than 3 is undefined behavior so I just return -1. The issue lies when I pass in values 5, 20, 100, and 1, this is the output:
Passed in 5 Expected 3: 5
Passed in 20 Expected 19: 19
Passed in 100 Expected 97: 97
Passed in 1 Expected -1: -1
Here is the method:
int Hash::nearestPrime(int num){
if(num <= 3){
return -1;
}
for(int i = 2; i < num; i++){
if(num == 3){
return 3;
}
if(num % i == 0 && num != i){
num--;
i = 2;
}
}
return num;
}
You could loop and decrement until the next one is found, by having a function determine if a value is prime.
#include <iostream>
int prime (int n)
{
int i;
for (i = 2; i < n; i++)
if (n % i == 0) return 0;
return 1;
}
int run (unsigned int n)
{
while (!prime (--n));
return n;
}
int main ()
{
std::cout << run (3);
return 0;
}
Your method is right but with simple mistake.
Assign i = 1 instead of i = 2 inside the if condition. Because if you assign 2 then for i++ next checking value will start from 3. But you need to check it from 2.
int Hash::nearestPrime(int num){
if(num <= 3){
return -1;
}
for(int i = 2; i < num; i++){
if(num == 3){
return 3;
}
if(num % i == 0 && num != i){
num--;
// mistake in this line
i = 1;
}
}
return num;
}

Make palindromic string non-palindromic by rearranging its letters

Q: Make palindromic string non-palindromic by rearranging its letters.
I just want to know why my solution is failing (wrong answer) for some test cases when i submit the code. I am sure there is an easy solution such as sorting the whole string?
void makeNonPalindrome(string& s)
{
bool ans = false;
int l = s.length();
if(l % 2 == 0)
{
for(int i = l/2; i < l; i++)
{
if(s[l/2 - 1] != s[i])
{
swap(&s[l/2 - 1],&s[i]);
ans = true;
break;
}
}
if(!ans)
{
for(int i = 0; i < l/2-1; i++)
{
if(s[l/2 - 1] != s[i])
{
ans = true;
break;
}
}
}
}
else
{
for(int i = l/2 + 1; i < l; i++)
{
if(s[l/2 - 1] != s[i])
{
swap(&s[l/2 - 1],&s[i]);
ans = true;
break;
}
}
if(!ans)
{
for(int i = 0; i < l/2-1; i++)
{
if(s[l/2 - 1] != s[i])
{
ans = true;
break;
}
}
}
if(!ans)
{
if(s[l/2] != s[0])
{
swap(&s[l/2],&s[0]);
ans = true;
}
}
}
if(ans)
cout << s << '\n';
else
cout << -1 << '\n';
}
Rearranging a palindrome so it become non-palindromic can be done quite fast, by simply trying to swap two adjacent letters in the string if they are different.
For instance, in 'bob', you'd need to find the first distinct adjacent letters (that is b and o in our case), and swap them. The result would then be 'obb', which is not a palindrome.
void makeNonPalindrome(std::string& s) {
char tmp;
for (unsigned i = 0; i < s.length() - 1; i++) {
if (s[i] != s[i+1]) { // then swap s[i] and s[i+1]
tmp = s[i];
s[i] = s[i+1];
s[i+1] = tmp;
std::cout << s << '\n';
return;
}
}
std::cout << -1 << '\n';
}
This is a simpler way to make a palindrome non palindromic.
NB: this function assumes that the input is indeed a palindrome, so if you feed it a string like 'oob', it will output 'bob' which is a palindrome.
Given the input palindrome string s you can just use find_first_not_of to determine if the string can be rearranged into a non-palindrome at all, and if so what characters should be swapped to do this. For example:
const auto foo = s.find_first_not_of(s[0], 1);
If foo == string::npos it means that there isn't a possible non-palindrome rearrangement. Otherwise swap(s[0], s[foo]) will break the palindrome.
Live Example

Checking if a number is a Simber

A simber is defined as a positive integer in which any odd digit, if present, occurs an odd number of times, 1414414 is a simber. 4 is even and it appears four time and 1 is odd and it appears 3 times.
Here are my variables:
int is_simber(int n)
{
int numberOfTimes = 0, length = 1, x = n;
bool answer;
vector <int> nmbrs = vector <int>();
//get how many digits are in the integer
do
{
x /= 10;
length++;
}
while(x != 0)
//get the digits in the integer
for(int i = 0; i<length; i++)
{
nmbrs.push_back(((n/10^i) % 10);
}
//checking how many times a digit occurs and also testing to see if the digits
//meet the requirements
for(int i = 0; i<length; i++)
{
for(int j = 0; j<length; j++)
{
if (nmbrs.at(i) == nmbrs.at(j))
{
numberOfTimes++;
}
}
if (nmbrs.at(i) % 2 == 0 && numberOfTimes % 2 == 0)
{
answer = true;
}
else if(nmbrs.at(i) % 2 == 1 && numberOfTimes % 2 == 1)
{
answer = true;
}
else if(nmbrs.at(i) % 2 == 0 && numberOfTimes % 2 == 1)
{
answer = false;
break;
}
else if(nmbrs.at(i) % 2 == 1 && numberOfTimes % 2 == 0)
{
answer = false;
break;
}
}
return answer;
}
Your code has compilation errors.
^ is Binary XOR Operator. You can't expect it to produce pow(10,i). So replace nmbrs.push_back(((n/10^i) % 10); with nmbrs.push_back(((n/pow(10,i)) % 10);
When I was able to remove the compilation errors, i realized your code has logical errors too. As your given definition of simber, int is_simber(int n) should be fair and simple.
bool is_simber( int n )
{
if ( n<0 ) return false; // simber is a positive integer
int digitCount[10] = {}; // initializing all with 0;
// digitCount array holds the number of occurance of a digit
// so d[1] holds the number of times 1 occurred
// d[2] holds the number of times 2 occurred and so on ...
while( n ){
int d = n%10;
n /= 10;
digitCount[d]++;
}
// we just have to check
// any odd digit, if present, occurs an odd number of times
for( int i=1; i<=9; i= i+2) // Attention i=i+2, to iterate over just odd numbers
{
if( digitCount[i] != 0 && digitCount[i]%2 == 0 ) return false;
}
return true;
}
My definition of is_simber
inline bool is_even(int n) { return n % 2 == 0; }
bool is_simber(int n) {
if (n < 0) return false;
int digits[10] = {0};
for (; n; n /= 10) ++digits[n % 10];
for (int i = 0; i < 10; i += 2)
if (!is_even(digits[i]) && is_even(digits[i + 1])) return false;
return true;
}
LIVE DEMO

Adding binary numbers in C++

How would I add two binary numbers in C++? What is the correct logic?
Here is my effort, but it doesn't seem to be correct:
#include <iostream>
using namespace std;
int main()
{
int a[3];
int b[3];
int carry = 0;
int result[7];
a[0] = 1;
a[1] = 0;
a[2] = 0;
a[3] = 1;
b[0] = 1;
b[1] = 1;
b[2] = 1;
b[3] = 1;
for(int i = 0; i <= 3; i++)
{
if(a[i] + b[i] + carry == 0)
{
result[i] = 0;
carry = 0;
}
if(a[i] + b[i] + carry == 1)
{
result[i] = 0;
carry = 0;
}
if(a[i] + b[i] + carry == 2)
{
result[i] = 0;
carry = 1;
}
if(a[i] + b[i] + carry > 2)
{
result[i] = 1;
carry = 1;
}
}
for(int j = 0; j <= 7; j++)
{
cout<<result[j]<<" ";
}
system("pause");
}
Well, it is a pretty trivial problem.
How to add two binary numbers in c++. what is the logic of it.
For adding two binary numbers, a and b. You can use the following equations to do so.
sum = a xor b
carry = ab
This is the equation for a Half Adder.
Now to implement this, you may need to understand how a Full Adder works.
sum = a xor b xor c
carry = ab+bc+ca
Since you store your binary numbers in int array, you might want to understand bitwise operation.
You can use ^ for XOR,| operator for OR, & operator for AND.
Here is a sample code to calculate the sum.
for(i = 0; i < 8 ; i++){
sum[i] = ((a[i] ^ b[i]) ^ c); // c is carry
c = ((a[i] & b[i]) | (a[i] & c)) | (b[i] & c);
}
Since you were asking about C++, you deserve a C++ answer. Use bitsets:
#include <bitset>
#include <iostream>
int main() {
std::bitset<5> const a("1001");
std::bitset<5> const b("1111");
// m here is a mask to extract the lsb of a bitset.
std::bitset<5> const m("1");
std::bitset<5> result;
for (auto i = 0; i < result.size(); ++i) {
std::bitset<5> const diff(((a >> i)&m).to_ullong() + ((b >> i)&m).to_ullong() + (result >> i).to_ullong());
result ^= (diff ^ (result >> i)) << i;
}
std::cout << result << std::endl;
}
This works for arbitrarily long bit sets.
You could use "Bitwise OR" operation to reduce the code since
1 or 1 = 1
1 or 0 = 1
0 or 1 = 1
0 or 0 = 0
You could also convert both number to decimal sum and them go back to binary again.
Converting decimal to binary
int toBinary (unsigned int num, char b[32])
{
unsigned int x = INT_MIN; // (32bits)
int i = 0, count = 0;
while (x != 0)
{
if(x & num) // If the actual o bit is 1 & 1 = 1 otherwise = 0
{
b[i] = '1';
count++;
}
else b[i] = '0';
x >>=1; // pass to the left
i++;
}
return count;
}
There is a bug :
if(a[i]+b[i]+carry==1)
{
result[i]=1;
carry=0;
}
Also u might want to print in reverse
for(int j=6; j>=0; j--)
{
cout<<result[j]<<" ";
}
Your arrays are one item too small for your indexing.
int a[3] only has 3 elements, so a[3] = 1 is invalid (it has undefined behaviour) since it's accessing the 4th element, which doesn't exist.
Likewise for the other arrays.
That means that the whole program has undefined behaviour, i.e. it can do anything or nothing at all.
(What's probably happening in your case is that writing outside the arrays is overwriting the other variables.)
You're also not initialising the result array, so its content is just some random data.
Since you only update 4 of its elements but print all of them (and more), the output will be random data as well.
Following were the errors in your code and fixed code is also below"
int a[] was of size 3 so it cannot store at the 3rd index. use int a[4].
if(a[i]+b[i]+carry==1) wrong values were assigned in this check update result[i]=1; carry=0.
The sequence of checks is reversed.
The last carry was not stored in the result.
The addition result stored in the result array was in reverse order so printed it in reverse.
here is the working piece of code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a[4];
int b[4];
int carry=0;
int result[5];
a[0]=1;
a[1]=0;
a[2]=0;
a[3]=1;
b[0]=1;
b[1]=1;
b[2]=1;
b[3]=1;
for(int i=0; i<4; i++)
{
if(a[i]+b[i]+carry==3)
{
result[i]=1;
carry=1;
}
if(a[i]+b[i]+carry==2)
{
result[i]=0;
carry=1;
}
if(a[i]+b[i]+carry==1)
{
result[i]=1;
carry=0;
}
if(a[i]+b[i]+carry==0)
{
result[i]=0;
carry=0;
}
}
result[4]=carry;
for(int j=4; j>=0; j--)
{
cout<<result[j];
}
cout<<endl;
return 0;
}
#include <stdio.h>
int main()
{
long binary1, binary2;
int i = 0, remainder = 0, sum[20];
printf("Enter the first binary number: ");
scanf("%ld", &binary1);
printf("Enter the second binary number: ");
scanf("%ld", &binary2);
while (binary1 != 0 || binary2 != 0)
{
sum[i++] =(binary1 % 10 + binary2 % 10 + remainder) % 2;
remainder =(binary1 % 10 + binary2 % 10 + remainder) / 2;
binary1 = binary1 / 10;
binary2 = binary2 / 10;
}
if (remainder != 0)
sum[i++] = remainder;
--i;
printf("Sum of two binary numbers: ");
while (i >= 0)
printf("%d", sum[i--]);
getch();
return 0;
}
Repeatedly do
(x, y) <- ((x & y) << 1, x ^ y)
until x is 0. y is the answer.
you should do this
for(int i = 3; i >= 0; i--)
{
if(a[i] + b[i] + carry == 0)
{
result[i] = 0;
carry = 0;
}
else if(a[i]+b[i]+carry==1)
{
result[i]=1;
carry=0;
}
else if(a[i] + b[i] + carry == 2)
{
result[i] = 0;
carry = 1;
}
else if(a[i] + b[i] + carry > 2)
{
result[i] = 1;
carry = 1;
}
printf("%d",result[i]);
}
A non-conventional solution, but it works:
int main() {
int A[] = { 0, 0, 0, 1, 1, 0, 1, 0};
int B[] = { 0, 0, 0, 0, 1, 1, 0, 0};
int size = sizeof(A)/sizeof(*A);
int C[size+1];
int t = 0;
for(int i = size-1; i > -1; i--){
C[i+1] = A[i]+B[i]+t;
t = C[i+1]/2;
C[i+1] %= 2;
}
C[0] = t;
}
What if their sizes are not the same? Also, you would want to allow the user to input the binary numbers (in this case representing integers) as integers and not as elements of arrays. Here is a piece of code that accomplishes those :-)
#include <iostream>
using namespace std;
// Add two numbers in binary
void sumBinary(int num1, int num2, int* sum12){
int mod1 = 0;
int mod2 = 0;
int carry = 0;
int factor = 1;
int flag = 0;
*sum12 = 0;
while (!flag){
mod1 = num1 % 10;
mod2 = num2 % 10;
num1 /= 10;
num2 /= 10;
if ((carry + mod1 + mod2) == 2){
*sum12 += 0;
carry = 1;
}
else if ((carry + mod1 + mod2) == 3){
*sum12 += factor;
carry = 1;
}
else if ((carry + mod1 + mod2) == 0){
*sum12 += 0;
carry = 0;
}
else{
*sum12 += factor;
carry = 0;
}
factor *= 10;
if ((num1 == 0) && (num2 == 0)){
*sum12 += carry*factor;
flag = 1; }
}
}
void main(){
int num1, num2, sum12;
cout << "Enter the first binary integer number: ";
cin >> num1;
cout << "Enter the second binary integer number: ";
cin >> num2;
sumBinary(num1, num2, &sum12);
cout << "The sum in binary form is :" << sum12 << endl;
}
A simple way :
int getBit(string s, int index)
{
if(index >= 0) return (s[index] - '0');
else return 0;
}
string addBinary(string a, string b)
{
if(a.size() > b.size()) while(a.size() > b.size()) b = "0" + b;
else if(b.size() > a.size()) while(b.size() > a.size()) a = "0" + a;
int l = max(a.size()-1, b.size() - 1);
string result = "";
int s=0;
while(l>=0 || s==1)
{
s += getBit(a, l) + getBit(b, l) ;
result = char(s % 2 + '0') + result;
s /= 2;
l--;
}
return result;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
int num1=12, num2=45, sum=0;
bool b1, b2, carry=0;
for(int i=0;i<32;i++){
b1=( 1<<i ) & num1;
b2=( 1<<i ) & num2;
sum = (b1 ^ b2 ^ carry) ? sum ^ (1<<i) : sum;
carry = ((b1 & b2) | (b1 & carry) | (b2 & carry));
}
cout<<sum;
return 0;
}
Easy to understand code
Add Two Binary Numbers (input datatype = int)
#include <iostream>
using namespace std;
int power(int a, int b)
{
int ans = 1;
while (b)
{
ans *= a;
b--;
}
return ans;
}
int main()
{
int n1, n2, carry = 0, ans = 0, rem1, rem2, remsum = 0, i;
cout << "Enter First Number : ";
cin >> n1;
cout << "Enter Second Number : ";
cin >> n2;
for (i = 0; n1 != 0 || n2 != 0; i++, n1 /= 10, n2 /= 10)
{
rem1 = n1 % 10;
rem2 = n2 % 10;
remsum = rem1 + rem2 + carry;
if (remsum == 2)
{
carry = 1;
remsum = 0;
}
else if (remsum == 3)
{
carry = 1;
remsum = 1;
}
else if (remsum == 0)
{
carry = 0;
remsum = 0;
}
else if (remsum == 1)
{
carry = 0;
remsum = 1;
}
ans = remsum * power(10, i) + ans;
}
ans = carry * power(10, i) + ans;
cout << ans;
return 0;
}