Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

array6 challenfge

Posted on 2016-09-15
6
Medium Priority
?
147 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 28

Assisted Solution

by:rrz
rrz earned 1000 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 35

Accepted Solution

by:
sarabande earned 1000 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
Stack Overflow Podcast - Developer Story

Welcome to the Stack Overflow podcast recorded Thursday July 20 at Stack Overflow Headquearters in NYC. Your hosts today are podcast regulars Jay Hanlon, David Fullerton, and Ilana Yitzhaki, plus the quite irregular Matt Sherman (Stack Overflow Engineering Manager extraordinaire)

 
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 28

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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
A solution for Fortify Path Manipulation.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Progress
Suggested Courses

705 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