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) { boolean found2 = false; boolean found4 = false; for (int i = 0;i < nums.length; i++) { if (nums[i] == 2 && i < nums.length-1 && nums.length > 1 && nums[i+1] == 2) found2 = true; if (nums[i] == 4 && i < nums.length-1 && nums.length > 1 && nums[i+1] == 4) found4 = true; } if (found2 && found4) return false; else if (!found2 && !found4) return false; else return true; }
public boolean either24(int[] nums) {
ReplyDeleteboolean esta = false;
boolean dos = false;
boolean cuatro = false;
for(int i=0; i<nums.length-1; i++)
{
if(nums[i]==4 && nums[i+1]==4)
{
esta = true;
cuatro=true;
}
if(nums[i]==2 && nums[i+1]==2)
{
esta = true;
dos=true;
}
if(dos && cuatro)
{
esta = false;
break;
}
}
return esta;
}
public boolean either24(int[] nums) {
ReplyDeleteboolean fours=false;
boolean twos=false;
for(int i=0;i<=nums.length-2;i++){
if(nums[i]==2 && nums[i+1]==2){
twos=true;
}
else if(nums[i]==4 && nums[i+1]==4){
fours=true;
}
}
if(fours && twos){
return false;
}
if(!fours && !twos){
return false;
}
else return true;
}
I think that my solution is easier..
ReplyDeletepublic boolean either24(int[] nums) {
boolean war2 = false;
boolean war4 = false;
for (int i = 0; i < nums.length-1; i++)
{
if (nums[i] == 2 && nums[i+1] == 2)
war2 = true;
if (nums[i] == 4 && nums[i+1] == 4)
war4 = true;
}
if (war2 == war4)
return false;
else
return true;
}
A better Version :
ReplyDeletepublic boolean either24(int[] nums) {
boolean zwei =false;
boolean vier = false;
for(int i =0; i<nums.length - 1; i++){
if(nums[i]== 2 && nums[i + 1]==2){
zwei =true;
}
if(nums[i]== 4 && nums[i + 1]== 4){
vier =true;
}
}
if(vier==true ^ zwei == true){
return true;
}
return false;
}
public boolean either24(int[] nums) {
ReplyDeleteboolean two = false;
boolean four = false;
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;
}return two ^ four;
}
if(Arrays.toString(nums).contains("2, 2")&&Arrays.toString(nums).contains("4, 4"))
ReplyDeletereturn false;
if(Arrays.toString(nums).contains("2, 2")||Arrays.toString(nums).contains("4, 4"))
return true;
return false;
public boolean either24(int[] nums) {
ReplyDeleteboolean twotwo=false;
boolean fourfour= false;
for(int i = 0;i<nums.length -1;i++){
if (nums[i]==2 && nums[i+1]==2){
twotwo=true;
}
if (nums[i]==4 && nums[i+1] ==4){
fourfour = true;
}
}
if( twotwo && fourfour) {
return false;
}
return (twotwo || fourfour);
}
String arrS = Arrays.toString(nums).replaceAll("\\[|,|\\]|\\s","");
ReplyDeleteboolean isTrue =
(arrS.contains("22")&&!arrS.contains("44"))?true:
(arrS.contains("44")&&!arrS.contains("22"))?true:false;
return isTrue;
int c2=0,c4=0;
ReplyDeletefor(int i=0;i0 && c4==0) || (c4>0 && c2==0);
int c2=0,c4=0;
Deletefor(int i=0;i0 && c4==0) || (c4>0 && c2==0);
doesn't let me to share whole code(?)
ReplyDeletepublic boolean either24(int[] nums) {
ReplyDeleteint four =0;
int two= 0;
for(int i=0; i0 && two==0) {
return true;
}
else if(two>0 && four==0) {
return true;
}
else{
return false;
}
}
public boolean either24(int[] nums) {
ReplyDeleteint four =0;
int two= 0;
for(int i=0; i0 && two==0) {
return true;
}
else if(two>0 && four==0) {
return true;
}
else{
return false;
}
}
public boolean either24(int[] nums) {
ReplyDeleteif (Arrays.toString(nums).contains("2, 2")) {
if (Arrays.toString(nums).contains("4, 4")) {
return false;
}
}
return Arrays.toString(nums).contains("2, 2")
|| Arrays.toString(nums).contains("4, 4");
}
public boolean either24(int[] nums) {
ReplyDeletereturn Arrays.toString(nums).contains("2, 2") != Arrays.toString(nums).contains("4, 4");
}
public boolean either24(int[] nums) {
ReplyDeletereturn Arrays.toString(nums).contains("2, 2") != Arrays.toString(nums).contains("4, 4");
}
public boolean either24(int[] nums) {
ReplyDeleteboolean is2 = false;
boolean is4 = false;
for(int i = 0; i<nums.length-1;i++){
if(nums[i]==2 && nums[i+1]==2){
is2 = true;
}
else if(nums[i]==4 && nums[i+1]==4){
is4 = true;
}
}
if(is2 && is4){
return false;
}else if( !is2 & !is4){
return false;
}else {
return true;
}
}