-
Notifications
You must be signed in to change notification settings - Fork 30
/
7 Segment Display.cpp
91 lines (74 loc) · 2.32 KB
/
7 Segment Display.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
Solution by Rahul Surana
***********************************************************
Using seven segment display, you can write down any digit from 0 to 9 using at most seven segments.
Given a positive number N and then a string S representing a number of N digits.
You have to rearrange the segments in this N digit number to get the smallest possible N digit number.
This number can have leading zeros. You can not waste any segment or add any segment from your own.
You have to use all of the segments of the given N digits.
Note: You can refer this diagram for more details
***********************************************************
*/
// { Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
string sevenSegments(string S, int N) {
int k = 0;
for(int i = 0 ; i < N; i++){
if(S[i] == '0') k += 6;
else if(S[i] == '1') k += 2;
else if(S[i] == '2') k += 5;
else if(S[i] == '3') k += 5;
else if(S[i] == '4') k += 4;
else if(S[i] == '5') k += 5;
else if(S[i] == '6') k += 6;
else if(S[i] == '7') k += 3;
else if(S[i] == '8') k += 7;
else k += 5;
}
int x = k-(2*N);
// cout << k <<" ";
string ans;
while(x>=4){
if(ans.length()<N)
ans += '0';
x-=4;
}
while(ans.length()<N){
if(x == 0){
while(ans.length()<N) ans+='1';
// ans+='1';
}
else if(x==1){
while(ans.length()<N-1) ans+='1';
ans+='7';
}
else if(x==2){
while(ans.length()<N-1) ans+='1';
ans+='4';
}
else if(x==3){
while(ans.length()<N-1) ans+='1';
ans+='2';
}
}
return ans;
}
};
// { Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
int N;
cin>>N;
string S;
cin>>S;
Solution ob;
cout << ob.sevenSegments(S,N) << endl;
}
return 0;
} // } Driver Code Ends