1

Given an array of integer.

I want to first print print 1st, 4th, 7th.. numbers. Then 2nd, 5th, 8th.. And then 3rd, 6th, 9th..

I want to use ONLY ONE LOOP to iterate through the array. How can I do this?

(Note: Size of array will always be like 4, 7, 10, 13.. and so on, i.e. in increments of 3.)

Thanks.

3
  • While it's definitely feasible, it's going to be pretty painful to read. A nested loop would be a lot simpler. Why do you only want one loop? Will there always be exactly 9 integers? Commented Jun 11, 2016 at 6:41
  • Jon: The length of array will be like 4, 7, 10, 13... i.e. increments of 3. The problem I am dealing with uses only one loop, I don't want to alter that. Commented Jun 11, 2016 at 6:45
  • Jon: Just curious; how can I accomplish this with nested loop? Commented Jun 11, 2016 at 6:49

5 Answers 5

2

Write a function

private int index(int i, int length)

which computes the right index for the loop variable i

EDIT

Wasn't that easy as i first thought:

public static void main(String[] args) {

    int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
    for (int i = 0; i < array.length; i++) {

        System.out.println(array[index(i, array.length)]);
    }
}

private static int index(int i, int length) {

    int third = (length + 2) / 3;
    if (length % 3 == 1 && i >= third) { // spezial, because second third is smaller
        return index(i - 1, length - 1);

    }

    int group = i % third;

    return (group) * 3 + (i / third);
}

EDIT did some cleanup;

Sign up to request clarification or add additional context in comments.

1 Comment

Can you please be more elaborate?
1

This is just a rough guess but something like this should work-

int b[]; int temp=b.length;//Store the inputted numbers in this array
for(int i=0, j=0; j<3; i=i+3){
    if(i<temp) System.out.println(b[i]);
    if(i>=temp){
        i=i%3+1;
        ++j;
     }


    }

1 Comment

This is a more generalized form for a variable sized array
1

Here's how i would do this,print each array sequence vertically:-

int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int k = 3;
    for (int i = 0; i < a.length; i += k) {
        System.out.print(a[i] + "\t");
        if (i + 1 < a.length) {
            System.out.print(a[i + 1] + "\t");
            if (i + 2 < a.length) {
                System.out.println(a[i + 2]);
            }
        }
    }

OUTPUT

1 2 3

4 5 6

7 8 9

10

Comments

0

You need to set the loop index again when length is reached. Check the code below

int a[] = {4,6,7,2,7,8,9,4,3};
int i = 0;
int j = 0;
int start = 3;
int printed = 0;
while (i < a.length && printed < a.length) {
 printed++;
 System.out.print(a[i] + " ");

 i += start;
 if (!(i < a.length)) {
  i = ++j;
  System.out.println();
 }
}

Output

4 2 9 
6 7 4 
7 8 3 

DEMO

Comments

0

Assuming that the length of the array is 9, which is how I read the second line in the question, this would be my approach:

int[] a = {1,2,3,4,5,6,7,8,9};

for (int i = 0; i < a.length*3; i+=3) {
    System.out.print(a[i%a.length + i/a.length] + " ");
}

Output:

1 4 7 2 5 8 3 6 9 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.