Why it is not working for 4 digit numbers? | Sololearn: Learn to code for FREE!

+2

Why it is not working for 4 digit numbers?

https://code.sololearn.com/cF97lrKSgwkN/?ref=app

4/23/2021 11:41:29 AM

Atul [Inactive]

13 Answers

New Answer

+2

public class Program { static long function(long a){ long one=a*2; long two=a*3; int f=0; String concat =""+a+one; long i=(long)Long.parseLong(concat+two); while(i>0){ long d=i%10; for(int j=1;j<=9;j++){ if(j==(d)){ ++f; } } i/=10; } return f;} public static void main(String[] args) { long a=2019; System.out.println ((long)function(a)); if(function(a)==9){ System.out.println ("yes"); } else{ System.out.println ("no"); } } } //JaScript getting correct output through this

+2

// the parse methode do not work with addition of string and integer String concat =""+a+one; int ico=Integer.parseInt(concat); int i = ico + two;

+2

@JaScript "// the parse methode do not work with addition of string and integer " It works. Just do and see int two = 3; int i = Integer.parseInt(concat + two);

+2

🅰🅹 (Challenge Accepted) ok, interesting found. I tried this code now with long instead int and got the same error. Why?

+1

Because your string converted in more than 10 character for example "201940386057" which is beyond the size of Integer.

+1

🅰🅹 (Challenge Accepted) what can be done to avoid the error?

+1

JaScript I too used long instead of int . I typecasted the integers too but it's showing different errors too

+1

Atul In your above edited code, you are printing function (a) directly as well as comparing function (a) with 9 so here you are calling same method two time which will take two time processing to get same value. So instead of doing this, just do long num = function (a); System.out.println(num); if (num == 9) { System.out.println("Yes"); }

+1

Oh you mean the time complexity will be O(log n)², or greater than that for the time complexity?

0

It is showing no for that

0

Atul No need to convert all values in Long, just parse string value in long. And also don't print method, just store in a variable and use that variable (to avoid two time processing) https://code.sololearn.com/c6Uyn01XFe77/?ref=app

0

🅰🅹 (Challenge Accepted) What is two time processing?

0