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 result = false;
  for (int i =0;i < nums.length-2;i++)
  if ( (nums[i] + 1 == nums[i+1]) && (nums[i+1] + 1 == nums[i+2]) )
  result = true;
  return result;
}


11 comments :

  1. If we take {1,2,3,4,5,6,7} then it shows FALSE.

    ReplyDelete
    Replies
    1. static 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;
      }

      Delete
  2. 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
  3. public boolean tripleUp(int[] nums) {
    for(int i = 0; i < nums.length - 2; i++) {
    if(nums[i] == (nums[i + 1] - 1) &&
    nums[i + 1] == (nums[i + 2] - 1)) {
    return true;
    }
    }
    return false;
    }

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

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

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

    ReplyDelete
  7. Could you solve this quesstion using Python? Thanks a lot:)

    ReplyDelete
  8. With Java Stream

    public static boolean tripleUp(int[] nums) {
    return IntStream.range(0, nums.length - 2).anyMatch(i -> nums[i] + 1 == nums[i+1] && nums[i+1] + 1 == nums[i+2]);
    }

    ReplyDelete
  9. public boolean tripleUp(int[] nums) {

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

    }
    return false;
    }

    ReplyDelete
  10. public boolean tripleUp(int[] nums) {
    int p=-1,bp=-1,c=-1;
    for(int i=0;i<nums.length;i++){
    bp=p;
    p=c;
    c=nums[i];
    if(bp==p-1&&p==c-1){
    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