Lucky and distinct[] (https://www.codechef.com/PATN2021/problems/LCKY3)

Practice

Author: Aryan KD
Tester: Aryan KD
Editorialist: Aryan KD

DIFFICULTY:

CAKEWALK, SIMPLE, EASY.

PREREQUISITES:

Math .

PROBLEM:

An Lucky number is a number KK such that the first digit of KK represent the count of how many zeroes are there in KK , the second digit represent the count of how many one’s are there in KK and so on .

You are given a program which accept string KK which is a number and checks whether the number is an Lucky number or not if it is an Lucky number then it prints the count of distinct number present in string KK If not then print the addition of all number in given string .

EXPLANATION:

SOLUTIONS:

Setter's Solution
  1. // lucky and distinct element

  2. #include<bits/stdc++.h>

  3. using namespace std;

  4. void testcase()

  5. {

  6. string n;

  7. cin>>n;

  8. int a[1000]={0};

  9. int count=0;

  10. for(int i=0;i<n.size();i++){

  11. a[n[i]-‘0’]++;

  12. }

  • int flag=1;
  1. for(int i=0;i<n.size();i++){

  2. if(a[i]!=(n[i]-‘0’)){

  3. flag=0;

  4. break;

  5. }

  6. }

  7. if(flag==1){

  8. for(int i=0;i<1000;i++){

  9. if(a[i]>0){

  10. count++;

  11. }

  12. }

  13. cout<<count;

  14. }

  15. else{

  16. int sum=0;

  17. for(int i=0;i<n.size();i++){

  18. sum+=n[i]-‘0’;

  19. }

  20. cout<<sum;

  21. }

  • }
  1. int main()

  2. {

  3. int t;

  4. cin>>t;

  5. while(t–)

  6. {

  7. testcase();

  8. cout<<endl;

  9. }

  10. return 0;

  11. }

Tester's Solution
  1. // lucky and distinct element

  2. #include<bits/stdc++.h>

  3. using namespace std;

  4. void testcase()

  5. {

  6. string n;

  7. cin>>n;

  8. int a[1000]={0};

  9. int count=0;

  10. for(int i=0;i<n.size();i++){

  11. a[n[i]-‘0’]++;

  12. }

  • int flag=1;
  1. for(int i=0;i<n.size();i++){

  2. if(a[i]!=(n[i]-‘0’)){

  3. flag=0;

  4. break;

  5. }

  6. }

  7. if(flag==1){

  8. for(int i=0;i<1000;i++){

  9. if(a[i]>0){

  10. count++;

  11. }

  12. }

  13. cout<<count;

  14. }

  15. else{

  16. int sum=0;

  17. for(int i=0;i<n.size();i++){

  18. sum+=n[i]-‘0’;

  19. }

  20. cout<<sum;

  21. }

  • }
  1. int main()

  2. {

  3. int t;

  4. cin>>t;

  5. while(t–)

  6. {

  7. testcase();

  8. cout<<endl;

  9. }

  10. return 0;

  11. }

Editorialist's Solution
  1. // lucky and distinct element

  2. #include<bits/stdc++.h>

  3. using namespace std;

  4. void testcase()

  5. {

  6. string n;

  7. cin>>n;

  8. int a[1000]={0};

  9. int count=0;

  10. for(int i=0;i<n.size();i++){

  11. a[n[i]-‘0’]++;

  12. }

  • int flag=1;
  1. for(int i=0;i<n.size();i++){

  2. if(a[i]!=(n[i]-‘0’)){

  3. flag=0;

  4. break;

  5. }

  6. }

  7. if(flag==1){

  8. for(int i=0;i<1000;i++){

  9. if(a[i]>0){

  10. count++;

  11. }

  12. }

  13. cout<<count;

  14. }

  15. else{

  16. int sum=0;

  17. for(int i=0;i<n.size();i++){

  18. sum+=n[i]-‘0’;

  19. }

  20. cout<<sum;

  21. }

  • }
  1. int main()

  2. {

  3. int t;

  4. cin>>t;

  5. while(t–)

  6. {

  7. testcase();

  8. cout<<endl;

  9. }

  10. return 0;

  11. }