I have a 2D array and i am trying to look for Similarities in the data, via number patterns.

For Example:

1) 3,4,5,6,7
2) 1,4,8,23,56
3) 14,15,16,34,45

Now what i would like to do is search this array to find patterns like in example 1. it goes 3,4,5,6,7 or in 3. it goes 14,15,16.

I have no clue how to start this if anyone could point me in the right direction that would be great!

Dani AI

Generated

You can treat the sequences you want (3,4,5,6,7 or 14,15,16) as arithmetic progressions with diff = 1. A simple approach is: project every row, column, and diagonal of your 2D array to a 1D view and scan it once to find runs. This keeps the logic small and reusable. Building on ’s idea of checking equal differences, here is a compact 1D detector that prints every run of length >= minLen with a given diff:

static void findRuns(int[] a, int diff, int minLen) {
    int i = 0;
    while (i < a.length) {
        int j = i + 1;
        while (j < a.length && a[j] - a[j - 1] == diff) j++;
        int len = j - i;
        if (len >= minLen) System.out.println("run start=" + i + " len=" + len);
        i = j;
    }
}

Usage on a grid: for rows, call findRuns(grid[r], 1, 3) for each r. For columns, copy one column into a small buffer and call the same method. For diagonals, start from the first row and first/last columns to avoid duplicates, collect the diagonal into a buffer, then scan. If you want non-consecutive steps (say diff = 2), just change the diff argument.

For the follow-up, printing all 5-number combinations from 1..35 means C(35,5) = 324,632 lines. Use a backtracking generator that avoids duplicates and does not build huge intermediate lists:

static void combinations(int n, int k) {
    int[] pick = new int[k];
    dfs(1, n, 0, k, pick);
}
static void dfs(int start, int n, int depth, int k, int[] pick) {
    if (depth == k) {
        System.out.println(java.util.Arrays.toString(pick));
        return;
    }
    for (int i = start; i <= n - (k - depth) + 1; i++) {
        pick[depth] = i;
        dfs(i + 1, n, depth + 1, k, pick);
    }
}

Tip: pipe output to a file or count matches instead of printing to the console, which will be slow at this scale.

Recommended Answers

All 3 Replies

Well,you need to define different functions that check for different patterns.If the patterns is very large,then it would be tedious to write the amount of code to verify any pattern.For exampl to check if a line numbered n from a vxv dimensional array contains a pattern defined like:
n1,n2,n3 are related by n3-n2=n2-n1
Then a corresponding checking program would look like:

class PatternFinder{
    public static boolean pattern0(int[] x){
        /*Code to check if it is a pattern    * 
         *If the condition holds for the      *
         *first three numbers n1,n2,n3 then   *
         *check for n2,n3,n4,else return false*/
    }

    public static void main(String args[]){
        int[][] myarray={{1,2,3,4},
                         {9,12,15,18},
                         {10,12,20,76},
                         {-3,0,3,7}};
        int[] temp;
        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                temp[j]=myarray[i][j];
            }
            if(pattern0(temp)){
                System.out.println("Pattern found between");
                for(int v=0;v<j;v++)
                    System.out.print(temp[v] + "  ");

            }
        }
    }
}

Thank you so much, that helps so much!

I have one more question, if i had an array with the values 1,2,3.

How would I go about getting the out put of all possible scenarios via,

1 2
1 3
2 3

Except I am trying to do this with an array with the numbers 1-35, and 5 values would be printed.

Again Thank you so much you have already help me so much!

/*Say you have a one-dimensional array with non-repeating*
 *elements as follows: int[] v={1,2,3,4,5}.Then you would*
 *require nested loop(for inside for to accomplish it. */

 class Alpha{

    public static void main(String args[]){
        int[] v={1,2,3,4,5};
        for(int i=0;i<5;i++){
            for(int j=i+1;j<5;j++){  //Mark this as Flag0
                System.out.println(v[i] + " " + v[j]);
            }
        }
    }

}

This will print only pairs like (x,y) where x!=y and also (x,y) is the same as (y,x).To do this for a triplet or quad,you need to define three or four nested loops respectively.

Note:In the line marked as Flag0 changing the initialization condition for the for loop to this:
for(int j=i;j<5;j++)
will make the program print pairs of(x,y) where x=y

And writing this:
for(int j=0;j<5;j++)
will make it print all possible values(all 25 of them) where x may be equal to y and (x,y) and (x,y) will be printed.

Hope this answered your question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.