Write a class SequenceCheck with a public method sequenceCheck that takes one parameter arr of type int[] and returns true if 6, 9, 12

Write a class SequenceCheck with a public method sequenceCheck that takes one parameter arr of type int[] and returns true if 6, 9, 12 present consecutively in the arr. The return type of sequenceCheck should be boolean.

Assumptions:

  1. arr is never null
  2. Elements 6, 9, 12 are appear consecutiviely

Here are examples:

Cmd Args : 62 32 6 9 12
true
Cmd Args : 99 36 6 12 56 9
false

Solution

public class SequenceCheckMain{
	public static void main(String[] args){
			int[] arr = new int[args.length];
			for (int i = 0; i < args.length; i++)
			{
		     arr[i] = Integer.parseInt(args[i]);
			}
			SequenceCheck sc = new SequenceCheck();
			boolean result = sc.sequenceCheck(arr);
			System.out.println(result);
	}

	public boolean sequenceCheck(int[] arr) {
		
		//Write your code here
		
		int size = arr.length;
		int count = 1;
		boolean result = false;
		
		for(int i = 0; i < size-1; i++){
			
			if(arr[i] == 6){
				
				if(arr[i+1] == 9 && arr[i+2] == 12){
					
					result = true;
				} else{
					
					return false;
				}
			}
		}
		return result;
		
	}
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *