3

I have the following problem that I tried to solve, but I cannot come with a solution:

Write in a class a method that receives as parameters two arrays of numbers a and b and returns true if the elements of b are found in a in the order of their appearance and false otherwise.

If the first string has length 0 the method returns false. If the second string is 0, the method returns true.

The signature of the method is: public static boolean contains(int[] a, int[] b)

Ex:

contains({1, 2, 3, 4, 2, 1, 3, 4}, {2, 3, 4}) -> true

contains({1, 2, 3, 4, 2, 1, 3, 4}, {1, 4, 3}) -> true

contains({1, 2, 3, 4, 2, 1, 3, 4}, {3, 3, 2}) -> false

contains({1, 2, 3, 4, 2, 1, 3, 4}, {1, 1, 2}) -> false

contains({1, 2, 3, 4, 2, 1, 3, 4}, {}) -> true

contains({}, {1, 2, 3}) -> false

My solution until now:

public class TestEA8 {
public static boolean contains(int[] a, int[] b) {
    int i=0;
    int j=0;
    boolean result = false;

    if(a.length == 0){
        System.out.println(false);
        return false;
    }else if(b.length ==0){
        System.out.println(true);
        return true;}

    for (i=0;i<b.length;i++){
        for (j=0;j<a.length;j++){
            if (b[i] == a[j]){
                result = true;
                break;
                }
            }
            if(j==a.length){
                result =false;
            }
        }
        System.out.println(result);
        return  result;
    }
}

Thank you,

2

3 Answers 3

2

You can use Collections.indexOfSubList() method.

Example:

public static boolean contains(int[] a, int[] b) {
    return findArray(a, b) > -1;
}

public static int findArray(int[] a, int[] b) {
    return Collections.indexOfSubList(arrayToList(a), arrayToList(b));
}

public static List<Integer> arrayToList(int[] array) {
    return Arrays.stream(array)
        .boxed()
        .collect(Collectors.toList());
}
Sign up to request clarification or add additional context in comments.

Comments

1

This Code will solve your problem:

use a variable temp where it will be the tracking index of first array and moves forward in every check.

 public static boolean contains(int[] a, int[] b) {
    int i=0;
    int j=0;
    boolean result = false;

    if(a.length == 0){
        System.out.println(false);
        return false;
    }else if(b.length ==0){
        System.out.println(true);
        return true;}

    int count = 0;
    int temp = 0;
    for (i=0;i<b.length;i++){
        for (j= temp;j<a.length;j++){
            if (b[i] == a[j]){
                count = count+1;
                temp++;
                break;
            }
            temp++;
        }
    }

    if (count == b.length){
        result = true;
    } else {
        result =false;
    }
    System.out.println(result);
    return  result;
}

Comments

0

You can walk through the first array, "ticking out" the expected element from the second one when you encounter it. If you reach the end of the second array, return true, otherwise false:

public static boolean contains(int[] a, int[] b) {
  int i=0,j=0;
  while(i<a.length && j<b.length){
    if(a[i]==b[j])
      j++;
    i++;
  }
  return j==b.length;
}

Test with the provided example data: https://ideone.com/JRBvkm

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.