fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. public class Main
  8. {
  9. public static void main (String[] args) throws java.lang.Exception
  10. {
  11. int[] numbers = {23, 56, 67, 06, 43, 22, 59, 24, 90, 66};
  12. int[] lottery = {01, 06, 43, 22, 89};
  13.  
  14. for (int n = lottery.length; n > 1; n--) {
  15. for (int m = 0; m <= lottery.length - n; m++) {
  16. int ix = indexOf(lottery, m, m + n, numbers);
  17. if (ix > -1) {
  18. System.out.printf("Found subarray %s, width=%d from: %d to %d ",
  19. Arrays.toString(Arrays.copyOfRange(lottery, m, m + n)), n, m, m + n - 1);
  20. System.out.printf("at index: %d%n", ix);
  21. }
  22. }
  23. }
  24. }
  25.  
  26. static int indexOf(int search[], int from, int to, int[] input) {
  27. if (null == search || null == input || search.length > input.length) {
  28. return -1;
  29. }
  30.  
  31. for (int i = 0, n = input.length - (to - from); i <= n; i++) {
  32. boolean found = true;
  33. for (int j = from; found && j < to; j++) {
  34. if (input[i + j - from] != search[j]) {
  35. found = false;
  36. }
  37. }
  38. if (found) {
  39. return i;
  40. }
  41. }
  42. return -1;
  43. }
  44. }
Success #stdin #stdout 0.08s 34164KB
stdin
Standard input is empty
stdout
Found subarray [6, 43, 22], width=3 from: 1 to 3 at index: 3
Found subarray [6, 43], width=2 from: 1 to 2 at index: 3
Found subarray [43, 22], width=2 from: 2 to 3 at index: 4