• Status: Solved
• Priority: Low
• Security: Public
• Views: 50

# Can Anyone help me. Please explain the question i cant get it and logic behind it.

Given an array of ints, compute recursively if the array contains a 6. We'll use the convention of considering only the part of the array that begins at the given index. In this way, a recursive call can pass index+1 to move down the array. The initial call will pass in index as 0.

array6([1, 6, 4], 0) → true
array6([1, 4], 0) → false
array6([6], 0) → true
0
gnanagowthaman sankar
• 6
• 3
• 2
• +1
2 Solutions

Commented:
What the problem statement is saying is to NOT not iterate over the array (do not use a "for/while) loop).  Once you invoke:

array6( someArray, idx);

look/examine the value at someArray[ idx ] and return true if the value is 6 or recurse if possible; otherwise return false.
2

Commented:
``````return (nums.length==0)||(index++)==nums.length?false:nums[index-1]==6?true:array6(nums,index);
``````
1

Prepress/OMS SpecialistCommented:
As hielo stated, you are not to use iteration using for or while loops.  This can be accomplished by using a function that calls itself recursively, as needed.

Here is an example in JavaScript:
``````function array6(arr, idx) {
if (idx < 0 || idx >= arr.length) {
return false;
}
if (arr[idx] === 6) {
return true;
}
return array6(arr,++idx);
}
``````
What happens should be obvious.  You call the function by passing an array and an index.  First you need to check to see if the index is out of range (i.e. is it less than 0 or greater than or equal to the length of the array.  Then, it checks to see if the array at the current index is equal to 6.  If it is, it returns true.  Otherwise, it calls itself with the same array after incrementing the index by 1.
1

Commented:
The question is about Java not Javascript.
Your code will not run on Codingbat of course.
0

Author Commented:
as i mentioned clearly in the question i want question explanation as @hielo done. i dont need any code i want logic. if i want any code i will mention in the comment area.
0

Commented:
I posted my code because by reading it, you ought to be able to deduce the logic chain heuristically.
If you can’t do that, and also cannot understand the original question, then your problem is in another dimension.
0

Author Commented:
yes in this scenario i cant understand the question.
0

Commented:
Your goal is to return true if the array contains a 6, false if it does not, and return a call to the very method itself if there are still array elements to check. (That’s the recursive part).
So you need to decide on which of those 3 conditions needs checking first to avoid fallthrough of logic. Obviously the recursive part doesn’t need to be done first, because you need to anyway check the first element. So deal with the cases where the array is zero length (that produces a false value obviously), and then whether the last element has already been checked (which if it has, and your code is still running must mean that it hasn’t found a 6), and if neither of those apply then check if it IS a 6, and return true.
1

Commented:
The quasi “hidden” logic in this one is that by checking up front whether all the elements have been interrogated, you are effectively obtaining a false return value simply by not having found a true one.
1

Prepress/OMS SpecialistCommented:
I understand that the question was about Java and not JavaScript.   I was showing the code example to better illustrate my explanation.  I apologize if the code was a distraction to you, but my explanation was correct.
0

Author Commented:
Thank you very much Jim riddles for your help. There is no distractions your explanation is so easy thank you.
0

Commented:
I understand that the question was about Java and not JavaScript.   I was showing the code example to better illustrate my explanation.  I apologize if the code was a distraction to you, but my explanation was correct.

That's as maybe, but EE is also an archive, where people come to obtain clear cut information. Mixing topic areas is bad practice, and undermines the value of the archive by muddying the waters for those who understand these matters less than you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.