Join Regular Classroom : Visit ClassroomTech

Tech Mahindra Coding Question Solve | Set-A | Part I | codewindow.in

Find Me Out

For a given number N (0<N<=100), little Jhonny wants to the find out the minimum positive integer X divisible by N, where the sum of digits of X is equal to N, and X is not equal to N.

Note: If such a number X does not exist, then the output should be -1

Input Specification:
input1: An integer N
Output Specification:
Return the minimum positive integer X

Example 1

Input:
9

Output:
18

Explanation:
Starting from 1, the minimum positive integer to be returned by the function is 18 which is divisible by the sum of the digits of 18 is equal to 9 and 18 is returned as the output.

Solution: In Java

import java.util.*;
import java.lang.*;
import java.io.*;

class CodeWindow {
    static int visited[][]= new int[501][5001];
    
    static class Node {
    	int a, b;
    	String str;
    	Node(int a1,int b1,String s)
    	{
    		a=a1;
    		b=b1;
    		str=s;
    	}
    }
    
    static int findNumber(int a, int b) {
    	Queue<Node> q= new LinkedList<Node>();
    	Node temp =new Node( 0, 0, "" );
    	visited[0][0] = 1;
    	q.add(temp);
    	while (q.size()!=0) {
    		Node u = q.peek();
    		q.remove();
    		
    		if (u.a == 0 && u.b == b && Integer.parseInt(u.str) != b)
    			return Integer.parseInt(u.str);

    		for (int i = 0; i < 10; i++) {
    			int dd = (u.a * 10 + i) % a;
    			int ss = u.b + i;
    			if (ss <= b && visited[dd][ss]==0) {
    				visited[dd][ss] = 1;
    				q.add(new Node( dd, ss, u.str + (char)('0' + i) ));
    			}
    		}
    	}
    	
    	return -1;
    }

	public static void main (String[] args) throws Exception
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int a = Integer.parseInt(br.readLine());
		int[] singleDigitAns = {0, 10, 20, 12, 40, 50, 24, 70, 80, 18};
		for(int i=0;i<500;i++)
    		for(int j=0;j<500;j++)
    			visited[i][j]=0;
    	if(a < 10)
    	    System.out.println(singleDigitAns[a]);
    	else
    	    System.out.println(findNumber(a, a));
	}
}

Trailing Zeros

Given ‘n'(1<=n<=10^6), find the number of trailing zeros in the factorial of ‘n’.
The factorial of ‘n’ is defined as the product of all integers from 1 to ‘n’.
Return the number of trailing zeros in the factorial of a given number.

Input Specification:
input1: An integer for which you have to calculate factorial
Output Specification:
Return an integer representing the number of trailing zeros

Example 1

Input:
5

Output:
1

Explanation:
The factorial of 5 is 120. The number of trailing zeros = 1.

Solution: In Java

import java.util.*;
import java.lang.*;
import java.io.*;

class CodeWindow {
    static int findTrailingZeros(int n) {
    	if (n < 0)
            return -1;
        int count = 0;
        for (int i = 5; n / i >= 1; i *= 5)
            count += n / i;
        return count;
    }

	public static void main (String[] args) throws Exception
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		System.out.println(findTrailingZeros(n));
	}
}

Also Checkout

Recent Posts
Categories
Pages