Java > Array-2 >modThree (CodingBat Solution)

Problem:

Given an array of ints, return true if the array contains either 3 even or 3 odd values all next to each other.

modThree({2, 1, 3, 5}) → true
modThree({2, 1, 2, 5}) → false
modThree({2, 4, 2, 5}) → true


Solution:

public boolean modThree(int[] nums) {
  boolean result = false;
  for (int i = 0;i<nums.length-2;i++)
  if( (nums[i] % 2 == 0 && nums[i+1] % 2 == 0 && nums[i+2] % 2 == 0)|| (!(nums[i] % 2 == 0) && !(nums[i+1] % 2 == 0) && !(nums[i+2] % 2 == 0)))
    result = true;
    
    return result;
        
}



15 comments :

  1. public boolean modThree(int[] nums) {
    int even = 0;
    int uneven = 0;
    for (int i: nums)
    {
    if (i%2==0)
    {
    uneven = uneven*0;
    even++;
    }
    if (i%2!=0)
    {
    even = even*0;
    uneven++;
    }
    if (even==3 || uneven==3) return true;
    }
    return false;
    }

    ReplyDelete
  2. public boolean modThree(int[] nums) {
    int current = -1;
    int previous = -1;
    int beforePrevious = -1;
    for(int i : nums) {
    beforePrevious = previous;
    previous = current;
    current = i%2;
    if(current == previous && previous == beforePrevious) return true;
    }
    return false;
    }

    ReplyDelete
    Replies
    1. Where did you get this style of programming? It is very interesting. Thanks.

      Delete
  3. public boolean modThree(int[] nums) {
    boolean even = false;
    boolean odd = false;
    for (int i = 0; i < nums.length; i++) {
    if (nums[i] % 2 == 0) {
    if (nums.length - 1 > i && nums[i + 1] % 2 == 0) {
    if (nums.length - 2 > i && nums[i + 2] % 2 == 0) {
    even = true;
    }
    }
    }
    if (nums[i] % 2 != 0) {
    if (nums.length - 1 > i && nums[i + 1] % 2 != 0) {
    if (nums.length - 2 > i && nums[i + 2] % 2 != 0) {
    odd = true;
    }
    }
    }
    }
    return (even || odd);
    }

    ReplyDelete
  4. public boolean modThree(int[] nums) {

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

    ReplyDelete
  5. public boolean modThree(int[] nums) {
    boolean bool = false;

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

    }

    ReplyDelete
  6. public boolean modThree(int[] nums) {
    int count=0, count2=0;
    for(int i=0; i=3 || count2>=3) return true;
    else
    return false;
    }

    ReplyDelete
  7. Why only half of my code is getting published each time...Can anyone help me with this?

    ReplyDelete
  8. I know answers already exist, this was just a clearer code to explain what is happening.

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

    ReplyDelete
  9. public boolean modThree(int[] nums)
    {
    for(int i = 2; i < nums.length; i++)
    {
    if(nums[i-2] % 2 == 0 && nums[i-1] % 2 == 0 && nums[i] % 2 == 0) return true;
    if(nums[i-2] % 2 == 1 && nums[i-1] % 2 == 1 && nums[i] % 2 == 1) return true;
    }

    return false;
    }

    ReplyDelete
  10. public boolean modThree(int[] nums) {
    int length = nums.length;
    int evenCounts = 0;
    int oddCounts = 0;
    boolean isPass = false;


    for(int index = 0; index < length; index++) {

    if(nums[index] % 2 == 0) {

    evenCounts += 1;
    oddCounts = 0;
    }
    else
    {
    oddCounts += 1;
    evenCounts = 0;
    }

    if(evenCounts == 3 || oddCounts ==3) {
    isPass = true;
    }

    }

    return isPass;
    }

    ReplyDelete
  11. public boolean modThree(int[] nums) {
    int ce = 0, co = 0, i = 0;

    while (i<nums.length){
    while (i<nums.length && nums[i] % 2 == 0){
    ce++;
    i++;
    if(co != 3) co = 0;
    }
    while (i<nums.length && nums[i] % 2 != 0){
    co++;
    i++;
    if(ce != 3) ce = 0;
    }
    }
    return ce == 3 || co == 3;
    }

    ReplyDelete
  12. public boolean modThree(int[] nums) {
    for(int i=0;ij && nums[j]%2!=0){
    count++;
    }
    }
    }
    if(nums[i]%2==0){
    for(int j=i;jj&& nums[j]%2==0){
    count++;
    }
    }
    }
    if(count==3){
    return true;
    }
    }
    return false;
    }

    ReplyDelete
  13. With Java Stream

    public boolean modThree(int[] nums) {
    boolean result = java.util.stream.IntStream.range(0, nums.length - 2)
    .anyMatch(i -> nums[i] % 2 == 0 && nums[i+1] % 2 == 0 && nums[i+2] % 2 == 0 ||
    !(nums[i] % 2 == 0) && !(nums[i+1] % 2 == 0) && !(nums[i+2] % 2 == 0));

    return result;
    }

    ReplyDelete
  14. public boolean modThree(int[] nums) {
    for(int i = 0; i < nums.length - 2; i++) {
    if(nums[i] % 2 == 0 && nums[i+1] % 2 == 0 && nums[i+2] % 2 == 0) return true;
    if(nums[i] % 2 != 0 && nums[i+1] % 2 != 0 && nums[i+2] % 2 != 0) return true;
    }
    return false;
    }

    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