Solved

array220 challenge

Posted on 2016-09-21
8
80 Views
Last Modified: 2016-09-22
Hi,
I am working on below challenge and not cclear on description. please advise

http://codingbat.com/prob/p173469


Recursion-1 > array220
prev  |  next  |  chance
Given an array of ints, compute recursively if the array contains somewhere a value followed in the array by that value times 10. 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.

array220([1, 2, 20], 0) → true
array220([3, 30], 0) → true
array220([3], 0) → false
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
  • 5
  • 3
8 Comments
 
LVL 34

Expert Comment

by:sarabande
ID: 41809324
- recursion stops with false if the index is greater or equal to array length.
- if the index is greater than 0 and nums[index] is equal to 10 times nums[index-1] then stop with success.
- proceed recursion by calling array220 passing the array and the index incremented by 1 and return the result.

Sara
0
 
LVL 7

Author Comment

by:gudii9
ID: 41810760
array220([1, 2, 20], 0) → true
array220([3, 30], 0) → true
array220([3], 0) → false

how above is true and true then false?

array220([1, 2, 20], 0) → true

element at 0 index of above array is 1 so 1 *10 is 10 are we expected to see 10 in rest of elements and if found return true else false?
0
 
LVL 34

Accepted Solution

by:
sarabande earned 500 total points
ID: 41810914
these are 3 different tests and not a sequence of recursive calls

array220([1, 2, 20], 0) → true  because 2 * 10 == 20  (3rd recursive call)

array220([3, 30], 0) → true      because3 * 10 == 30  (2nd recursive call)

array220([3], 0) → false           because the array only has 1 element.

so 1 *10 is 10 are we expected to see 10 in rest of elements

the challenge is to find a pair of consecutive numbers where (left * 10 == right).

so you have to check whether the 1 was followed by 10 and if that is not the case whether you have more luck with the next pair.

Sara
0
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 
LVL 7

Author Comment

by:gudii9
ID: 41811159
the challenge is to find a pair of consecutive numbers where (left * 10 == right).

above is clear. let me think
0
 
LVL 7

Author Comment

by:gudii9
ID: 41811180
public boolean array220(int[] nums, int index) {
  if(index>nums.length-2){
    return false;
  }
if(nums[index+1]==nums[index]*10){
  return true;
}
else{
  return array220(nums,index+1);
}

}

Open in new window


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

Author Comment

by:gudii9
ID: 41811188
public boolean array220(int[] nums, int index) {
  if(index>nums.length-2){
    return false;
  }
if(nums[index]*10==nums[index+1]){
  return true;
}
else{
  return array220(nums,index+1);
}

}


/*- recursion stops with false if the index is greater or equal to array length.
- if the index is greater than 0 and nums[index] is equal to 10 times nums[index-1] 
then stop with success.
- proceed recursion by calling array220 passing the array and the index incremented 
by 1 and return the result.
*/

Open in new window


above bit refined i think
0
 
LVL 34

Expert Comment

by:sarabande
ID: 41811244
any improvements or alternate approaches?

the 'index>nums.length-2' looks a little bit clumsy.

normally, if a range of indices needs to be handled i prefer to skip the index 0 and then can use nums[index-1] and nums[index].

public boolean array220(int[] nums, int index) {
  if(index>=nums.length){
    return false;
  }
if(index > 0 && nums[index-1]*10==nums[index]){
  return true;
}
return array220(nums,index+1);

}

Open in new window


Sara
0
 
LVL 7

Author Comment

by:gudii9
ID: 41811284
sure. got it
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

734 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