How to recursively count number of occurences in an array - java

I've got a task that gets an int value "n" and an Int Array as parameters and is supposed to return a boolean.
The method is supposed to determine, how many "n" are in the given Array. If the number is even the method should return true, else false. If the Array has the length 0, it should return "false" aswell.
What i managed to do is :
public static boolean evenNumberOf(int n, int[] arr) {
boolean result = false;
System.out.println("Starting count");
if (n < arr.length) {
if (arr[n] == n) {
result = true;
} else {
return evenNumberOf(n - 1, arr);
}
}
return result;
}
Im just really confused and i dont know what to do to be honest. I have really tried my best but the longer i work on this task the less i understand.
Any help is appreciated and thank you in advance! :)

Separate it into two methods:
The method you call initially
and a method that gets called recursively to count the number of ns in the array:
boolean evenNumberOf(int n, int[] arr) {
int count = countNs(n, arr, 0);
// Logic to choose what to return based on count and/or length of arr.
}
int countNs(int n, int[] arr, int i) {
// Check if arr[i] is equal to n.
// Make a recursive call to countNs for i := i + 1.
// Combine the check/recursive call result to return a value.
}

Try
//arr should not be empty, index and count >= 0
public static boolean evenNumberOf(int value, int index,int[]arr, int count) {
if(index >= arr.length) return count%2 == 0;
if(arr[index] == value ) {
count++;
}
return evenNumberOf(value, ++index, arr, count);
}
Usage example: System.out.println(evenNumberOf(2, 0, new int[]{2,0,3,7,6,11,1,2}, 0));
(You can add an helper method evenNumberOf(int value,int[]arr))

