# Count of a, b & c after n seconds for given reproduction rate

Given three different alphabets ‘a’, ‘b’ and ‘c’ with a certain rule that after every 2 seconds every ‘a’ changes to a ‘b’, after every 5 seconds every ‘b’ changes to one ‘c’ and after every 12 seconds every ‘c’ changes again into two ‘a’s
Starting with one ‘a’, the task is to find the final count of a, b and c after given n seconds.
Examples:

Input: n = 2
Output: a = 0, b = 1, c = 0
Initially a = 1, b = 0, c = 0
At n = 1, nothing will change
At n = 2, all a will change to b i.e. a = 0, b = 1, c = 0
Input: n = 72
Output: a = 64, b = 0, c = 0

Approach: It can be observed that the values of a, b and c will form a pattern after every 60 seconds (which is the LCM of 2, 5 and 12) as follows:

• At n = 60 -> a = 321, b = 0, c = 0
• At n = 120 -> a = 322, b = 0, c = 0
• At n = 180 -> a = 323, b = 0, c = 0 and so on.

If n is a multiple of 60 then calculate the result from the above observation else calculate the result for the multiple of 60 which is nearest to n say x and then update the result for the seconds from x + 1 to n.
Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach` `#include ` `using` `namespace` `std;` `#define ull unsigned long long`   `// Function to print the count of` `// a, b and c after n seconds` `void` `findCount(``int` `n)` `{` `    ``ull a = 1, b = 0, c = 0;`   `    ``// Number of multiples of 60 below n` `    ``int` `x = n / 60;` `    ``a = (ull)``pow``(32, x);`   `    ``// Multiple of 60 nearest to n` `    ``x = 60 * x;`   `    ``for` `(``int` `i = x + 1; i <= n; i++) {`   `        ``// Change all a to b` `        ``if` `(i % 2 == 0) {` `            ``b += a;` `            ``a = 0;` `        ``}`   `        ``// Change all b to c` `        ``if` `(i % 5 == 0) {` `            ``c += b;` `            ``b = 0;` `        ``}`   `        ``// Change each c to two a` `        ``if` `(i % 12 == 0) {` `            ``a += (2 * c);` `            ``c = 0;` `        ``}` `    ``}`   `    ``// Print the updated values of a, b and c` `    ``cout << ``"a = "` `<< a << ``", "``;` `    ``cout << ``"b = "` `<< b << ``", "``;` `    ``cout << ``"c = "` `<< c;` `}`   `// Driver code` `int` `main()` `{` `    ``int` `n = 72;` `    ``findCount(n);`   `    ``return` `0;` `}`

## Java

 `// Java implementation of the approach` `class` `GFG` `{` `    `  `// Function to print the count of` `// a, b and c after n seconds` `static` `void` `findCount(``int` `n)` `{` `    ``long` `a = ``1``, b = ``0``, c = ``0``;`   `    ``// Number of multiples of 60 below n` `    ``int` `x = n / ``60``;` `    ``a = (``long``)Math.pow(``32``, x);`   `    ``// Multiple of 60 nearest to n` `    ``x = ``60` `* x;`   `    ``for` `(``int` `i = x + ``1``; i <= n; i++) ` `    ``{`   `        ``// Change all a to b` `        ``if` `(i % ``2` `== ``0``) ` `        ``{` `            ``b += a;` `            ``a = ``0``;` `        ``}`   `        ``// Change all b to c` `        ``if` `(i % ``5` `== ``0``) ` `        ``{` `            ``c += b;` `            ``b = ``0``;` `        ``}`   `        ``// Change each c to two a` `        ``if` `(i % ``12` `== ``0``)` `        ``{` `            ``a += (``2` `* c);` `            ``c = ``0``;` `        ``}` `    ``}`   `    ``// Print the updated values of a, b and c` `    ``System.out.println(``"a = "` `+ a + ``", b = "` `+` `                           ``b + ``", c = "` `+ c);` `}`   `// Driver code` `public` `static` `void` `main (String[] args) ` `{` `    ``int` `n = ``72``;` `    ``findCount(n);` `}` `}`   `// This code is contributed by mits`

## Python3

 `# Python3 implementation of the approach`   `# Function to print the count of` `# a, b and c after n seconds` `import` `math` `def` `findCount(n):` `    ``a, b, c ``=` `1``, ``0``, ``0``;`   `    ``# Number of multiples of 60 below n` `    ``x ``=` `(``int``)(n ``/` `60``);` `    ``a ``=` `int``(math.``pow``(``32``, x));`   `    ``# Multiple of 60 nearest to n` `    ``x ``=` `60` `*` `x;`   `    ``for` `i ``in` `range``(x ``+` `1``, n ``+` `1``):`   `        ``# Change all a to b` `        ``if` `(i ``%` `2` `=``=` `0``):` `            ``b ``+``=` `a;` `            ``a ``=` `0``;`   `        ``# Change all b to c` `        ``if` `(i ``%` `5` `=``=` `0``):` `            ``c ``+``=` `b;` `            ``b ``=` `0``;`   `        ``# Change each c to two a` `        ``if` `(i ``%` `12` `=``=` `0``):` `            ``a ``+``=` `(``2` `*` `c);` `            ``c ``=` `0``;`   `    ``# Print the updated values of a, b and c` `    ``print``(``"a ="``, a, end ``=` `", "``);` `    ``print``(``"b ="``, b, end ``=` `", "``);` `    ``print``(``"c ="``, c);` `    `  `# Driver code` `if` `__name__ ``=``=` `'__main__'``:` `    ``n ``=` `72``;` `    ``findCount(n);`   `# This code is contributed ` `# by 29AjayKumar`

## C#

 `// C# implementation of the approach` `using` `System;`   `class` `GFG` `{` `    `  `// Function to print the count of` `// a, b and c after n seconds` `static` `void` `findCount(``int` `n)` `{` `    ``long` `a = 1, b = 0, c = 0;`   `    ``// Number of multiples of 60 below n` `    ``int` `x = n / 60;` `    ``a = (``long``)Math.Pow(32, x);`   `    ``// Multiple of 60 nearest to n` `    ``x = 60 * x;`   `    ``for` `(``int` `i = x + 1; i <= n; i++) ` `    ``{`   `        ``// Change all a to b` `        ``if` `(i % 2 == 0) ` `        ``{` `            ``b += a;` `            ``a = 0;` `        ``}`   `        ``// Change all b to c` `        ``if` `(i % 5 == 0) ` `        ``{` `            ``c += b;` `            ``b = 0;` `        ``}`   `        ``// Change each c to two a` `        ``if` `(i % 12 == 0)` `        ``{` `            ``a += (2 * c);` `            ``c = 0;` `        ``}` `    ``}`   `    ``// Print the updated values of a, b and c` `    ``Console.WriteLine(``"a = "` `+ a + ``", b = "` `+ b + ``", c = "` `+ c);` `}`   `// Driver code` `static` `void` `Main()` `{` `    ``int` `n = 72;` `    ``findCount(n);` `}` `}`   `// This code is contributed by mits`

## PHP

 ``

## Javascript

 ``

Output:

`a = 64, b = 0, c = 0`

Time Complexity: O(n)

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next