Java > Array-2 > tripleUp (CodingBat Solution)

Problem:

Return true if the array contains, somewhere, three increasing adjacent numbers like .... 4, 5, 6, ... or 23, 24, 25.

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


Solution:

public boolean tripleUp(int[] nums) {
  boolean foo = false;
  int count = 0;
  
  for (int i = 0; i < nums.length; i++) {
    if (i < nums.length-1) {
      int sum = nums[i+1] - nums[i];
      if (sum == 1)
        count++;
      else
        count = 0;
    }
    if (count == 2)
      foo = true;
  }
  return foo;
}


10 comments :

  1. My solution:

    public boolean tripleUp(int[] nums) {

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

    return false;
    }

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

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

    ReplyDelete
  4. public boolean tripleUp(int[] nums) {
    boolean triple = false;
    for(int i=0; i1 && nums[i-2]+1==nums[i-1] && nums[i-1]+1==nums[i])
    triple = true;
    }
    return triple;
    }

    ReplyDelete
  5. public int[] shiftLeft(int[] nums) {
    int tmp;
    for (int i = 0; i < nums.length - 1; i++){
    tmp = nums[i];
    nums[i] = nums[i + 1];
    nums[i + 1] = tmp;
    }
    return nums;
    }

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

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

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

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

    return false;
    }

    ReplyDelete
  10. public boolean tripleUp(int[] nums) {
    boolean flag = false;
    int temp = 0;
    int temp2 = 0;
    for(int i=0; i<nums.length-2 ; i++){
    temp=nums[i]+1;
    temp2=nums[i]+2;
    if(nums[i+1]==temp && nums[i+2]==temp2){
    flag = true;
    }

    }
    return flag;
    }

    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