0

How do I get this with an algorithm in java: ex:

the_specific_number = 5
length_of_the_array = 3

and this should return:

5 - 0 - 0;
4 - 1 - 0;
4 - 0 - 1;
3 - 2 - 0;
3 - 1 - 1;
3 - 0 - 2;
2 - 3 - 0;
2 - 2 - 1;
2 - 1 - 2;
2 - 0 - 3;
1 - 4 - 0;
1 - 3 - 1;
1 - 2 - 2;
1 - 1 - 3;
1 - 0 - 4;
0 - 5 - 0;
0 - 4 - 1;
0 - 3 - 2;
0 - 2 - 3;
0 - 1 - 4;
0 - 0 - 5;

I have been thinking about this since 10 days but I found nothing

1 Answers1

0

Well I would like to help you up to some extent that's why I'm going to tell you solution of this problem almost. You can try rest of the things as per your requirement.

public static void main(String[] args) {
    try (Scanner in = new Scanner(System.in)) {
        int iNum = in.nextInt();
        int iRange = 3;// Fixed for now.

        int iCount = 1;
        int iIncrement = iRange / 2;
        for (int i = iNum; i >= 0; i--) {
            for (int k = 0; k < iCount; k++) {
                for (int j = 0; j < iRange; j++) {
                    if (j == 0) {// Printing First Digit.
                        System.out.print(i + " - ");
                    } else if (j == iRange - 1) {// Printing Last Digit.
                        System.out.print(k + ";");
                    } else {
                        // Printing Rest of the Digits.
                        /**
                         * Edit this section to make this code generic for variable iRange.
                         */
                        System.out.print((iNum - i - k) + " - ");
                    }
                    // System.out.print(i + " - " + j + " - " + k + " : ");
                }
                System.out.println();
            }
            iCount += iIncrement;
        }
    }
}

Output-:

5
5 - 0 - 0;
4 - 1 - 0;
4 - 0 - 1;
3 - 2 - 0;
3 - 1 - 1;
3 - 0 - 2;
2 - 3 - 0;
2 - 2 - 1;
2 - 1 - 2;
2 - 0 - 3;
1 - 4 - 0;
1 - 3 - 1;
1 - 2 - 2;
1 - 1 - 3;
1 - 0 - 4;
0 - 5 - 0;
0 - 4 - 1;
0 - 3 - 2;
0 - 2 - 3;
0 - 1 - 4;
0 - 0 - 5;

Description-: I have written this code for variable iNum (the_specific_number) and fixed iRange (length_of_the_array). While running this application it'll ask for iNum to input manually and generate your required array of numbers.
If you want to make it generic for iRange then you can update logic inside else section.

 } else {
                            // Printing Rest of the Digits.
                            /**
                             * Edit this section to make this code generic for variable iRange.
                             */
                            System.out.print((iNum - i - k) + " - ");
                        }

You can use // System.out.print(i + " - " + j + " - " + k + " : "); line to analyse values of i, j & K in all the cases so that you can make some conclusion to write logic for variable range of iRange. I hope this would help you up to some extent. We can discuss further if you wish to do so. But it's always better to try your self before asking to some one else and if you have done something then share the work done while asking.

Ashish Kumar
  • 916
  • 2
  • 15
  • 32
  • thanks for your answer, I'm very glad you helped me out with this. In my mind, I've always thinking about a recurssive function but I'm not good in that and the performance would be really slow with an arraylist when I'll get the answer so, that's why I dont want to show a part of my code, I'm very sorry, but thank you, again – Kevin Randriajaoson Feb 03 '17 at 18:53