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++
#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
void findCount( int n)
{
ull a = 1, b = 0, c = 0;
int x = n / 60;
a = (ull) pow (32, x);
x = 60 * x;
for ( int i = x + 1; i <= n; i++) {
if (i % 2 == 0) {
b += a;
a = 0;
}
if (i % 5 == 0) {
c += b;
b = 0;
}
if (i % 12 == 0) {
a += (2 * c);
c = 0;
}
}
cout << "a = " << a << ", " ;
cout << "b = " << b << ", " ;
cout << "c = " << c;
}
int main()
{
int n = 72;
findCount(n);
return 0;
}
|
Java
class GFG
{
static void findCount( int n)
{
long a = 1 , b = 0 , c = 0 ;
int x = n / 60 ;
a = ( long )Math.pow( 32 , x);
x = 60 * x;
for ( int i = x + 1 ; i <= n; i++)
{
if (i % 2 == 0 )
{
b += a;
a = 0 ;
}
if (i % 5 == 0 )
{
c += b;
b = 0 ;
}
if (i % 12 == 0 )
{
a += ( 2 * c);
c = 0 ;
}
}
System.out.println( "a = " + a + ", b = " +
b + ", c = " + c);
}
public static void main (String[] args)
{
int n = 72 ;
findCount(n);
}
}
|
Python3
import math
def findCount(n):
a, b, c = 1 , 0 , 0 ;
x = ( int )(n / 60 );
a = int (math. pow ( 32 , x));
x = 60 * x;
for i in range (x + 1 , n + 1 ):
if (i % 2 = = 0 ):
b + = a;
a = 0 ;
if (i % 5 = = 0 ):
c + = b;
b = 0 ;
if (i % 12 = = 0 ):
a + = ( 2 * c);
c = 0 ;
print ( "a =" , a, end = ", " );
print ( "b =" , b, end = ", " );
print ( "c =" , c);
if __name__ = = '__main__' :
n = 72 ;
findCount(n);
|
C#
using System;
class GFG
{
static void findCount( int n)
{
long a = 1, b = 0, c = 0;
int x = n / 60;
a = ( long )Math.Pow(32, x);
x = 60 * x;
for ( int i = x + 1; i <= n; i++)
{
if (i % 2 == 0)
{
b += a;
a = 0;
}
if (i % 5 == 0)
{
c += b;
b = 0;
}
if (i % 12 == 0)
{
a += (2 * c);
c = 0;
}
}
Console.WriteLine( "a = " + a + ", b = " + b + ", c = " + c);
}
static void Main()
{
int n = 72;
findCount(n);
}
}
|
PHP
<?php
function findCount( $n )
{
$a = 1; $b = 0; $c = 0;
$x = $n / 60;
$a = pow(32, $x );
$x = 60 * $x ;
for ( $i = $x + 1; $i <= $n ; $i ++)
{
if ( $i % 2 == 0)
{
$b += $a ;
$a = 0;
}
if ( $i % 5 == 0)
{
$c += $b ;
$b = 0;
}
if ( $i % 12 == 0)
{
$a += (2 * $c );
$c = 0;
}
}
echo ( "a = " . $a . ", b = " .
$b . ", c = " . $c );
}
$n = 72;
findCount( $n );
?>
|
Javascript
<script>
function findCount(n) {
var a = 1, b = 0, c = 0;
var x = parseInt(n / 60);
a = Math.pow(32, x);
x = 60 * x;
for (i = x + 1; i <= n; i++) {
if (i % 2 == 0) {
b += a;
a = 0;
}
if (i % 5 == 0) {
c += b;
b = 0;
}
if (i % 12 == 0) {
a += (2 * c);
c = 0;
}
}
document.write( "a = " + a + ", b = " + b + ", c = " + c);
}
var n = 72;
findCount(n);
</script>
|
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!
Last Updated :
22 Jun, 2022
Like Article
Save Article