By kbadrinath, history, 6 weeks ago, Hey guys! Today I'm back with another TAS. We're going to be looking at the problem Sequence with Digits.
The problem statement is above, if you want to check it out.

Let's just try some testing with the first testcase. We go from 487 1, to 487 2. What this is supposed to return is
519.
The reason this happens is because the "rule" we are supposed to follow is that we are supposed to take the max digit and the min digit of a number, multiply them, and add them to the number to get the next value in the sequence.
The twist to this problem is that we are not always given the positions in an order. We could jump from 487 1 to 487 6, and we would have to print the correct value for both. That makes the problem a little harder, but we'll manage. Let's create a function for this, and call it kval. This function will basically grab the input and return all the correct answers. Now is time for the logic. My first thought here is to run through the number and get the max and min digits. This will be pretty simple. We'll create a variable num = 1, that will be the equivalent of a for loop, but in while loop format. We'll do something like

 public static long kval(int val, int k) { long num = 1; while(num<=k-1){ }
After we have this we'll create three new variables, tmp, l(max), and s(min). L and s will act as our trackers for the max and min digit, while tmp will just be a way to get val without having to change val itself. It'll look like this.
 int tmp=0,l=0,s=10; (We do s=10 because the max value of digit is 9). tmp=val;
Then, what we have to do is run through the number using a while loop and tracking every digit. This sort of thing is very common, and you can find it online, but what we have to do is find the minimum and the maximum digits, which is easy to implement once you have the function down. Here's the code.
 while(tmp>0) {//10^18 with sum effects  long p = tmp%10;  s = Math.min(s,p);  l = Math.max(l,p);  tmp/=10;  if(s==0) {  break;  } }
You may be wondering why we are breaking if s=0. Remember that in the problem statement the next number is equal to the current number plus (the max digit of the current number times the min digit of the current number), but since any number multiplied by 0 is 0, all we'd be doing is adding 0 to the number, keeping the number anyways. So, outside of the loop, we could just do something like this:
 if(s==0) {  return val; }
Then, at the end of this, we simply use the rule and add (l*s) to val. This works because, remember that the input could ask us for any index in the sequence, rather than staying at a constant rate of the index increasing by one, and so, using our first while loop, we just do val+=(l*s), so that we'll keep on adding the values to val until we reach the correct index in the sequence. Then add the end of this we simply return val. Again, as I said in my last post, if you didn't understand something in my explanation, my code explains it better, and you'll understand it better. Finally, in our main method, we just do this(simply getting input and running function on it):
 StringTokenizer st = new StringTokenizer(in.readLine()); int first = Integer.parseInt(st.nextToken()); int second = Integer.parseInt(st.nextToken()); System.out.println(kval(first,second));
Again, here's my code if there was any misunderstanding.
87390724

Thanks for tuning in.  Comments (0)