CMS
CMS

Reputation: 706

How to get a sequence of 5 numbers from an array

I want to get a sequence of 5 numbers from an array. For example :

int arr1[] = {3,88,99,5,4,6,22,32,7,45}; // array where there is the sequence 3,4,5,6,7
Vector<Integer> myVec = new Vector<Integer>(); // vector wehre to save the sequence

Now what do I have to do to get the sequence from that array? I have a code here but it does not work properly:

for(int i = 0; i < arr1.length -1; i++) {

    int a = arr1[i];
    int b = arr1[i+1];
    int c = b - a;

    if(c == 1) {
        myvec.add(arr1[i]);
    }
}

How should I change my code to solve this?

Upvotes: 0

Views: 795

Answers (3)

Masudul
Masudul

Reputation: 21961

You should count successive count upto 5 and put the result into ArrayList instead of Vector. Because, ArrayList is more efficient than Vector. Try,

 int arr1[] = {3, 88, 99, 5, 4, 6, 22, 32, 7, 45, 11, 12, 13, 14, 15};
 List<Integer> myVec = new ArrayList<>();

 Arrays.sort(arr1);

    int count = 0;
    int lastValue = 0;
    for (int i = 0; i < arr1.length - 1; i++) {
        if (arr1[i + 1] - arr1[i] == 1) {
            count++;
            System.out.println(arr1[i]);
            lastValue = arr1[i + 1];
        } else {
            System.out.println(count);
            if (count >= 4) {
                for (int j = 0; j <= count; j++) {
                    myVec.add(lastValue - 4 + j);
                }
            }
            count = 0;
        }
    }
    System.out.println(myVec);

Upvotes: 0

Syam S
Syam S

Reputation: 8499

This program will print all the sequence in the array WITHOUT SORTING THE ARRAY. You could select the list with size.. Say if you want match 5 sequence get the list with size 5. Hope this help. (Modify as per your need.)

import java.util.ArrayList;
import java.util.List;

public class Sequence {

    private static int arr1[] = { 3, 88, 99, 5, 4, 6, 22, 32, 7, 45 };

    private static int findNextInSequence(int start) {
        int next = -1;

        for(int i = 0; i < arr1.length; i++){
            if((start - arr1[i]) == -1){
                next = arr1[i];
            }
        }

        return next;
    }

    public static void main(String[] args) {


        for (int i = 0; i < arr1.length; i++) {

            List<Integer> sequence = new ArrayList<Integer>();
            int nextSequence = arr1[i];
            do{
                sequence.add(nextSequence);
                nextSequence = findNextInSequence(nextSequence);
            } while(nextSequence != -1);

            System.out.println(sequence);
        }
    }
}

Upvotes: 1

TheMP
TheMP

Reputation: 8427

Your code checks the difference between two successive values from arr1 array. Because it is not sorted, it works like this:

88-3 !=1 (value not added)
99-88 !=1 (value not added)
5-99 !=1 (value not added)
...
45-7 !=1 (value not added).

You have to ensure that values in your array are sorted first. Use Arrays.sort() and apply the method on sorted array. This should add values: 3,4,5,6, but it WON'T add 7 (because it'll be your arr[i+1] value at the time of loop execution.

4-3 ==1 (value added)
5-4 ==1 (value added)
6-5 ==1 (value added)
7-6 ==1 (value added, but it is only number 6 that is added!)

Upvotes: 0

Related Questions