Detecting 3 continuous odd or even number in an array [closed] - java

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
While I was practicing Java Problems on coding bat I came across the following problem statement:-
Problem:-
Given an array of integers, return true if the array contains either 3 even or 3 odd values all next to each other.
Example:-
modThree([2, 1, 3, 5]) → true
modThree([2, 1, 2, 5]) → false
modThree([2, 4, 2, 5]) → true
My Solution:-
public boolean modThree(int[] nums) {
for(int i=0; i<nums.length-2; i++){
if((nums[i] % 2 == 0 && nums[i+1] % 2 == 0 && nums[i+2] % 2 == 0) || (nums[i] % 2 == 1 && nums[i+1] % 2 == 1 && nums[i+2] % 2 == 1)){
return true;
}
}
return false;
}
Though my solution works, my solution looks a bit long(especially the if statement condition). So, I am looking for a solution with fewer lines of code. Can you help me with this?

The three modulo values must be equal, not necessarily 0 or 1 (although % 2 makes 0 and 1 the single possible results). Therefore, you can replace the if statement with:
if(nums[i]%2 == nums[i+1]%2 && nums[i]%2 == nums[i+2]%2) {
...
}

Related

How can I count integer occurrence when the int is 0 in java? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm a beginner in java and I made a program to count the number of times an int d occurs in a given integer n i.e n = 988, d = 8 returns 2.
It works with most cases (negative, positive, etc.) but my code says n = 0, d = 1 contains 1, which is wrong. How do I add a place to my 0 without making the integer 10 (which I erroneously do in my first if statement).
public class countingints{
public static int count(int n, int d) {
n = Math.abs(n);
if(n == 0) {
n = 10;
}
int result = 0;
while (n > 0) {
int place = n % 10;
if (place == d) {
result++;
}
n /= 10;
}
return result;
}
public static void main(String[] args) {
System.out.println(count(0, 1)); //SHOULD return 0
System.out.println(count(0, 5)); //returns 0
}
}
if(n == 0) {
n = 10;
}
```
Computer just follows instructions. If n is 0, n is set to 10, and 10 contains a single 1 digit.
If perhaps your intent with this if (n == 0) n = 10 line is to ensure that e.g. count(0, 0) returns 1, then just code that in: if (n == 0 && d == 0) return 1; - that's a weird case because mathematically speaking the question 'how many times does the digit 0 show up' is tricky. You can write 15 as 0015 as well, and in many ways, 0 is just a way of writing it, the number really can be considered just a completely blank string with no digits at all.
Point is, it's logical, more or less, that your algorithm does 'weird things' when you ask it for how many zeroes are in a number, in particular when you ask it how many zeroes are in the number 0. "Weird cases" usually mean they need hardcoding, so, do that.

Can you explain to me this java code step by step? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
This is a code, I don't understand
public class main {
public static void main(String[] args) {
int result = sum(10);
System.out.println(result);
}
public static int sum(int k) {
if (k > 0) {
return k + sum(k - 1);
} else {
return 0;
}
}
}
The result is 55
This is a recursive method.
As long as k > 0, the method will keep calling itself with a smaller value of k.
The initial call, sum(10) returns
10 + sum(9) == 10 + 45 == 55
sum(9) returns
9 + sum(8) == 9 + 36 == 45
sum(8) returns
8 + sum(7) == 8 + 28 == 36
...
sum(2) returns
2 + sum(1) == 2 + 1 == 3
sum(1) returns
1 + sum(0) == 1 + 0 == 1
sum(0) returns 0.
Therefore the method returns the sum of all integers between 0 and 10, which is 55.

Understanding code to find the largest prime factor of a number [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I found this code on an old youtube video and am having the hardest time understanding the logical sequence for the output. I was hoping any of you can help clarify.
Let's assume I pass a value of 21 to the getLargestPrime method.
In line 17 the conditional statement checks whether 21 % 2 != 0, that's True. So it carries another iteration of the code. But this time m = 3, which 21 % 3 != 0, that's actually False. Why is the code still executing the else statement? This is question #1.
Question #2
How can the if statement inside the else ever execute? When does number become == 1?
I know that this is probably super basic for you guys but for some reason I cannot follow the sequence in the output.
public class LargestPrime {
public static int getLargestPrime(int number){
if(number < 0){
return -1;
}
int m = 2;
int ans = 0;
int numbern;
if(number == 1){
System.out.println("This number is not a prime");
} else{
while (ans == 0){
if(number % m !=0){
m = m +1;
System.out.println(m + " value of m");
} else {
numbern = number;
number = number / m;
if(number == 1){
System.out.println(numbern + " is the largest prime factor of your number");
ans++;
}
}
}
} return number;
}
}
This is the output:
3 value of m
4 value of m
5 value of m
6 value of m
7 value of m
7 is the largest prime factor of your number
1
21%3!=0 returns false. Because of this the jvm executes the code after else. In e.g. 21%2!=0, that is true, it executes the code after if.
It is 1, if number is as m.
This is when your calculation gets to an end because there cannot be any more numbers after your number.

Return even numbers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm a newbie here and just want to know how to return only even digits. And that without using while or for.
public static long evenDigit(long digit){
return ((digit < 10 && digit % 2 == 0)
|| ((digit % 10) % 2 == 0)
?
: evenDigit(digit / 10));
}
I'm getting a StackOverflowError. And I think it's because of the line behind the ?. It will recurse infinitely. I don't really know what to do right now. So maybe you guys have some tips?
edit: okay change the name of the method. Now it's probably more clear.
So this was btw a method from an exam last year. To print out only even digits. Using recursion. In this style.
From comment:
I have in the main method a system print out with the numbers: 123456
And I want only 246 to return.
First, you need to determine the stop condition, to ensure the recursion will terminate. If the input is only 1 digit, it shouldn't recurse:
if (number < 10) { // only one digit
if (number % 2 == 0)
return number; // keep even digit
return 0; // discard odd digit
}
Now, since number has multiple digits, we need to decide if we want to keep the last digit, then make recursive call on higher-order digits:
if (number % 2 != 0)
return odd(number / 10); // Discard last digit and process other digits
return odd(number / 10) * 10 // process other digits
+ number % 10; // keeping last digit
The above can be abbreviated to:
public static long odd(long number) {
return (number < 10 ? (number % 2 == 0 ? number : 0) :
number % 2 != 0 ? odd(number / 10) : odd(number / 10) * 10 + number % 10);
}

checking wether an int is not between 2 values [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How do i check wether a int is between two values when comparing it in an if statement, similar to this
if (num >= 1 && num <= 100 && bool != false) {System.out.print(true);}
Thanks.
Well, this looks like it should work. What are you looking for exactly - a more efficient way to do this?
int num =0;
if (num >= 1 && num <= 100) {
//do something
}
bool != false
is very confusing. Not only is it a double negative, but comparing a boolean value to true or false is redundant. Neither does the name give any hint as to its purpose. Something like the following would be clearer.
if(checkRange && (num >= 1 && num <= 100))
That question is quite weird since the answer is in the code you have provided. To check if the int is between two values just use this code.
if(num >= min && num <= max)
The more effective way - when you have multiple intervals is to use Interval Tree.
int j=3;
for (int i=0;i<=10;i++) {
if(j.equals(i)) {
System.out.println("Given value is between 0-10");
}
}

Categories

Resources