Simple C++ and queue

amazon
Tags: #<Tag:0x00007f18261b3100>

#1

string Solution::multiple(int n) {

if(n==1)
return "1";
queue<long long int> q;
q.push(1);
long long int i=0,j=0;
long long int ans=INT_MAX;
string a="",b="",ans1="";

while(!q.empty()&& b.length()!=10)
{
    long long int k =q.front();
    q.pop();
    
     a=to_string(k)+"1";
     b=to_string(k)+"0";
   long long int x=stoi(a);
   long long int y=stoi(b);
    
    if(x%n==0)
    {
     if(x<ans&& x>=n)
     {
         ans=x;
         ans1=to_string(ans);
     }
    }
    if(y%n==0)
    {
        if(y<ans && y>=n)
        {
            ans=y;
            ans1=to_string(ans);
        }
    }
   
   q.push(x);
   q.push(y);
}

return ans1;

}


#2

best solution thanks brother


#4

I am getting a runtime error for the code. The answers, however are correct. Could someone help debug the runtime error.

#include<bits/stdc++.h>
using namespace std;
long long n;
string ans_num;
string num1;
string num2;
long long int s;
queue<long long int> myqueue;
long long int element;
void check_div()
{
    s=myqueue.front();
    num1=to_string(s)+"0";
    num2=to_string(s)+"1";
    if(num1.length()<=10)
    {
        if((stoll(num1))%n==0)
        {
            ans_num=num1;
            return;
        } 
        myqueue.push(stoll(num1));
        
    }
    if(num2.length()<=10)
    {
        if((stoll(num2))%n==0)
        {
            ans_num=num2;
            return;
        }
        myqueue.push(stoll(num2));
    }
    myqueue.pop();
    check_div();
}
string Solution::multiple(int A) {
    n=A;
    while(!myqueue.empty())
    {
        myqueue.pop();
    }
    ans_num="";
    if(n==1)
    {
        return "1";
    }
    myqueue.push(1);
    check_div();
    return ans_num;
}

#5

why have you considered length of binary string to be less than 10 ?


#6

@Akash_Krishnan your Solution gives TLE , why do you post such solutions?