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
gnanagowthaman sankarAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
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
krakatoaCommented:
return (nums.length==0)||(index++)==nums.length?false:nums[index-1]==6?true:array6(nums,index);

Open in new window

1
Jim RiddlesPrepress/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);
}

Open in new window

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
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

krakatoaCommented:
The question is about Java not Javascript.
Your code will not run on Codingbat of course.
0
gnanagowthaman sankarAuthor 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
krakatoaCommented:
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
gnanagowthaman sankarAuthor Commented:
yes in this scenario i cant understand the question.
0
krakatoaCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
krakatoaCommented:
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
Jim RiddlesPrepress/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
gnanagowthaman sankarAuthor Commented:
Thank you very much Jim riddles for your help. There is no distractions your explanation is so easy thank you.
0
krakatoaCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.