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;
}


18 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
  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

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