0

I defined an array which contains random numbers.I get some numbers from the user and put them in other array.I want to check whether first array contains the second array in order.My code is not enough to check order.It is just checking contains or not. EX:

arr1=[45, 21,1,19 ,8,90,21,2] ,arr2=[21,1,19] result=TRUE
arr1=[45,21,1,19,8,90,21,2] ,arr2=[21,19] result=FALSE

 public class Main  {

 private static int[] array1;

 public static int[] list() {
     array1 = new int[10];
        for(int i=0;i<array1.length;i++)
        {
            array1[i] = randomFill();
        }
        return array1;
    }



 public static void print()
 {
        for(int n: array1)
        {
        System.out.println(n+" ");
        }
 }

 public static int randomFill()
 {

        Random rand = new Random();
        int randomNum = rand.nextInt(90)+10; //to generate two digits number which is between 10-99 to the array
        return randomNum;
 }

 public static void main(String args[])
 {

     list();
     print();

     Scanner s=new Scanner(System.in);

        System.out.println("How many numbers will you add? ");

        int n=s.nextInt();

        int array2[]=new int[n];

        System.out.println("Enter the numbers:");

        for(int i=0;i<n;i++){//for reading array
            array2[i]=s.nextInt();

        }

        for(int i: array2){ //for printing array

            System.out.println(i);

        }
        int result=0;
       for(int i=0;i<array1.length;i++)
       {
           for(int j=0;j<array2.length;j++)
           {

               if(array1[i]==array2[j])
               {
                   result=result+1;

               }

            }

       }
       if(result==n)
       {

           System.out.println("TRUE");
       }
       else
       {
           System.out.println("false");
       }



 }

}

tung
  • 719
  • 2
  • 14
  • 30
Ali Göktaş
  • 15
  • 1
  • 7
  • "Here ya go... (Although your professor may want you to actually try doing it with techniques taught in your classroom)" `System.out.println(Arrays.toString(array1).contains(Arrays.toString(array2).replace("[","").replace("]",""))?"TRUE":"FALSE");` – phatfingers Apr 04 '18 at 19:42

1 Answers1

1

Here is the hard way without any helper

    Integer i,j,k;
    Boolean found=false;

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

        // we look for the index of the first element of the second array in the original one
        if(arr2[0] == arr1[i] ) {

            found=true;

            // after that we iterate in both arrays simultaneously
            for(j=1, k=i+1; j<arr2.length; j++,k++) {
                // if we complete the iteration of the original array
                // or if we find any two elements not equal we break 
                // we set then the boolean variable to false
                if( k>=arr1.length || arr2[j] != arr1[k] ) {
                    found=false;
                    break;
                }
            }

            if(found) break;
        }    
    }

    System.out.println(found);
Hayi
  • 6,972
  • 26
  • 80
  • 139
  • 1
    This could fail if the first element of the subarray is present multiple times in the original array. – Vasan Apr 04 '18 at 20:03
  • Pretty nice technique.. but a little explanation to OP (even as little as a few lines of comment) would definitely be more satisfactory. – Taslim Oseni Apr 04 '18 at 20:04
  • @Vasan i made some adjustment – Hayi Apr 04 '18 at 20:49
  • @Youssef I don't think you've addressed the problem even now. Try this:`arr1=[2,1,0,2,3,4]` and `arr2=[2,3,4]`. – Vasan Apr 04 '18 at 21:01
  • It clearly doesn't. But if you need proof, [here](https://ideone.com/e5GWf5). – Vasan Apr 04 '18 at 21:09
  • Come to think of it, there are other things wrong here too. You check `k` against `arr2` length but `k` starts from `i+1` which is from the source array. What if the source array is huge and subarray starts near the end? – Vasan Apr 04 '18 at 21:16
  • i fix it i think @Vasan – Hayi Apr 04 '18 at 21:22