Wednesday, July 29, 2009

Puzzle 46 - Just a Sort.

Language – Java Type – Concept Last date 2-Aug-2009 9:00 p.m. IST Points 5

Its been a long time since we had a programming question out here. So here is one where you get to punch in some real code.

Print the maximum number of a list of ten inputted numbers.

Easy? Well here are some clauses that should make this harder.
1. No classes may be used except java.lang.Object. So no collection frameworks, none of the inbuilt stuff to sort the numbers :).
2. No arrays may be used in the entire code.
3. Should compile and run with Java 1.4

Here is what the pseudo code for the program should look like;

int num1 = [scan the number at runntime]
int num9 = [scan the number at runntime]
int num10 = [scan the number at runntime]


If you find yourself more than 20 to 25 lines of code ... think again!!

Got a solution. Do leave it here.


  1. private static int max(int x, int y){
    return x > y ? x : y;

    // BTW what the hell programmer start counting from 1???
    private static printMax(num0, num1..., num9){
    return max(num0,max(num1,max(num2,max(num3,max(num4,max(num5,max(num6,max(num7,max(num8,num9)))))))));

  2. public static void printMax(int num1, int num2, int num3, int num4, int num5, int num6, int num7, int num8, int num9, int num10) {
    int curMax = num1;
    if (curMax < num2) curMax = num2;
    if (curMax < num3) curMax = num3;
    if (curMax < num4) curMax = num4;
    if (curMax < num5) curMax = num5;
    if (curMax < num6) curMax = num6;
    if (curMax < num7) curMax = num7;
    if (curMax < num8) curMax = num8;
    if (curMax < num9) curMax = num9;
    if (curMax < num10) curMax = num10;

  3. 1. int max = 0
    2. loop thru 1 to n
    2.1. int num = (scan number)
    2.2. check if num > max
    then max = num
    3. print tmp

  4. That's easy, but can you do it without ifs?

  5. I hope that I've correctly understand thing about 20-25 lines :)

    private static void printMax(int num1, int num2, int num3, int num4, int num5, int num6, int num7, int num8, int num9, int num10) {
    System.out.println(max(num1, max(num2, max(num3, max(num4, max(num5, max(num6, max(num7, max(num8, max(num9, num10))))))))));

    private static int max(int num1, int num2) {
    return num1 > num2 ? num1 : num2;

  6. One more (more ugly :))
    private static void printMax(int num1, int num2, int num3, int num4, int num5, int num6, int num7, int num8, int num9, int num10) {
    int max = num1 > num2 ? num1 : num2;
    max = max > num3 ? max : num3;
    max = max > num4 ? max : num4;
    max = max > num5 ? max : num5;
    max = max > num6 ? max : num6;
    max = max > num7 ? max : num7;
    max = max > num8 ? max : num8;
    max = max > num9 ? max : num9;
    max = max > num10 ? max : num10;

    This can be copy-pasted to single line (looks like a nightmare :)), but I can't post it here because it longer than 4096 characters.

  7. Though we can solve this problem using java.lang.String and Java.lang.StrinTokenizer classes, but the restrictions are these should not be used. As you said we should not use arrays, we don't have chance to use args[] array, but as u said that should not use any other classes, then we cant uses to read at runtime.
    As you are saying that, we should not use any other class, rather than Lang.Object. Then, there is only one way to achieve this is through Native coding.

  8. The solution for this puzzle can be solved by Native Coding(i.e calling a native method located in C from Java)

    Java Code
    public class Main4
    private native void sort();
    public static void main(String[] args)
    new Main4().sort();

    C Code
    #include "jni.h" // here we have to use jni
    #include "stdio.h"
    #include "conio.h"
    //#include "Main4.h"

    Java_Main4_sort(JNIEnv *, jobject)
    int x=0,y=0,i=0;
    printf("Enter element %d:",(i+1));
    printf("\n Maximum Number in the given list is:%d"x);


    Procedure for the above code

    1. Compile the file we get Main4.class
    2. Now again create a header file for the Main4.class using javah command.(javah -jni Main4)
    3. Now we get Main4.h, now compile the .C file with Microsoft Visual C++ compiler, by including all the header files located in the java directory and create a .dll file.
    4. Now share that .dll file, now set the library path and run the java Main4. we get the required output.


Solution for this question?