Java > Array-2 > haveThree (CodingBat Solution)

Problem:

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.

haveThree({3, 1, 3, 1, 3}) → true
haveThree({3, 1, 3, 3}) → false
haveThree({3, 4, 3, 3, 4}) → false


Solution:

public boolean haveThree(int[] nums) {
  int count = 0;
  boolean found = false;
  
  for (int i = 0; i < nums.length; i++) {
    if (nums[i] != 3)
      found = false;
    if (nums[i] == 3 && found == true)
      return false;
    if (nums[i] == 3 && found == false) {
      found = true;
      count++;
    }
  }
  if (count == 3)
    return true;
  else
   return false;
}


30 comments :

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

    for(int i=0; i<nums.length; i++){
    if(nums[i] == 3) three++;
    }

    return three == 3;
    }

    ReplyDelete
  2. public boolean haveThree(int[] nums) {
    int count = 0;

    if(nums.length < 3) return false;
    else {
    for(int i = 0; i < nums.length-1; i++) {
    if(nums[i] == 3 && nums[i+1] == 3) return false;
    else if(nums[i] == 3) ++count;
    }
    if(nums[nums.length-1] == 3) ++ count;
    }

    return count == 3;
    }

    ReplyDelete
  3. public boolean haveThree(int[] nums) {
    boolean aux=false,aux2=true;
    int cont=0;

    for(int i=0;i<nums.length;i++)
    {
    if(nums[i]==3)
    {
    cont+=1;
    }

    }
    for(int i=0;i<nums.length-1;i++)
    {
    if(nums[i]==3 && nums[i+1]==3)
    {
    aux2=false;
    break;
    }

    }


    if(cont==3 && aux2)
    {
    aux=true;
    }


    return aux;
    }

    ReplyDelete
  4. public boolean haveThree(int[] nums) {
    int count = 0;

    if (nums.length <= 1) return false;

    if (nums[0] == 3 && nums[1] != 3) count ++;
    if (nums[nums.length-1] == 3 && nums[nums.length-2] != 3) count ++;


    for (int x = 1; x < nums.length-2; x++){
    if (nums[x-1] != 3 && nums[x]==3 && nums[x+1] != 3 )
    count++;
    }

    return (count == 3);
    }

    ReplyDelete
  5. // public boolean haveThree(int[] nums) {
    // int counter = 0;
    // for(int i = 0; i < nums.length; i++) {
    // int num = nums[i];
    // if(num == 3) {
    // if(i > 0 && nums[i - 1] == 3) {
    // return false;
    // }
    // counter++;
    // }
    // }
    // if(counter == 3) {
    // return true;
    // }
    // return false;
    // }

    ReplyDelete
  6. public boolean haveThree(int[] nums) {
    int count3 = 0;
    boolean next3 = true;

    for(int i = 0; i < nums.length; i++){
    if(nums[i] == 3){
    count3++;
    if(i < nums.length - 1 && nums[i+1] == 3){
    next3 = false;
    }
    }
    }

    return count3 == 3 && next3 == true;
    }

    ReplyDelete
  7. public boolean haveThree(int[] nums) {
    int count = 0;
    for (int i = 0; i < nums.length; i++) {
    if ( nums[i] == 3 ) {
    count++;
    i++;
    }
    }
    return count == 3;
    }

    ReplyDelete
    Replies
    1. Very short, but if something like [1, 3, 3, 3, 3, 3, 1] appears it would return "true" instead of "false". Althought the tests in Codingbad miss to cover this case.

      Delete
    2. nice and yes hopper case is missed

      Delete
  8. public boolean haveThree(int[] nums) {

    int count = 0;

    Boolean index = false;

    List lista = new ArrayList<>();

    for (int i=0; i<nums.length; i++) {

    if (nums[i]==3) {

    count++;

    lista.add(i);
    }

    }

    for (int i=0; i<lista.size()-1; i++) {

    if (lista.get(i+1)-lista.get(i)==1)

    index = true;

    }
    return (count==3 && !index);
    }

    ReplyDelete
  9. public boolean haveThree(int[] nums) {
    boolean a=false;
    int i=0;
    int count=0;
    while(i<nums.length){
    if(nums[i]==3){
    i+=2;
    count++;
    }
    else if(nums[i]!=3)
    i++;
    }
    if(count==3)
    a=true;
    return a;

    }

    ReplyDelete
  10. Does anyone know why this code fails to work? It passes all the tests except for the hidden ones.

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

    ReplyDelete
  11. int count = 0;
    String arrS = "";
    for(int i : nums)
    {
    if(i==3)count++;
    arrS = arrS + String.valueOf(i);
    }
    return (count==3&&arrS.matches("(.*)3\\d3(.*)")&&!arrS.contains("33"));

    ReplyDelete
  12. This is my solution.

    public boolean haveThree(int[] nums) {
    boolean NoUpper=false;
    int counter=0;
    for(int i=0;i<nums.length;i++){

    if(i+1<nums.length){
    if(nums[i]==3 && nums[i+1]==3) {
    NoUpper=true;
    }
    }

    if(nums[i]==3 && !NoUpper) counter++;

    }


    return counter==3;
    }

    ReplyDelete
  13. BEST SOULUTION:

    public boolean haveThree(int[] nums) {
    int count = 0;

    for(int i = 0; i < nums.length; i++){
    if(nums[i] == 3){
    count++;
    i++;
    }
    }
    if(count == 3){
    return true;
    } else {
    return false;
    }

    }

    ReplyDelete
  14. public boolean haveThree(int[] nums) {
    int count = 0;
    for(int i=0; i<nums.length; i+=1){
    if(nums[i]==3){
    count++;
    i+=1;
    }
    }
    if(count==3)
    return true;
    return false;
    }

    ReplyDelete
  15. int count=0;
    for (int k = 0; k < nums.length; k++) {
    if(nums[k]==3) count++;
    }
    for (int i = 0; i < nums.length-4; i++) {
    if(count>3) return false;
    if(nums[i]==3 && nums[i+2]==3 && nums[i+4]==3)
    return true;
    }
    return false;
    }

    ReplyDelete
  16. public boolean haveThree(int[] nums) {
    int count = 0;
    for (int i = 0; i < nums.length; i++)
    {
    if (i + 1 < nums.length && nums[i] == 3 && nums[i + 1] == 3)
    {
    return false;
    }
    else if (nums[i] == 3)
    {
    count += 1;
    }
    }
    return count == 3;
    }

    ReplyDelete
  17. public boolean haveThree(int[] nums) {
    int count = 0;
    for(int i = 0; i < nums.length; i++){
    if(nums[i] == 3){
    count++;
    i++;
    }
    }
    return count == 3;
    }

    ReplyDelete
  18. public boolean haveThree(int[] nums) {
    int count = 0;
    boolean next = true;
    for(int i = 0; i < nums.length; i++){
    if(nums[i] == 3){
    count++;
    if(i<nums.length - 1 && nums[i+1] ==3){
    next = false;
    }
    }
    }
    return count == 3 && next;
    }

    ReplyDelete
  19. int count=0;
    for(int i=0; i<nums.length; i++){
    for(int k=i+1; k<nums.length; k++){
    if(nums[i]==3 && nums[k]!=3)
    count++;
    }

    }return count==3;

    ReplyDelete
  20. public boolean haveThree(int[] nums) {
    int c=0;
    for(int i=0;i<nums.length-1;i++){
    if(nums[i]==3&&nums[i+1]==3) return false;
    }
    for(int i=0;i<nums.length;i++){
    if(nums[i]==3) c++;
    }
    return (c==3);
    }

    ReplyDelete
  21. for python makers cuz no more python tasks on coding bat:

    def haveThree(n):
    c = 0
    b = False
    if n[len(n)-1] == 3:
    c += 1
    for i in range(len(n)-1):

    if n[i] == 3 and n[i+1] == 3:
    return b
    break
    if n[i] == 3:
    c += 1
    if c == 3:
    b = True
    return b

    ReplyDelete
  22. public boolean haveThree(int[] nums) {
    boolean x = false;
    int c =0;
    for(int i =0 ; i<nums.length;i++){
    if(i<nums.length-1&&(nums[i]==3&&nums[i+1]==3))
    x = true;
    if(nums[i]==3)
    c++;
    }
    return(x==false&&c==3);

    ReplyDelete
  23. int count=0;
    for(int i=0; i<nums.length-4; i++){
    if(nums[i]==3 && nums[i+2]==3 && nums[i+4]==3){
    count++;}
    }if(count==1)return true;
    return false;

    ReplyDelete
  24. public boolean haveThree(int[] nums) {
    int count = 0 ;

    for ( int x = 0; x < nums.length; x++){
    if (nums[x] == 3){
    count++;
    if (x > 1 && nums[x-1] == 3){
    count = 0;
    break;
    }
    }
    }
    return (count == 3);
    }

    ReplyDelete
  25. more simple to underistand:


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

    return false;
    }

    ReplyDelete
  26. I think this codes makes work easier and simple to understand
    public boolean haveThree(int[] nums) {
    int count = 0;
    for(int i = 0; i < nums.length; i++) {
    if(nums[i] == 3) {
    count++;
    i++;
    }
    }
    return count == 3;
    }

    ReplyDelete
  27. public boolean haveThree(int[] nums) {
    int count=0,prev=0;
    for(int i=0;i<nums.length;i++){
    if(prev!=nums[i]&&nums[i]==3){
    count++;
    }
    prev=nums[i];

    }
    return count==3;
    }

    ReplyDelete
  28. public boolean haveThree(int[] nums) {
    int count =1;
    for(int i=0 ; i<nums.length-2 ; i++){

    if(nums[i]==3){

    if( (nums[i+1]!=3 && nums[i+2]==3)){
    count++;
    }
    }
    }
    return count==3;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact