1

Codingbat.com array question

This is a simple array question, not for homework, just for general knowledge before I take another programming class next fall.

Given an array of ints, return true if 6 appears as either the first or last element in the array. The array will be length 1 or more.

firstLast6({1, 2, 6}) → true
firstLast6({6, 1, 2, 3}) → true
firstLast6({3, 2, 1}) → false

The problem I have is that you are not supposed to use any loops to traverse the array. How can this be written to avoid an index out of bounds exception if I don't know the total number of ints in the input data?

My solution --- it works but not exactly the answer they were looking for.

public boolean firstLast6(int[] nums) {
  for (int i=0; i < (nums.length ); i++)
  {

  if (i == 0 && nums[i] == 6)
  { 
  return true;
  }

  else if (i == (nums.length - 1) && nums[i] ==6)
  {

  return true;
  }
  }
  return false;
}
1
  • I think the array length is already known to you as you have written: nums.length then whats the problem?? just use nums[0] for first element and nums[nums.length-1] for last element. Commented May 15, 2011 at 5:19

7 Answers 7

4

You would use the length property to access the last index:

public boolean firstLast6(int[] nums) {
    if (nums == null || nums.length == 0) {
        return false;
    }

    return nums[0] == 6 || nums[nums.length - 1] == 6;
}

EDIT: added check for null or empty array.

Sign up to request clarification or add additional context in comments.

3 Comments

That's a lot easier than the method I was using. I wasn't aware that you could return a boolean like that. Thanks for your help.
@Peter thanks, updated. @user733952 sure! Essentially you're returning the result of the logical condition, which is a boolean. You could store the result in a variable then return that, but for such a short piece of code this approach is pretty straightforward.
Adding special checks for nulls is usually just masking programming bugs :) (sorry for nitpicking, I'm shutting up now)
1

Simply retrieve the first array element, and the last array element. That is:

nums[0]
nums[nums.length - 1];

Comments

1

Hey , you don't need any loops for this . All you have to do is :

if(array[0]==6 || array[array.length-1]==6){
    return true ;
}
  else{
     return false ; 
 }

Comments

0

The most compact and efficient expression of this would be

public void firstLast(int[] a) {
  return a.length > 0 && (a[0] == 6 || a[a.length-1] == 6);
}

Comments

0

the output can be easily obtained by

if(array[0]==6||array[nums.length]==6)
return true;
else
return false;

Comments

0

this is full code for this problem,i hope this gonna help you

import java.util.Scanner;


public class pair13 {


public static void main(String[] args) {

    int[] number=new int[6];



    Scanner ss=new Scanner(System.in);

    for (int j = 0; j < number.length; j++) {
        number[j]=ss.nextInt();

    }

    firstLast6( number);
    System.out.print(firstLast6(number));
}



 public static boolean firstLast6( int[] nums ) {

     if ( nums[0]==6 || nums[nums.length-1]==6 ){
                   return true;
     }
     else{

         return false;

     }
}




}

1 Comment

Hi, do add a bit of explanation along with the code as it helps to understand your code. Code only answers are frowned upon.
0

This code works fine.

public boolean firstLast6(int[] nums) {
      int length=nums.length;
      if(nums[0]==6||nums[length-1]==6){
        return true;
      }
      return false;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.