Reputation: 1
Find i-th boolean from an array of boolean, for example: array is {true, true , false, false, true}, the method will output int that shows 3rd true value, that will be 4.
Some code i have already tried, it works but i need to use recursion, not a while function.
public static int check(int n, boolean[] b, boolean val){
int i = 0;
int count = 0;
while(i < b.length && count <= n){
if(b[i] == val) count++;
i++;
}
if(n == count){
return i;
}
else{
return -1;
}
}
Upvotes: 0
Views: 424
Reputation: 51
You can do it like this :
int f(int n,boolean[] b,boolean val,int i)
{
if(i>=b.length)
return -1;
if(b[i]==val)
{
if(n==1)
return i;
else
return f(n-1,b,val,i+1);
}
return f(n,b,val,i+1);
}
Upvotes: 1
Reputation: 113
The actual function starts counting from 0, so I'm passing a position-1 inside. If i = 0, then it's quite obvious what we should return - the index of the first entry. If i is greater than 0, let's say it's 1, then we split the array into two parts: the part we already searched which contains the first entry, and the rest of an array. This can be done using subList(). Now we can use our function with i-1 on the rest of an array, which would find the index of the first entry in the second part of an array.
Also we have to add the size of what we cut out and that would be list.subList(0, list.indexOf(value) + 1).size()
this is needed to remember the index in the original array.
public static void main(String[] args) {
List<Boolean> list = Arrays.asList(true, true, false, false, true, true, false, false); //8
int position = 4; //find index of fourth false
System.out.println(recursiveSearch(list, false, position - 1));
position = 2; //find index of second true
System.out.println(recursiveSearch(list, true, position - 1));
}
private static int recursiveSearch(List<Boolean> list, boolean value, int i) {
if(i == 0) {
return list.indexOf(value);
} else {
return list.subList(0, list.indexOf(value) + 1).size() + recursiveSearch(list.subList(list.indexOf(value) + 1, list.size()), value, i - 1);
}
}
Upvotes: 0
Reputation: 1
public class Recursion {
private static boolean[] b = {true, true , false, false, true};
private static int i = 0;
private static int position = 0;
public static void check( int i, boolean[] b, boolean val ) {
if( i < b.length ) {
if( b[i] == val ) {
position = i;
}
i++;
check( i, b, val );
}
}
public static void main(String[] args) {
boolean myChoice = true;
check( Recursion.i, Recursion.b, myChoice );
System.out.println( "Last " + myChoice + " position computed is " + position );
}
}
Recursion is about a method calling itself. In the example above, the method checks if the 'counter' variable i is less than the length of the boolean array. Followed by a check of whether the current element of the array matches the selection. If so, the counter value is assigned to the position (last computed position). The counter is incremented, and the method calls itself.
The process continues until i is equal to the size of the boolean array. At that point, the method stop calling itself.
Upvotes: 0