Problem:
Given an array of ints, return true if the array contains a 2 next to a 2 or a 4 next to a 4, but not both.
either24({1, 2, 2}) → true
either24({4, 4, 1}) → true
either24({4, 4, 1, 2, 2}) → false
Solution:
public boolean either24(int[] nums) { int two =0, four = 0; for (int i =0;i<nums.length-1;i++) { if(nums[i] == 2 && nums[i+1] == 2) two++; if (nums[i] == 4 && nums[i+1] == 4) four++; } if (two!=0 && four!=0) return false; else if (two!=0 || four!=0) return true; else return false;}
public boolean either24(int[] nums) {
ReplyDeleteboolean twos = false;
boolean fours = false;
for (int i=0; i<nums.length-1; i++)
{
if(nums[i]==2 && nums[i+1]==2) twos = true;
if(nums[i]==4 && nums[i+1]==4) fours = true;
}
return (twos ^ fours);
}
its great developers like you who help Jr. developers like myself think out side the box thanks for your answer i'm learning so much by looking at other developers code i really appreciate it.
Deletewhat is the function of return(twos^fours)?
Delete^ is the XOR operator in java. It simply checks that the 2 values are different.
DeleteIn this case, if the twos and fours booleans are the same (both true or both false) it will return true, but if they are different (one is true and the other false) it will return true.
It's great to see XOR operation minimizing whole bunch of code. Thank you!
Deletepublic boolean either24(int[] nums) {
ReplyDeleteint next2 = 0;
int next4 = 0;
for(int i = 0; i < nums.length - 1; i++){
if(nums[i] == 2 && nums[i+1] == 2){
next2++;
}
if(nums[i] == 4 && nums[i+1] == 4){
next4++;
}
}
return !(next2 == next4);
}
wao great
DeleteHi Guys - how to explain return (twos ^ fours) in words?
ReplyDeleteboolean two=false;
ReplyDeleteboolean four=false;
if(nums.length>1){
for(int i=0;i<nums.length-1;i++){
if(nums[i]==2&&nums[i+1]==2) two=true;
if(nums[i]==4&&nums[i+1]==4) four=true;
}}else return false;
if(two==true&&four==true)return false;
else return (two||four);
public boolean either24(int[] nums) {
ReplyDeleteint j = 0;
boolean result = false;
for(int i = 1; i<nums.length; i++){
if(nums[j] == 2 && nums[i] == 2) {
if(result){
return false;
} else {
result = true;
}
}
if(nums[j] == 4 && nums[i] == 4) {
if(result) {
return false;
} else {
result = true;
}
}
j++;
}
return result;
}
public boolean either24(int[] nums) {
ReplyDeleteint two = 0;
int four = 0;
for(int i = 0; i < nums.length - 1; i++){
if(nums[i] == 2 && nums[i+1] == 2){
two++;
}
if(nums[i] == 4 && nums[i+1] == 4){
four++;
}
}
if(two != 0 && four != 0){
return false;
} else if(two != 0 || four != 0){
return true;
} else {
return false;
}
}
public boolean either24(int[] nums) {
ReplyDeleteboolean found2 = false, found4 = false;
int i = 0;
while (i + 1 < nums.length && !(found2 && found4)) {
if (nums[i] == 2 && nums[i+1] == 2) {
found2 = true;
} else if (nums[i] == 4 && nums[i+1] == 4) {
found4 = true;
}
i++;
}
return !found2 && !found4 ? false: !(found2 && found4);
}
public boolean either24(int[] nums) {
ReplyDeleteboolean either = false;
int count4 = 0;
int count2 = 0;
for(int i=0; i1 && count4<2) || (count4>1 && count2<2)){
either = true;
}
return either;
}
public boolean either24(int[] nums) {
ReplyDeleteboolean either = false;
int count4 = 0;
int count2 = 0;
for(int i=0; i1 && count4<2) || (count4>1 && count2<2)){
either = true;
}
return either;
}
Anyone know the reason why it paste incomplete code? My code wasn`t like that, but somehow there is a problem with copy-paste. And I can`t delete my comment
ReplyDeletepublic boolean either24(int[] nums) {
ReplyDeleteint count = 0;
for(int i = 0; i < nums.length - 1; i++) {
if((nums[i] == 2 && nums[i+1] == 2)
|| (nums[i] == 4 && nums[i+1] == 4)) {
count++;
}
}
return count == 1;
}
public boolean either24(int[] nums)
ReplyDelete{
HashMap map = new HashMap<>();
for(int i = 1; i < nums.length; i++)
{
if(nums[i-1] == 4 && nums[i] == 4) map.put(nums[i],nums[i]);
if(nums[i-1] == 2 && nums[i] == 2) map.put(nums[i],nums[i]);
}
if(map.containsKey(4) && map.containsKey(2)) return false;
if(!map.containsKey(4) && !map.containsKey(2)) return false;
else return true;
}
public boolean either24(int[] nums) {
ReplyDeleteint two = 0;
int four = 0;
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] == 2 && nums[i + 1] == 2)
two++;
if (nums[i] == 4 && nums[i + 1] == 4)
four++;
}
return ((two>0 && four==0)|| (two==0 && four>0));
}