yesshubham
BAN USERHi Acharya,
This is to calculate the next number, just like after 1 comes 2, after 2 comes 3 and so on.
Only difference is that you need to increase the middle element first to find the next number i.e. for 455554 next number is 544445.
So we started from middle and went till we got a number which is 4, and we convert it to 5 and again go till middle and set everything to 4. i.e. 455554>555555>544445
(Now to make sense of it in our decimal number system.
9899>9999>9900)
What about the case when we have all 5 then we need to add 4 to beginning and end and keep everything else as 4 i.e. 5555>455554>444444
(Similar to our decimal system 9999>19999>10000)
Below is the code:
The complexity is n*(time taken to generate the next item, which is proportional to the string length)
( 2 + 2 + 4 + 4 + 4 + 4 + 8 + 8 + 8 + 8.. + (n / log(n))) n times
let's convert every string length to n so this will be proportional to O(n^2)
/******************************************************************************
Online Java Compiler.
Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.
*******************************************************************************/
public class Main
{
private static String find(String str, int n){
StringBuilder sb = new StringBuilder(str);
while(n > 0){
int mid, pos;
mid = pos = sb.length() / 2;
//go out till you find a 4 and update to 5
while(pos < sb.length() && sb.charAt(pos) != '4'){
pos++;
}
if(pos < sb.length()){
sb.setCharAt(pos, '5');
sb.setCharAt(mid  (pos  mid)  1, '5');
}
//if no such 4 found means we need to add 4 to beginning and end and reset all to '4'
else{
for(int i = 0; i < sb.length(); i++){
sb.setCharAt(i, '4');
}
sb = new StringBuilder("4"+sb.toString()+"4");
continue;
}
//go in till you find mid and convert everything to 4
pos;
while(pos >= mid){
sb.setCharAt(pos, '4');
sb.setCharAt(mid  (pos  mid)  1, '4');
pos;
}
}
return sb.toString();
}
public static void main(String[] args) {
for(int i = 1; i <40; i++){
System.out.println(find("44", i));
//System.out.println(Integer.toBinaryString(i));
}
}
}
Regards,
Shubham
I was trying this today and came up with recursive solution for this
you can check the logic:
start with the leftmost digit and find the number of 2 with the leftmost digit and the
remainder has to be calculated again to find number of 2, I also added the iterative version to check the correct answer
so we can verify it is correct(as every time the number is reduced to 1/10 of the number it is log(n) solution):
#include <iostream>
#include <cmath>
using namespace std;
long calcD(long rem){
long count = 0;
while(rem){
count++;
rem /= 10;
}
return count;
}
long q(long m, long z){
long total = 1;
long i = 1;
while(i < z){
total = total * 10 + pow(10, i);
i++;
}
total = total * m;
if(m > 2)
total += pow(10, z);
return total;
}
long pow10L(long p){
long ans = 1;
while(p){
ans *= 10;
}
return ans;
}
long f(long n, long d){
if(d == 1)
return n >= 2 ? 1 : 0;
long msd = n / pow10L(d  1);
long rem = n % pow10L(d  1);
//cout << "method f" << n<< " msd " << msd << " rem "<< rem << endl;
long total = 0;
if(rem == 0)
total += q(msd, d  1) + (msd == 2 ? 1 : 0);
else if(msd > 2)
total += q(msd, d1) + f(rem, calcD(rem));
else if(msd == 2)
total += q(msd, d  1) + rem + 1 + f(rem, calcD(rem));
else
total += q(msd, d  1) + f(rem, calcD(rem));
//cout << "method f" << n << " =" << total << endl;
return total;
}
long find2(long n){
int count = 0;
while(n){
if(n % 10 == 2)
count++;
n /= 10;
}
return count;
}
long iterative(long n){
long total = 0;
for(long i = 2; i <= n; i++){
total += find2(i);
}
return total;
}
int main() {
cout << "enter the n: ";
long n;
cin >> n;
cout << "The answer is from method: " << f(n, calcD(n)) << endl;
cout << "The answer is from iterative: " << iterative(n) << endl;
return 0;
}

yesshubham
May 15, 2017 Open Chat in New Window
We need a node of bfs like
 yesshubham August 21, 2019Node{
int row, col;
boolean flipped;
}
and visited boolean will be a 3D array
boolean visited[][][] = new boolean[2][rows][cols];
If we are on a node we will visit all the neighbors which are not visited and mark the visited [0][row][col] as done and also visit all the neighbours which are zero and push these node as flipped in queue. When visiting these nodes we need to check if visited[1][row][col] is done or not.
At the end we will reach with the minimum steps to reach the destination.
Note that the same problem can be done for k flips allowed as well, we will just need to increase the first dimension to k instead of 2 and similarly check for the destination.