-1

problem

Given an array of ints, return true if every 2 that appears in the array is next to another 2.

  • twoTwo({4, 2, 2, 3})true

  • twoTwo({2, 2, 4})true

  • twoTwo({2, 2, 4, 2})false

my code is only mising this case

twoTwo({2, 2, 7, 2, 1}) → false; but returns true;

my code

public boolean twoTwo(int[] nums) {
    int notFound = 0;
    int i = 0;
    boolean found = false;
    if (nums.length == 0) {
        return true;
    }
    if (nums.length == 1 && (nums[0] != 2)) {
        return true;
    }

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

        if ((nums[i] == 2 && nums[i + 1] == 2)) {
            found = true;
        }
        if (nums[nums.length - 1] == 2 && nums[nums.length - 2] != 2) {
            return false;
        }
        if (nums[i] != 2) {
            notFound++;
        }


    }

    if (nums[i] != 2) {
        notFound++;
    }

    if (notFound == nums.length) {
        return true;
    }
    return found;
}
GrandMasterFlush
  • 6,269
  • 19
  • 81
  • 104
Bodhert
  • 17
  • 1
  • 6

2 Answers2

1

There is never a "wrong" way to code a working solution, but there are bad ways. In your solution, I think you try to handle every individual case in chaotic ways instead of tackling the overarching problem. You have floating variables all over the place and hard coded numbers that are very specific to each case. You have unnecessary and excessive returns.

My suggestion is to work on solving your own question "Return true if all 2's are next to another 2" - instead of trying to code for each specific case. You aren't REALLY solving a problem if you are hard coding to work on a specific subset of that problem.

Just my critique; keep working at it.

Consider refactoring your for loop with this as a starting point, see if you can figure out the logic (semi pseudo code):

for(int i = 1; i < nums.length-1; i++) { // Why do I start i at 1?
  if(nums[i]==2) {
    if(nums[i-1] == 2 || nums[i+1] == 2) // What does this if check?
      do something; // What to do here?  Look up the 'continue' keyword.
    else
      return false;
  }
}
return true;

You will find this for loop is JUST a starting point. There will be more needed to add, but hopefully a good jumping point for you.

Best of luck!

lmcphers
  • 468
  • 3
  • 18
  • 1
    I will keep your critique in mind , thanks for that, it is really helpfull :) – Bodhert Jun 16 '15 at 23:09
  • You're welcome! I hope you enjoy learning Java - it is a very robust and fun language. Just keep in mind that the proper way of saying thank you on StackOverflow is to upvote and accept an answer. – lmcphers Jun 16 '15 at 23:12
  • 1
    i can not t vote yet , but i will acepeted like the solution until i can vote. – Bodhert Jun 16 '15 at 23:16
  • 1
    Do you mean to return true after the loop is over? – Daemedeor Jun 16 '15 at 23:30
  • @Daemedeor who are you asking to? – Bodhert Jun 16 '15 at 23:36
  • 1
    @AlejandroCordobaBodhert Imcphers, i know its psuedocode but it would be useful for people to not try run return true; inside the for loop... – Daemedeor Jun 16 '15 at 23:38
  • You are correct Daemedeor, sorry, I was writing this in my original draft where I included some of the class information outside the for loop so I misplaced my return. Thank you! I've corrected my edit. – lmcphers Jun 17 '15 at 00:47
0
public boolean twoTwo(int[] nums)
{
    if (nums.length == 1 && nums[0] == 2)
        return false;

    for (int i = 0; i < nums.length - 1; i++)
        if (nums[i] == 2)
            if (nums[i + 1] != 2 && (i > 0 && nums[i - 1] != 2))
                return false;
    return true;
}

Basically this goes through each number in the list, and if it finds a 2, it checks it against the previous and next numbers. That's all it does.

Jashaszun
  • 9,207
  • 3
  • 29
  • 57
  • Using `i++` twice here is a bit obnoxious in your for loop. My main critique with your suggestion. – lmcphers Jun 16 '15 at 23:08
  • @Imcphers Yeah, I guess it isn't really needed, as it would work just fine without. However, it speeds it up by a factor of 2 in the case of an array full of 2s. :) – Jashaszun Jun 16 '15 at 23:11
  • I understanded and submmited your solution, but its not working for all the cases, but thank you for the help, i will try to improve it. – Bodhert Jun 16 '15 at 23:13
  • I *think* you can use `i+=2` in the for loop declaration instead of `i++`. I know that's not 'traditional', but probably will achieve what you're looking for. I don't know, I've never tried that. – lmcphers Jun 16 '15 at 23:15
  • @Imcphers If you had `i+=2` as the step, then you could skip over a 2 in the array if it is in an odd index. Note that my `i++` was run IFF we found a 2 in the current index. – Jashaszun Jun 16 '15 at 23:17
  • If you skip over a two, but the previous number was not a 2, then I think it's safe to assume that it will fail the `if` check, so it doesn't actually matter. – lmcphers Jun 16 '15 at 23:18
  • @Imcphers I'm talking about, for example, this array: `{ 0 0 0 2 0 0 }`. We see the first `0`, we're good, so we move to `i=2`. We see another `0`, so we move to `i=4`. We see another `0`, move on, and we're done! But we skipped right over that 2. – Jashaszun Jun 16 '15 at 23:20
  • Oh, I forgot how you are if checking and was comparing to how I was doing it. Sorry =P – lmcphers Jun 16 '15 at 23:22
  • @Imcphers But in the case I just described, we would not have seen a lone 2, which is specifically what we need to find. Are you sure you're not inverting the question? – Jashaszun Jun 16 '15 at 23:24
  • @Jashaszun wouldn't that not compile you need the braces for multiple lines inside tests right? – Daemedeor Jun 16 '15 at 23:32
  • 1
    This solution fails for any input array that has a lone 2 as the last element. – Erick G. Hagstrom Jun 16 '15 at 23:47