HACKER RANK Algorithms - Implementation - Sherlock and the Beast - Solution.'C'

                        Algorithms - Implementation - Sherlock and the Beast - Solution
Problem Statement
Sherlock Holmes suspects his archenemy, Professor Moriarty, is once again plotting something diabolical. Sherlock's companion, Dr. Watson, suggests Moriarty may be responsible for MI6's recent issues with their supercomputer, The Beast.
Shortly after resolving to investigate, Sherlock receives a note from Moriarty boasting about infecting The Beast with a virus; however, he also gives him a clue—a number, N. Sherlock determines the key to removing the virus is to find the largest Decent Number having Ndigits.
Decent Number has the following properties:
  1. Its digits can only be 3's and/or 5's.
  2. The number of 3's it contains is divisible by 5.
  3. The number of 5's it contains is divisible by 3.
Moriarty's virus shows a clock counting down to The Beast's destruction, and time is running out fast. Your task is to help Sherlock find the key before The Beast is destroyed!
Constraints
1T20
1N100000
Input Format
The first line is an integer, T, denoting the number of test cases.
The T subsequent lines each contain an integer, N, detailing the number of digits in the number.
Output Format
Print the largest Decent Number having N digits; if no such number exists, tell Sherlock by printing -1.
Sample Input
4
1
3
5
11
Sample Output
-1
555
33333
55555533333
Explanation
For N=1, there is no such number.
For N=3555 is the only possible number.
For N=533333 is the only possible number.
For N=1155555533333 and all permutations of these digits are valid numbers; among them, the given number is the largest one.

Solution:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
 
int main() {
 
    int t;
    long long int n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld",&n);
        int div3 = n%3; //remainder after dividing by three
        int i;
        //This is brute force approach
        if(div3 == 0)
        {
            for(i =0;i<n;i++)
                printf("5");//We are printing each digit individually because 1<=N<=100000
            printf("\n");
        }
        
        else if(div3 == 2 && n>=5)//Question requires max no,so if div3 = 2 we keep 5 3's in the end
        {
            for(i=0;i<n-5;i++)
                printf("5");
            for(i=0;i<5;i++)
                printf("3");
            printf("\n");
        }
        else if(div3 == 1 && n>=10)//10 3's in the end for div3 = 1
        {
            for(i=0;i<n-10;i++)
                printf("5");
            for(i=0;i<10;i++)
                printf("3");
            printf("\n");
        }
        else if(n%5 == 0)
        {
            for(i=0;i<n;i++)
                printf("3");
            printf("\n");
        }
        else
            printf("-1\n");
        
    }
    return 0;
}

Thanks for Visiting, Hope this helps you....

Comments

  1. can you explain your approach in simple english??

    ReplyDelete

Post a Comment

Popular Posts