-3

Current Outputs:

Is [1, 2, 3] found in [1, 2, 1, 2, 3] ? >>> false
Is [1, 1, 3] found in [1, 2, 1, 2, 3] ? >>> false

Needed Outputs:

Is [1, 2, 3] found in [1, 2, 1, 2, 3]? >>> true
Is [1, 1, 3] found in [1, 2, 1, 2, 3]? >>> false

This is an example of a general output of the code, it needs appear consecutively and in the same order. I will show my code, all I want is guidance and teaching, no cheating due to that being academic dishonesty. I hope to find some tasteful insight, and furthermore get my code running properly.

Java Code:

public static boolean contains(int[] a, int[] b)
    {
        int acounter = 0;
        int bcounter = 0;
        boolean tempcounter = true;
        while (acounter < a.length)
        {
            if (a[acounter] ==  b[bcounter])
            {
                if (bcounter == b.length - 1)
                    return true;
                bcounter += 1;
                tempcounter = true;
            }
            else
            {    
                if (tempcounter && acounter > 0)
                {
                    acounter -= 1;
                }
                bcounter = 0;
                tempcounter = false;
            }
            acounter += 1;
        }
        return false;
    }

Main Method:

import java.util.*;
public class PracticeProblems
{
    public static void main(String[] args)
    {
        int[] a = {1,2,3};
        int[] b = {1,2,1,2,3};
        int[] c = {1,1,3};
        int[] d = {1,2,1,2,3};
        System.out.println("\n\nIs " + Arrays.toString(a) + " found in " + Arrays.toString(b) + " ? " + ">>> " + contains(a, b));
        System.out.println("Is " + Arrays.toString(c) + " found in " + Arrays.toString(d) + " ? " + ">>> " + contains(c, d) );
      }
}
1
  • Normally a method like contains(a, b) should be read as 'a contains b'. So that means a is the larger array and b is the sub-array that might or might not be found in a. I think you have this the wrong way round in your example code. Commented Mar 31 at 9:39

4 Answers 4

2

Your logic uses a single loop in which it juggles with indexes. That is complicated if not incomprehensible.

Look for the algorithm, the logic. What would you do manually?

part:  1+2+3- 
       | 1-2 3
       | | 1+2+3+!
all:   1 2 1 2 3

So in one loop over the larger array you would shift the smaller array inside the larger.

And inside the loop you would need to match elements of the smaller array, a second loop over the smaller array.

Sign up to request clarification or add additional context in comments.

Comments

0

I think this is the basic algorithm:

boolean contains(int[] a, int[] b) {

    int indexA = 0
    int indexB = 0  

    while (indexA < a.length) {
        
        if (a[indexA] != b[indexB]) {
            indexB = 0
        }
        
        if (a[indexA] == b[indexB]) {
            indexB++    
            if (indexB == b.length) {
                return true;
            }
        }       
        
        indexA++
    }
    return false;
}

The 'trick' is to first check the reset condition. That is, when matches are being found, but then a mismatching value is encountered, and the indexB has to be reset to 0.

By doing this first, we achieve that after the reset, the same mismatching value of a is still tried as a possible start value for matching b.

Comments

0

If you analyze the code below these lines, you'll see that it does exactly the same thing as the code provided by Adriaan Koster. I think presented this way, it's easier to interpret.

The outer loop determines from which position in the containing array we will search for matches with the contained array. If a mismatch is found, "break" is called, and the check is repeated from the next position. If the end of the inner for is reached without calling "break," we return "true."

If the outer for reaches its end, we return "false".

public boolean contains( int[] arrA, int[] arrB ) {
   for( int i = 0; i < arrB.length; i ++ ) {
      for( int j = 0; j < arrA.length; j ++ ) {
         if( arrB[ i + j ] != arrA[ j ] ) {
            break;
         }
         if( j == arrA.length - 1 ) {
            return true;
         }
      }
   }
   return false;
}

Comments

-3
public static boolean contains(int[] a, int[] b)
    {
        int left = 0;
        int right = a.length-1;
        while(right<b.length)
        {
            boolean flag = false;
            int temp = left;
            while(temp<=right)
            {
                if(a[temp-left]!=b[temp])
                {
                    flag = true;
                    break;
                }
                else temp++;
            }
            if(!flag) return true;
            right++;
            left++;
        }
        return false;
    }

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.