Program to print maximum number of characters by copy pasting in n steps in Python?

PythonServer Side ProgrammingProgramming

Suppose we have a number n; we have to find the maximum number of characters we can enter using n operations where each operation is like

  • Inserting the character "x".

  • Copy all characters.

  • Paste

So, if the input is like n = 12, then the output will be 81.

To solve this, we will follow these steps

  • if n <= 4, then

    • return n

  • v := 6, x := 3, i := 5, j := 0

  • while i is not same as n, do

    • v := v + x

    • i := i + 1, j := j + 1

    • if j is divisible by 3, then

      • x := integer of (x * 1.5)

    • otherwise when j is not divisible by 3, then

      • do nothing

    • otherwise,

      • x := x * 2

  • return v

Let us see the following implementation to get better understanding

Example

 Live Demo

class Solution:
   def solve(self, n):
      if n <= 4:
         return n

      v = 6
      x = 3
      i = 5
      j = 0
      while i != n:
         v += x
         i += 1
         j += 1

         if j % 3 == 0:
            x = int(x * 1.5)
         elif j % 3 == 1:
            pass
         else:
            x *= 2

      return v

ob = Solution()
n = 12
print(ob.solve(n))

Input

12

Output

81
raja
Updated on 10-Nov-2020 09:00:23

Advertisements