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);
}
Related
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;
}
}
}
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);
}
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!
Basically i'm trying to return true, if 1, 2, 3 in that specific order are in the array, i cant work out how to do that though.
Ive already tried using some for loops and if statements but i dont know if that actually is the best way to do it
public static boolean arrayOneTwoThree(int[] nums) {
for(int i = 0; i < nums.length - 2; i++) {
if(nums[i] == 1 && nums[i + 1] == 2 && nums[i + i] == 3){
return true;
}
}
return false;
}
it only returns true if 1s 2s and 3s are in the array i want it to only return true if 1,2,3 are in they specific orderenter image description here
public boolean arrayOneTwoThree(int[] nums) {
// variables declaration.
boolean result = false;
int counter = 0,index = 0;
int arr[] = {1,2,3};
// base condition check. like array length should not be less than 3.
if(nums.length < 3){
return result;
}
//for loop for iterating over array and finding desired pattern
for(int i = 0; i < nums.length; i++){
//pattern found in array
if(counter == 3){
result = true;
return result;
}
if(nums[i] == arr[counter]){
index++;
counter++;
}
else if(counter != 0){
i = index-1;
counter = 0;
}
}
if (counter == 3) {
result = true;
return result;
}
return result;
}
Complexity of this solution is O(n).
There are quite a few things wrong with you current code.
You return true for every single case.
You create a result variable and never do anything with it.
You create three loops when you can use just a single one.
You never actually check to see if the values are next to each other.
For the first problem, we will return true only when the 1, 2, 3 are next to each other, and return false; for EVERY other case. This is done by using return false; after the loop.
For the next problem, result is not needed, you do not actually need to count anything, so remove this.
For the third issue, combine all the loops into a single loop. However, we will need to loop to the condition of nums.length - 2 instead of the length because we will comparing 3 values at the same time, and we do not want to get an ArrayOutOfBoundsException.
Finally, to check to see if all the values are next to each other, simply compare the values at the current Array index, the next one, and the index two values over with 1, 2 and 3 respectively.
This would look like if (nums[i] == 1 && nums[i + 1] == 2 && nums[i + 2] == 3). If this condition is true, we will return true immediately.
Here is how the code will look with all these fixes:
public static void main(String[] args) {
// test cases
int [] arr = {1, 1 ,2, 1, 2, 3};
System.out.println(arrayOneTwoThree(arr));
int [] arr2 = {3, 2, 3};
System.out.println(arrayOneTwoThree(arr2));
}
public static boolean arrayOneTwoThree(int[] nums) {
for(int i = 0; i < nums.length - 2; i++) {
if(nums[i] == 1 && nums[i + 1] == 2 && nums[i + 2] == 3){
return true;
}
}
return false;
}
Test Runs:
true
false
Note: Remove static from the arrayOneTwoThree(int [] nums) if you need to use it in non-static context, I used static to test it from my main.
Please try this solution and you may able to solve all your test cases
public static boolean find123(List<Integer> numbers) {
boolean isOneExist=false;
boolean isTwoExist=false;
boolean isThreeExist=false;
for(Integer n1:numbers)
{
if(n1==1)
{
isOneExist=true;
}
if(n1==2 && isOneExist)
{
isTwoExist=true;
}
if(n1==3 && isTwoExist)
{
isThreeExist=true;
}
}
if(isOneExist && isTwoExist && isThreeExist)
{
return true;
}
return false;
}
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);
}