Solved

array6 challenfge

Posted on 2016-09-15
6
63 Views
Last Modified: 2016-09-18
Hi,

I am working on below challenge.
http://codingbat.com/prob/p108997
Psedo code:
1. create new array of the portion of original array length
2. fill new array with rest of the elements
3. check to see if it has 6 if yes return true if no return false
4. retrun above boolean

I wrote my code as below
public boolean array6(int[] nums, int index) {
  int len=nums.length-index;
  int[] num=new int[len];
  //fill without using for loop but recursive way
    if(num.length==0){
    return false;
  }
  if(num.length==1&&!num.contains(6)){
    return false;
  }
   if(num.length==1&&num.contains(6)){
    return true;
  }
 
  
  return null;
}

Open in new window

0
Comment
Question by:gudii9
  • 3
  • 2
6 Comments
 
LVL 27

Assisted Solution

by:rrz
rrz earned 250 total points
ID: 41800152
Why do you feel the need to create a new array?
You are trying to use a method named contains.  Can you point to it in the API?
0
 
LVL 32

Accepted Solution

by:
sarabande earned 250 total points
ID: 41800270
since the challenge is to use a recursive approach, you need to

- check if index >= array size
- if yes, return false
- check if nums[index] == 6
- if yes, return true,
- return array6 function call passing nums and index+1
.

if you do so for [3, 5, 6, 0, 4], then the recursive calls are

array6(nums, 0)
array6(nums, 1)
array6(nums, 2) ==> return true

Sara
0
 
LVL 7

Author Comment

by:gudii9
ID: 41804119
java 8 has below lamda for each method but coding bat do not support

public boolean array6(int[] nums, int index) {
 IntStream.of(nums).anyMatch(x -> x == 6);
}
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 7

Author Comment

by:gudii9
ID: 41804122
public boolean array6(int[] nums, int index) {
  if(index>nums.length()){
    return false;
  }
    else if(nums[index]==6){
      return true;
    }
    else return arrays6[nums,index+1];
  }





/*- check if index >= array size
- if yes, return false
- check if nums[index] == 6
- if yes, return true,
- return array6 function call passing nums and index+1*/

Open in new window


above gives below error.

Compile problems:


Error:      else return arrays6[nums,index+1];
                              ^
Syntax error on token ",", . expected


see Example Code to help with compile problems
please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 41804124
public boolean array6(int[] nums, int index) {
  if(index>=nums.length){
    return false;
  }
    else if(nums[index]==6){
      return true;
    }
    else return array6(nums,index+1);
  }

Open in new window


above passed all tests.
any improvements or alternate approaches?
0
 
LVL 27

Expert Comment

by:rrz
ID: 41804138
Your code is good.   My personal preference is to omit the elses.
public boolean array6(int[] nums, int index) {
  if(index>=nums.length){
    return false;
  }
  if(nums[index]==6){
    return true;
  }
  return array6(nums,index+1);
}

Open in new window

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
json example 39 114
Python Assistance 7 35
python sqlite question 11 43
Modify a small python script 19 51
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

757 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

19 Experts available now in Live!

Get 1:1 Help Now