I've to print a number which is a huge
sequence of 5's and 3's (upto 100,000 ints). Instead of storing it in array, I just kept
their count in noOfThrees
and
noOfFives
.
For
simplicity call this number x
.
.
Since I have to print the largest number in
the sequence, x
will be initially 5's and then followed with
3's (I have working logic to print if there's no 5's or no
3's)
To print the number, I am using
a for loop like this :
for(int
i=0; i System.out.print(5);
for(int i=0;
i
System.out.print(3);
But
if x
is a 100,000 long int number, it takes about 4-5sec to
print it on console which is not
desirable.
My
take:
- If the
noOfFives
is even, then print55
in
the for loop which increases the performance by x2 and increment the loop by two,
else - Use the same for loop as above. Same goes for
noOfThrees
.
But
the problem here is if it's odd, it will again end up printing in steps of 1. How do I
effectively print this sequence?
No comments:
Post a Comment