as Recursive Counting in an Array got closed as a duplicate I will answer it here:
Let's analyze what you did and why it's wrong
public static int countN(int n,int [] arr,int i, int count) {
if (arr[i] == n) {
System.out.println("MATCH");
count++;
return count;
}
Here you already return the count when you get a match. You shouldn't do that because if the first number is already the same it returns 1. all you need to do is increase the count here
else {
System.out.println("Moving on");
i = i + 1;
countN(n,arr,i, count);
}
Here you do the recursion. This is good. But this also needs to be done in the case that you do get a match. And it needs to return that value. But, also this only needs to be done when you are not at the end of the array yet
if (arr.length == i) {
evenNumberOf(n,arr);
}
this part doesn't make sense, because you call evenNumberOf with the exact same arguments as it started so it will result in an infinite loop. you should have returned the count here. also keep in mind that the last index of an array is length - 1
putting this together you can make:
public static int countN(int n,int [] arr,int i, int count) {
if (arr[i] == n) {
count++;
}
if (arr.length - 1 == i) {
return count;
}
return countN(n, arr, i + 1, count);
}

Related

Recursive function that has an array of integers and it's size as parameters that will return true if the sum of the array elements is even?

How do I create a function that has an array of integers and the array's length as parameters that will return true if the sum of the array elements is even and false otherwise?
How would I do this without using any static variables?
I've tried making a code that will check if,
current is odd and previous is even will return false recursively, else will return true recursively, this idea is based on the mathematical axiom that only even plus odd is equal to odd, every other combination is even.
public static boolean q3(int[] arr, int index) {
if (index == 0) {
return arr[index] % 2 == 0;
}
if (arr[index] % 2 == 0) {//if current is even
if (!q3(arr, index - 1)) {//even plus odd = odd
return false;
} else
return true; //every other combo equal true
} else if (q3(arr, index - 1)) {//if current is odd
return true;
} else {
return false;
}
}
I wrote 2 versions, one with recursion, other with no recursion
import java.util.Arrays;
public class MyClass {
public static void main(String args[]) {
int[] arr={5, 3, 1};
System.out.println("sum of "+Arrays.toString(arr)+" is " + size_ispair_rec(arr, arr.length));
}
public static boolean size_ispair_rec(int[] arr, int size){
if(size-1 == 0){
return arr[size]%2==0;
}
return !((arr[size-1]%2 == 0) ^ size_ispair_rec(arr, size-1));
}
public static boolean size_ispair(int[] arr, int size){
int sum=0;
for(int i=0; i<size; ++i){
sum+=arr[i];
}
return sum%2 == 0 ? true : false;
}
}
Here is a recursive method that does the job. I've tried to keep the code readable.
The idea is that the only time you get an even number by addition is when
Both the numbers are even
Both the numbers are odd
At every recursive call, we check if adding the number to the previous calculated value makes it even or odd.
public static boolean isSumEven(int arr[], int length){
if(length == 0){
return true;
} else {
boolean sumPreviousElemsEven = isSumEven(arr, length - 1);
boolean currentElemEven = arr[length-1]%2 == 0 ? true : false;
if(sumPreviousElemsEven && currentElemEven || !sumPreviousElemsEven && !currentElemEven){
return true;
} else {
return false;
}
}
}

Count repeated elements in Java without using loops or imports

For training purposes I tried to code a program which counts how often a given number appears in an given array of integers. Then checks if the number is even or odd. Without using any imports or loops. I tried to solve it recursive.
For Example:
(3, new int[]{3,3,4,5,3,3,2,1})
There are 4 threes, so the Program should check if 4 is even or odd.
After days of coding and not working code I decided to ask here:
any Solutions?
public static int evenNumberOf(int num, int[] numarr) {
int i = 0 ;
int counter = 0;
if(a == null || a.length == 0) {
return false;
} else {
if(a[i] == a.length -1 ) {
if(counter % 2 == 0) {
System.out.println("true");
return true;
} else System.out.println("false");
return false;
} else {
if(a[i] == n) {
counter++;
i++;
return evenNumberOf(n,a) ;
} else {
i++;
return evenNumberOf(n,a) ;
Hint:
If you are trying to do this recursively, you can do this quickly with divide and conquer. Split the array into half, count each subarray and combine the results. Make sure the base case of an empty array/single element array is handled correctly.
Try this:
public static void main(String[] args) throws Exception{
System.out.println(evenNumberOf(2, 0, new int[]{2,0,3,7,6,11,1,2}, 0));
}
//arr should not be empty, index and count >= 0
public static int evenNumberOf(int num, int index,int[]numarr, int count) {
if(index >= numarr.length) return count;
if(numarr[index] == num ) {
count++;
}
return evenNumberOf(num, ++index, numarr, count);
}
You can add an helper method to make calling simpler :
public static int evenNumberOf(int num, int[] numarr) {
return evenNumberOf(num, 0, numarr,0);
}

find if given pack of coins can make a given value - coin row problem

given array of coins (int) and an int n the function need to return true if there is atleast one solution to the coin-change problem.
meaning: for array of ints> 0: [c(0) ,c(1) ,c(2) ,c(3) ,...... ,c(k)]. check if there is a solution for
the eqauation: a(0)*c(0)+ a(1)*c(1)+.....+ a(k)*c(k)= n. //n is the money we need to change
given c(0),c(1),....,c(n) >0 and a(0),a(1),....,a(n) =>0 both integers.
so I managed to make this code: the problem is that its algorithmic efficiency sucks, and this should be running on high values, and big coins array, so I need a code that is able to do this quicker.
public static boolean change(int[] coins, int n) {
boolean ans = false;
//loop running in recursion till founds ans/ passing limit
for (int i = 0; i < coins.length & (!ans); i = i + 1) {
if (n % coins[i] == 0) {
return true;
}
if (n >= coins[i]) {
ans = change(coins, n - coins[i]);
}
}
return ans;
}//O(n*k^n) solution for false ans , very bad :(
for example: for coins = {2,4,8} and n= 4111; I should get false, but the program unable to run this.
btw I would prefer this function to use recursion, but any solution/ guidnes is good :)
this is an other try doing this better but still not running as wanted.
//trying to use binary search and using divisions instead of minus
public static int iscashable(int[] coins, int n, int min, int max)
{
int check=(max+min)/2;
if(check == coins.length-1 | check == 0)
return check;
if(n/coins[check] > n% coins[check])
{
return (iscashable(coins,n,check,max));
}
else
{
return check;
}
}
public static int canchange(int[] coins, int n, int count)
{
int x=0;
int check= iscashable(coins,n,0,coins.length-count);
if(n%coins[check]==0)
{
return 0;
}
if(check==0)
{
return n;
}
if(n/coins[check] > n% coins[check])
{
x= (n/coins[check]) - (n% coins[check]);
int k= n-(coins[check]*x);
return canchange(coins, k, count+1);
}
else
{
return canchange(coins,n-coins[check],count+1);
}
}
the problem is both about runtime and number of recursion calls (with big number given, every recursion layer is coins.length^(num of layers));
I really thank you for your help!

Recursive Selection sort Java

I've been looking for a recursive selection sort, using only 2 parameters:
The array that has to be sorted
a value k, which indicates till which
element it has to be sorted.
Example: SelectionSort(array[] a, int k) with a being {6,3,5,7,2} and k being 2 will sort the first 3 elements, and will keep the last elements untouched.
I was thinking about starting with an if-statement for k being 0, and if that was the case, it would just return the array as it is, since you cant sort an array of size 1.
Something like:
public int[] sort(int[] a){
a = selectionSort(a, n-1);
return a;
}
public int[] selectionSort(int[] a, int k){
if (k = 0){
return a;
}
else{
selectionSort(a, k-1 );
... (part i really don't know)
}
I have no clue how to do the 'else' part since I only know that it has to call the method again.
I'm not allowed to create other methods. I also need to make sure I use exactly 2 parameters, nothing more, nothing less.
I have to work it out in pseudocode, but I understand some Java, so if someone could help me by using either pseudo, or Java, it would be so helpful
First some remarks to your code:
Your methods sort and selectionSort don't need to return an int[] array,
since the array object a stays the same all the time.
It is only the content within this array which changes.
Hence, you can use void as return-type.
In your if use (k == 0) instead of (k = 0)
You already figured out the first part.
Here it is how you can do the second part in pseudo code:
public void selectionSort(int[] a, int k) {
if (k == 0) {
return;
}
else {
selectionSort(a, k-1 );
find x such that a[x] is the smallest of a[k] ... a[a.length - 1]
if (a[k-1] > a[x]) {
swap a[k-1] and a[x]
}
}
}
I'm sure you are able to refine the pseudo code to real Java code.
By doing a simple google search, I found the biggest part of the code below on this site. I added the selectionSort method myself to suit your parameters.
public void selectionSort(int a[], int n)
{
recurSelectionSort(a, n, 0);
}
// Recursive selection sort. n is size of a[] and index
// is index of starting element.
static void recurSelectionSort(int a[], int n, int index)
{
// Return when starting and size are same
if (index == n)
return;
// calling minimum index function for minimum index
int k = minIndex(a, index, n-1);
// Swapping when index nd minimum index are not same
if (k != index){
// swap
int temp = a[k];
a[k] = a[index];
a[index] = temp;
}
// Recursively calling selection sort function
recurSelectionSort(a, n, index + 1);
}
// Return minimum index
static int minIndex(int a[], int i, int j)
{
if (i == j)
return i;
// Find minimum of remaining elements
int k = minIndex(a, i + 1, j);
// Return minimum of current and remaining.
return (a[i] < a[k])? i : k;
}

how to call a recursion call N number of times, given the number N?

I have an array of numbers: S= {4,5} and I want to check if this group creates the sum = 13.
In this case, yes: 4 + 4 + 5 = 13
Another example: s={4,5}, sum = 6 -> no
I wrote a recursive function to solve this:
public static boolean isSumOf(int [] s,int n)
{
if(n == 0)
return true;
if(n < 0)
return false;
return isSumOf(s,n-s[0]) || isSumOf(s,n-s[1]);
}
But this function works only for 2 numbers in the array.
I need to write a recursive function that will deal with N numbers, like {4,9,3} or {3,2,1,7} etc.
I'm not sure how can I do this? How can I call a recursion N times, according to the length of the array? Or maybe I should change my algorithm completely?
Also - I'm not allowed to use loops.
return isSumOf(s,n-s[0]) || isSumOf(s,n-s[1]);
You can generalize this with a loop like this:
for (int i = 0; i < s.length; ++i) {
if (isSumOf(s,n-s[i])) return true;
}
return false;
But, since you can't use loops, you can write the equivalent loop as another recursive method:
boolean withoutLoop(int [] s,int n, int i) {
if (i >= s.length) return false;
return isSumOf(s,n-s[i]) || recurse(s, n, i+1);
}
and then call it like so from your isSumOf method:
public static boolean isSumOf(int [] s,int n)
{
if(n == 0)
return true;
if(n < 0)
return false;
return withoutLoop(s, n, 0); // Change here.
}
Or, if you want to write it more concisely:
return (n == 0) || (n < 0 && withoutLoop(s, n, 0));
Break the problem down:
Sum the numbers
Is the sum equal to 13?
Then think of a way to express summing an array as recursive task; e.g. Sum of elements 1 to N is element 1 + Sum of elements 2 to N.
Finally, turn that idea / expression into code.
For any recursion problem, use the template:
ResultType recursiveMethod(params) {
if( /* this is the simplest case */ ) {
return answer for the simplest case
} else {
partialResult = solve part of the problem
resultForRest = recursiveMethod(rest of problem)
}
}
Particularly for list processing, this becomes:
if(list is empty) {
return solution for an empty list
} else {
r = call self recursively for tail of list
return solution for head of list combined with r
}
(Where "head" is the first item, and "tail" is the rest. Tail may be empty.)
For your problem, the simplest case is an empty array:
if(s.length == 0) {
return n == 0;
}
For the else, the "part of the problem" is s[0] and the "rest of the problem" is s[1] onwards.
...
} else {
int head = s[0];
int[] tail = Arrays.copyOfRange(s,1,s.length-1);
return isSumOf(tail, n - head);
}
The code would be cleaner (and probably more efficient) if you used a List instead of an array directly, because you could then use List.subList() instead of copyOfRange().
You could also pass the whole array each time, along with an extra parameter indicating how much of the array has already been accounted for.
This should work:
public static boolean isSumOf(int [] s,int n)
{
if(n == 0)
return true;
if(n < 0)
return false;
for (int x: s) {
if (isSumOf(s, n-x)) {
return true;
}
}
return false;
}
UPDATE:
Oh! no loop, only recursion, you will need an extra argument:
public static boolean isSumOf(int [] s,int n)
{
if(n == 0)
return true;
if(n < 0)
return false;
return isSum2(s, n, 0);
}
public static boolean isSum2(int [] s,int n,int i)
{
if (i >= s.length)
return false;
return isSumOf(s,n-s[i]) || isSum2(s,n,i+1);
}

Categories