Finding occurrences of element before and after the element in Array

Posted by user3718040 on Stack Overflow See other posts from Stack Overflow or by user3718040
Published on 2014-06-07T15:18:04Z Indexed on 2014/06/07 15:24 UTC
Read the original article Hit count: 154

Filed under:
|

I am writing a algorithm, if an array contain 3 does not contain between two 1s.
like this

int array={5, 2, 10, 3, 15, 1, 2, 2}

the above array contain 3, before 3 there is no 1 and after 3 is one 1 it should return True.

int array={3,2,18,1,0,3,-11,1,3}

in this array after first element of 3 there is two 1 it should return False.
I have try following code

public class Third {  
    public static void main(String[] args){  
        int[] array = {1,2,4,3, 1}; 

        for(int i=0;i<array.length;i++)
        {
            if(array[i]==3)
            {
                for(int j=0;j<array[i];j++)
                {
                    if(array[j]==1)
                    {

                         System.out.println("I foud One before "+array[j]); 


                    }else
                    {
                        break;
                    }
                System.out.println("yes i found the array:"+array[i]);
                }
                for(int z=0;z>array[i];z++)
                {
                    if(array[z]==1)
                    {
                        System.out.println("I found after 3 is :"+array[z]);
                    }
                    break;
                }
            }
        }
    }
}  

I am not getting exact result from my above code which i want.

© Stack Overflow or respective owner

Related posts about java

Related posts about arrays