LiziPizi
LiziPizi

Reputation: 196

Arithmetic Sequences program

I want to write a program Sequence that reads an arbitrary number of command­line ints and stores them inside an array. The program then looks for 3 numbers in the array that form an arithmetic sequece of length 3.

For example:

% java Sequence 20 8 27 19 10 56 7 12 98 
The numbers 8, 10, 12 located at indices 1, 4, 7 form an arithmetic sequence

This is my code until now but it doesn't work:

public class Sequence {

    public static void main(String[] args){

        int[] arr = new int[args.length];

        for(int i = 0; i < arr.length; i++){
            arr[i] = Integer.parseInt(args[i]);
        }

        // Process
        for(int a = 0; a < arr.length; a++){
            for(int b = 1; b < arr.length; b++){
                for(int c = 2; c < arr.length; c++){
                    if (arr[b] - arr[a] == arr[c] - arr[b]){
                        System.out.println(a + " " + b + " " + c);
                    }
                }
            }
        }   
    }
}

When I run this it shows me this:

1 4 7
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8

Upvotes: 1

Views: 3622

Answers (2)

What you want is tot start index b at a+1 and index c at b+1 in order to avoid using certain numbers double. Although I must say, the sequences you printed are in fact correct as "5 5 5" is an arithmetic sequence. Adding this little change leads to:

public class Sequence {

public static void main(String[] args){

int[] arr = new int[args.length];

for(int i = 0; i < arr.length; i++){
    arr[i] = Integer.parseInt(args[i]);
}

// Process
for(int a = 0; a < arr.length; a++){
    for(int b = a+1; b < arr.length; b++){
        for(int c = b+1; c < arr.length; c++){
            if (arr[b] - arr[a] == arr[c] - arr[b]){
                System.out.println(arr[a] + " " + arr[b] + " " + arr[c]);
                //print the sequence and not the index
            }
        }
    }
}


}
}

Upvotes: 2

Ivaylo Toskov
Ivaylo Toskov

Reputation: 4021

You have two major issues:

  1. You are not printing the numbers that you find, but their indexes instead. You need to change System.out.println(a + " " + b + " " + c); to System.out.println(arr[a] + " " + arr[b] + " " + arr[c]);
  2. Your indexing is not correct. For each index from the second on, you end up getting the same number three times, which always builds a sequence.

Here is the correct code:

public class Sequence {
    public static void main(String[] args){

        // 20 8 27 19 10 56 7 12 98
        int[] arr = new int[args.length];

        for(int i = 0; i < arr.length; i++){
            arr[i] = Integer.parseInt(args[i]);
        }

        for(int a = 0; a < arr.length; a++){
            for(int b = a + 1; b < arr.length; b++){
                for(int c = b + 1; c < arr.length; c++){
                    if (arr[b] - arr[a] == arr[c] - arr[b]){
                        System.out.println(arr[a] + " " + arr[b] + " " + arr[c]);
                    }
                }
            }
        }
    }
}

Upvotes: 2

Related Questions