Count changes in Led Lights to display digits one by one
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++
// CPP program to count number of on offs to // display digits of a number. #include<bits/stdc++.h> using namespace std; int countOnOff(string n) { // store the led lights required to display // a particular number. int Led[] = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 5 }; int len = n.length(); // compute the change in led and keep // on adding the change 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; } // Driver code int main() { string n = "082"; cout << countOnOff(n); return 0; } |
Java
// Java program to count number of on offs to // display digits of a number. import java.io.*; class GFG { static int countOnOff(String n) { // store the led lights required to display // a particular number. int Led[] = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 5 }; int len = n.length(); // compute the change in led and keep // on adding the change 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; } // Driver code public static void main(String args[]) { String n = "082"; System.out.println( countOnOff(n) ); } } |
Python 3
# Python3 program to count number of on offs to # display digits of a number. def countOnOff(n): # store the led lights required to display # a particular number. Led = [ 6, 2, 5, 5, 4, 5, 6, 3, 7, 5 ] leng = len(n) # compute the change in led and keep # on adding the change 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 #Driver code if __name__=='__main__': n = "082" print(countOnOff(n)) # this code is contributed by # ash264 |
C#
// C# program to count number of on // offs to display digits of a number. using System; class GFG { public static int countOnOff(string n) { // store the led lights required // to display a particular number. int[] Led = new int[] {6, 2, 5, 5, 4, 5, 6, 3, 7, 5}; int len = n.Length; // compute the change in led and // keep on adding the change 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; } // Driver code public static void Main(string[] args) { string n = "082"; Console.WriteLine(countOnOff(n)); } } // This code is contributed by Shrikant13 |
PHP
9
Recommended Posts:
- Count of integers of length N and value less than K such that they contain digits only from the given set
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Count numbers in range such that digits in it and it's product with q are unequal
- Program to count vowels, consonant, digits and special characters in string.
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Python | Display images with PyGame
- Python | Display text to PyGame window
- Maximize the value of A by replacing some of its digits with digits of B
- Count numbers < = N whose difference with the count of primes upto them is > = K
- n-th number with digits in {0, 1, 2, 3, 4, 5}
- Pairs whose concatenation contain all digits
- Smallest odd digits number not less than N
- Check if the given decimal number has 0 and 1 digits only
- Check if a number has digits in the given Order
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



