Given an array of ints, return true if the value 3 appears in the array exactly 3 times, and no 3’s are next to each other.





Following are the test cases

Test cases Result Status
haveThree({12,3,4,3,88,3}) true OK
haveThree({22,3,3,3,17,3}) false OK
haveThree({3,4,3,6,3,7}) true OK

Java solution


package test;

public class Program20 {
	public static void main(String[] args) {
		// 3, 1, 3, 1, 3
		int a[] = {3,4,3,6,3,7};
		Program20.haveThree(a);
	}

	public static boolean haveThree(int[] nums) {
		for (int i = 1; i < nums.length-1; i++) {
			if (nums[i] == 3 && nums[i-1] != 3 && nums[i+1] != 3) {
				return true;
			}
		}
		return false;
	}
}




About the author

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

View all posts

Leave a Reply

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