Avatar of gudii9
gudii9
Flag for United States of America asked on

array6 challenfge

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

Java EEJavaProgramming Languages-OtherProgramming

Avatar of undefined
Last Comment
rrz

8/22/2022 - Mon
SOLUTION
rrz

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
sarabande

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
gudii9

ASKER
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);
}
gudii9

ASKER
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
gudii9

ASKER
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?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
rrz

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