Count changes in Led Lights to display digits one by one
Last Updated :
05 Dec, 2022
Given a number n. Count the number of changes in LED light when display one after another of a given number. (Initially all LED is off). Number is given input in the form of a string.
See this image of seven segment display for better understanding.
Examples:
Input : n = "082"
Output : 9
We need 6 LED lights to display 0 in seven segment display. We need 7 lights for 8 and 5 lights for 2. So total on/off is 6 + 1 + 2 = 9.
Input : n = "12345"
Output : 7
Source :Morgan Stanley Interview Set 20
The idea is to pre-compute the led lights required to display a given number. Now iterate the number and keep adding the changes. For the implementation, a basic concept of string hashing is used.
Below is the implementation of above problem.
C++
#include<bits/stdc++.h>
using namespace std;
int countOnOff(string n)
{
int Led[] = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 5 };
int len = n.length();
int sum = Led[n[0] - '0' ];
for ( int i = 1; i < len; i++) {
sum = sum + abs (Led[n[i] - '0' ] -
Led[n[i - 1] - '0' ]);
}
return sum;
}
int main()
{
string n = "082" ;
cout << countOnOff(n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int countOnOff(String n)
{
int Led[] = { 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 5 };
int len = n.length();
int sum = Led[n.charAt( 0 ) - '0' ];
for ( int i = 1 ; i < len; i++) {
sum = sum + Math.abs(Led[n.charAt(i) - '0' ] -
Led[n.charAt(i - 1 ) - '0' ]);
}
return sum;
}
public static void main(String args[])
{
String n = "082" ;
System.out.println( countOnOff(n) );
}
}
|
Python 3
def countOnOff(n):
Led = [ 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 5 ]
leng = len (n)
sum = Led[ int (n[ 0 ]) - int ( '0' )]
for i in range ( 1 ,leng):
sum = ( sum + abs (Led[ int (n[i]) - int ( '0' )]
- Led[ int (n[i - 1 ]) - int ( '0' )]))
return sum
if __name__ = = '__main__' :
n = "082"
print (countOnOff(n))
|
C#
using System;
class GFG
{
public static int countOnOff( string n)
{
int [] Led = new int [] {6, 2, 5, 5, 4,
5, 6, 3, 7, 5};
int len = n.Length;
int sum = Led[n[0] - '0' ];
for ( int i = 1; i < len; i++)
{
sum = sum + Math.Abs(Led[n[i] - '0' ] -
Led[n[i - 1] - '0' ]);
}
return sum;
}
public static void Main( string [] args)
{
string n = "082" ;
Console.WriteLine(countOnOff(n));
}
}
|
PHP
<?php
function countOnOff( $n )
{
$Led = array (6, 2, 5, 5, 4,
5, 6, 3, 7, 5 );
$len = strlen ( $n );
$sum = $Led [ $n [0] - '0' ];
for ( $i = 1; $i < $len ; $i ++)
{
$sum = $sum + abs ( $Led [ $n [ $i ] - '0' ] -
$Led [ $n [ $i - 1] - '0' ]);
}
return $sum ;
}
$n = "082" ;
echo countOnOff( $n );
?>
|
Javascript
<script>
function countOnOff( n)
{
var Led = [ 6, 2, 5, 5, 4, 5, 6, 3, 7, 5 ];
var len = n.length;
var sum = Led[n.charAt(0) - '0' ];
for (i = 1; i < len; i++) {
sum = sum + Math.abs(Led[n.charAt(i) - '0' ] -
Led[n.charAt(i - 1) - '0' ]);
}
return sum;
}
n = "082" ;
document.write( countOnOff(n) );
</script>
|
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(1), no extra space is added, so it is a constant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...