Sherlock and The Beast


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. He also gives him a clue: an integer. Sherlock determines the key to removing the virus is to find the largest Decent Number having that number of digits.

A Decent Number has the following properties:

Its digits can only be 3's and/or 5's.
The number of 3's it contains is divisible by 5.
The number of 5's it contains is divisible by 3.
It is the largest such number for its length.
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!


Function Description

Complete the decentNumber function in the editor below.

decentNumber has the following parameter(s):

int n: the length of the decent number to create
Prints

Print the decent number for the given length, or  if a decent number of that length cannot be formed. No return value is expected.

Input Format

The first line is an integer, , the number of test cases.

The next  lines each contain an integer , the number of digits in the number to create.



Solution :



title-img


                            Solution in C :

In  C :







#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
  int T, N, i, j, c;
  fscanf(stdin, "%d", &T);

  for (i=0; i<T; ++i) {
	if (fscanf(stdin, "%d", &N)==EOF) return 1;
	if (N%3==0) {
		for (j=0; j<N/3; ++j) printf("555");
	}
	else {
		c=0;
		while (N>=0 && N%3!=0) {
			++c;
			N-=5;
		}
		if (N<0) printf("-1");
		else {
			for (j=0; j<N/3; ++j) printf("555");
			for (j=0; j<c; ++j) printf("33333");
		}
	}
	printf("\n");
  }
  return 0;
}
                        


                        Solution in C++ :

In  C++  :






#include <string>
#include <vector>
#include <map>
#include <list>
#include <iterator>
#include <set>
#include <queue>
#include <iostream>
#include <sstream>
#include <stack>
#include <deque>
#include <cmath>
#include <memory.h>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <utility> 
using namespace std;
 
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define REP(i, N) FOR(i, 0, N)
#define RREP(i, N) RFOR(i, N, 0)
 
#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define PB push_back
#define MP make_pair
#define Pi 3.14159265358979

typedef long long Int;
typedef unsigned long long UInt;
typedef vector <int> VI;
typedef pair <int, int> PII;



int main()
{
		
	int T;
	cin>>T;
	REP(tests,T)
	{
		int n;
		cin>>n;
		
		int res = -1;
		
		for (int i = 0; i <= n; ++i)
		{
			if (i % 5 == 0)
			{
				int j = n - i;
				
				if (j % 3 == 0)
				{
					res = i;
					break;
				}
			}
		}
		
		if (res == -1)
		{
			cout << -1 << endl;
			continue;
		}
		
		REP(i,n-res)
			putchar('5');
		REP(i,res)
			putchar('3');
		puts("");
	}

	
	return 0;
}
                    


                        Solution in Java :

In  Java :







import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Solution {
	static BufferedReader in = new BufferedReader(new InputStreamReader(
			System.in));
	static StringBuilder out = new StringBuilder();
	

	public static void main(String[] args) throws NumberFormatException, IOException {
		int numCases = Integer.parseInt(in.readLine());
		for(int t = 0; t < numCases; t ++)
		{
			int k = Integer.parseInt(in.readLine());
			int numFives = (k / 3) * 3;
			int numThrees = 0;
			if(k-numFives == 2)
			{
				numFives -= 3;
				numThrees += 5;
			}
			else if(k-numFives == 1)
			{
				numFives -= 9;
				numThrees += 10;
			}
			
			if(numFives >= 0)
			{
				for(int i = 0; i < numFives; i ++)
				{
					out.append(5);
				}
				for(int i = 0; i < numThrees; i ++)
				{
					out.append(3);
				}
				out.append("\n");
			}
			else
			{
				out.append("-1\n");
			}
		}

		System.out.print(out);
	}
}
                    


                        Solution in Python : 
                            
In  Python3 :







T = int(input())
for i in range(T):
    k = int(input())
    n5 = k
    n3 = 0
    done = False
    while n5 >= 0:
        if n5%3 == 0 and n3%5 == 0:
            for j in range(n5):
                print('5',end='')
            for j in range(n3):
                print('3',end='')
            print()
            done = True
            break
        n5 -= 1
        n3 += 1
    if not done:
        print(-1)
                    


View More Similar Problems

Direct Connections

Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do

View Solution →

Subsequence Weighting

A subsequence of a sequence is a sequence which is obtained by deleting zero or more elements from the sequence. You are given a sequence A in which every element is a pair of integers i.e A = [(a1, w1), (a2, w2),..., (aN, wN)]. For a subseqence B = [(b1, v1), (b2, v2), ...., (bM, vM)] of the given sequence : We call it increasing if for every i (1 <= i < M ) , bi < bi+1. Weight(B) =

View Solution →

Kindergarten Adventures

Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti

View Solution →

Mr. X and His Shots

A cricket match is going to be held. The field is represented by a 1D plane. A cricketer, Mr. X has N favorite shots. Each shot has a particular range. The range of the ith shot is from Ai to Bi. That means his favorite shot can be anywhere in this range. Each player on the opposite team can field only in a particular range. Player i can field from Ci to Di. You are given the N favorite shots of M

View Solution →

Jim and the Skyscrapers

Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space

View Solution →

Palindromic Subsets

Consider a lowercase English alphabetic letter character denoted by c. A shift operation on some c turns it into the next letter in the alphabet. For example, and ,shift(a) = b , shift(e) = f, shift(z) = a . Given a zero-indexed string, s, of n lowercase letters, perform q queries on s where each query takes one of the following two forms: 1 i j t: All letters in the inclusive range from i t

View Solution →