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

Delete a Node

Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value. Example: list=0->1->2->3 position=2 After removing the node at position 2, list'= 0->1->-3. Function Description: Complete the deleteNode function in the editor below. deleteNo

View Solution →

Print in Reverse

Given a pointer to the head of a singly-linked list, print each data value from the reversed list. If the given list is empty, do not print anything. Example head* refers to the linked list with data values 1->2->3->Null Print the following: 3 2 1 Function Description: Complete the reversePrint function in the editor below. reversePrint has the following parameters: Sing

View Solution →

Reverse a linked list

Given the pointer to the head node of a linked list, change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty. Example: head references the list 1->2->3->Null. Manipulate the next pointers of each node in place and return head, now referencing the head of the list 3->2->1->Null. Function Descriptio

View Solution →

Compare two linked lists

You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis

View Solution →

Merge two sorted linked lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C

View Solution →

Get Node Value

This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t

View Solution →