Possible timings
Last Updated :
07 Feb, 2022
Given a one/two digit timing, calculate the possibilities of occurrence of other timings(including the glowing one) with relevant to glowing segments, assuming some segments may not be glowing.
Displaying the numbers is done using seven segment display. It is guaranteed that the sticks currently displaying are working fine..
Examples:
Input : 78
Output :5
Input :05
Output :8
Explanation:
Example 1: 7 can be replaced by 5 different numbers 9, 3, 8, 0 and 7(if none of the segment is broken) and 8 can be replaced by only 1 number i.e 8 itself(if none of the segment is broken), therefore the answer is 5*1=5.
Example 2: 0 can be replaced by 2 numbers, 8 and 0, while 5 can be replaced by 4 different numbers. So, answer is 4*2=8.
Approach :
Calculate for every number from 0–9 what all digits are possible by adding or
removing exactly one rod from the display. Store this in an array and the answer will be product of the array value of both the digits of the input.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int num[10] = { 2, 7, 2, 3, 3,
4, 2, 5, 1, 2 };
void possibleTimings(string n)
{
cout << num[n[0] - '0' ] *
num[n[1] - '0' ] << endl;
}
int main()
{
string n = "05" ;
possibleTimings(n);
return 0;
}
|
Java
import java.io.*;
class Calci {
static int num[] = { 2 , 7 , 2 , 3 , 3 ,
4 , 2 , 5 , 1 , 2 };
public static void possibleTimings(String n)
{
System.out.println(num[(n.charAt( 0 ) - '0' )]
* num[n.charAt( 1 ) - '0' ]);
}
public static void main(String args[])
{
String n = "05" ;
possibleTimings(n);
}
}
|
Python3
num = [ 2 , 7 , 2 , 3 , 3 , 4 , 2 , 5 , 1 , 2 ]
def possibleTimings(n):
print (num[ int (n[ 0 ]) - int ( '0' )] * num[ int (n[ 1 ]) - int ( '0' )] )
n = "05"
possibleTimings(n)
|
C#
using System;
class Calci {
static int []num = { 2, 7, 2, 3, 3,
4, 2, 5, 1, 2 };
public static void possibleTimings( string n)
{
Console.WriteLine(num[(n[0] - '0' )]
* num[n[1] - '0' ]);
}
public static void Main()
{
string n = "05" ;
possibleTimings(n);
}
}
|
PHP
<?php
$num = array (2, 7, 2, 3, 3,
4, 2, 5, 1, 2);
function possibleTimings( $n )
{
global $num ;
echo ( $num [ $n [0] - '0' ] *
$num [ $n [1] - '0' ]. "\n" );
}
$n = "05" ;
possibleTimings( $n );
?>
|
Javascript
<script>
var num = [ 2, 7, 2, 3, 3, 4, 2, 5, 1, 2 ];
function possibleTimings(n)
{
document.write(num[n[0] - '0' ] * num[n[1] - '0' ]);
}
var n = "05" ;
possibleTimings(n);
</script>
|
Output:
8
Share your thoughts in the comments
Please Login to comment...