Solved

firstLast6

Posted on 2015-02-12
5
71 Views
Last Modified: 2015-02-17
Hi,
I was working on below challenge to find 6 at first and last position of array.

http://codingbat.com/prob/p185685

public boolean firstLast6(int[] nums) {



if(nums.contains(6))
{
return true;
}

else
{
return false;
}
  
}

Open in new window


i put some  code. Not exactly sure how to go about it. please advise
0
Comment
Question by:gudii9
  • 2
  • 2
5 Comments
 
LVL 7

Author Comment

by:gudii9
ID: 40606490
public boolean firstLast6(int[] nums) {

int firstInt=nums[0];
int numsLength=nums.length;
int lastInt=nums[numsLength-1];

if(firstInt==6)
{
return true;
}

if(lastInt==6)
{
return true;
}

else
{
return false;
}
  
}

Open in new window


i made changes and passed the tests. i wonder how i can improve my code. Please advise
0
 
LVL 84

Accepted Solution

by:
ozo earned 400 total points
ID: 40606501
I don't see anything wrong with your code.
You can also do it without the temporary variables:
  return nums[0]==6 || nums[nums.length-1]==6;
0
 
LVL 7

Author Comment

by:gudii9
ID: 40606508
public boolean firstLast6(int[] nums) {
  return nums[0]==6 || nums[nums.length-1]==6;
}

Open in new window


above passed all tests too in just one single simple line. couple  of questions here
1. What are temporary variables are there any permenant variables too if yes which to use when
2. How the false scenario taken care automatically how it passed all the tests( returning true is somewhat fine but returning false scenario not clear)

please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40606515
there aren't really permanent variables, but some variables can be more temporary than others
http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html

nums[0]==6 and nums[nums.length-1]==6 are either true or false.
0
 
LVL 3

Assisted Solution

by:Nadia Sobnom
Nadia Sobnom earned 100 total points
ID: 40606640
public boolean firstLast6(int[] nums) {
  return nums[0] == 6 || nums[nums.length - 1] == 6;
}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JAVA part two 5 57
VB Script to add site to Java Exception List 4 50
HSSFWorkbook cannot be resolved error 10 50
java jdbc batch example 8 22
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now