PROBLEM LINK:
Setter: sayan_kashyapi
Tester: mishra_roshan
Editorialist: shaw_sandeep
DIFFICULTY:
Simple
PREREQUISITES:
FizzBuzz Program / Basic Conditional Logic
PROBLEM:
Given three integers A, B, and N. Travel from 1 to N, If the number is a multiple of A, print “Beautiful”. If the number is a multiple of B, print “Lie”. If the number is a multiple of both A and B, print “BeautifulLie” else print the current number itself.
EXPLANATION
The implementation is same as standard FizzBuzz program with some basic modification.
-
Check for multiple of A and B i.e, “BeautifulLie”
-
Either check for the multiple of A (Beautiful) or multiple of B (Lie)
-
Else print the number itself
TIME COMPLEXITY
Time complexity is O(N).
SOLUTIONS:
Setter's Solution
C++
#include <iostream>
using namespace std;
int main(){
int n1, n2,n;
cin >> n1 >> n2 >> n;
for ( int i = 1 ; i <= n ; i++){
if(i%n1==0 && i%n2==0){
cout << "BeautifulLie" << endl;
}
else if(i%n1==0){
cout << "Beautiful" << endl;
}
else if(i%n2==0){
cout << "Lie" << endl;
}
else{
cout << i << endl;
}
}
return 0;
}
Tester's Solution
C++
#include <iostream>
using namespace std;
int main()
{
int n1, n2,n;
cin >> n1 >> n2>>n;
int i, f = n1 - 1, b = n2 - 1;
for ( i = 1 ; i <= n ; ++i, --f, --b )
{
if ( f && b )cout << i;
if ( !f ) {cout << "Beautiful"; f = n1;}
if ( !b ) {cout << "Lie"; b = n2;}
cout << "\n";
}
return 0;
}
Feel free to Share your approach.
Suggestions are welcomed as always had been.