Problem:
Given an array of ints, return true if it contains no 1's or it contains no 4's.
no14({1, 2, 3}) → true
no14({1, 2, 3, 4}) → false
no14({2, 3, 4}) → true
Solution:
public boolean no14(int[] nums) { boolean two = false,four = false; for(int count = 0;count <nums.length; count++) { if (nums[count] == 1) two = true; if(nums[count] == 4) four = true; } if(nums.length == 0 || nums.length == 1) return true; else if (two ==true && four ==true) return false; else if (two || four) return true; else return false; }
public boolean no14(int[] nums) {
ReplyDeleteboolean ones=false;
boolean fours=false;
for(int i=0;i<nums.length;i++){
if(nums[i]==1) ones=true;
else if(nums[i]==4)fours=true;
};
return !(fours&&ones);
}
public boolean no14(int[] nums) {
ReplyDeleteboolean ones=false;
boolean fours=false;
for(int s: nums){
if(s == 1)
ones = true;
if(s == 4)
fours = true;
}
if(ones && fours)
return false;
else
return true;
}
clean
Deletepublic boolean no14(int[] nums) {
ReplyDeleteboolean res = true;
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums.length; j++) {
if (nums[i] == 1 && nums[j] == 4)
res = false;
}
}
return res;
}
public boolean no14(int[] nums) {
ReplyDeletefor(int i =0; i< nums.length; i++){
if(nums[i] ==1 ){
for(int j =0; j< nums.length; j++){
if(nums[j] ==4 )
return false;
}
}
}
return true;
}
public boolean no14(int[] nums) {
ReplyDeletereturn !Arrays.toString(nums).contains("1") || !Arrays.toString(nums).contains("4") ;
}
public boolean no14(int[] nums) {
ReplyDeletereturn !Arrays.toString(nums).contains("1") || !Arrays.toString(nums).contains("4") ;
}
This is my solution :
ReplyDeletepublic boolean no14(int[] nums) {
boolean a = false;
boolean b=false;
for(int i=0;i<nums.length;i++){
if(nums[i]==1) a=true;
if(nums[i]==4) b=true;
}
if(a && b) return false;
return true;
}
public boolean no14(int[] nums) {
ReplyDeleteboolean ones = false ;
boolean fours = false ;
for ( int i = 0 ; i < nums.length ; i++ ) {
if(nums[i]==1)
ones=true;
if(nums[i]==4)
fours=true;
}
if ( fours && ones )
return false;
return true;
}
public boolean no14(int[] nums) {
ReplyDeleteboolean one = false, four = false;
for(int i=0; i<nums.length; i++){
if(nums[i]==1)
one = true;
if(nums[i]==4)
four = true;
}
if(one==true&&four==true)
return false;
return true;
}
public boolean no14(int[] nums) {
ReplyDeleteint four = 0;
int one = 0;
for (int n : nums) {
if (n == 1) one++;
else if (n==4) four++;
}
if (four==0 && one==0) return true;
return !(one>0 && four>0);
}
I did exactly like this
Deletepublic boolean no14(int[] nums) {
int oneCount = 0;
int fourCount = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 1) {
oneCount++;
}
if (nums[i] == 4) {
fourCount++;
}
}
return !(oneCount > 0 && fourCount > 0);
}
public boolean no14(int[] nums) {
ReplyDeleteint count1=0;
int count4=0;
for(int i=0 ; i0 &&count4>0)return false;
return true;
}
public boolean no14(int[] nums) {
ReplyDeleteboolean one=false;
boolean two=false;
for(int i=0;i<nums.length;i++){
if(nums[i]==1)
one=true;
if(nums[i]==4)
two=true;
if(one==true && two==true){
return false;
}
}return true;
}
int count1 =0;
ReplyDeleteint count4 =0;
for(int i =0;i= 1 && count4 >=1)
return false;
else
return true;
Array-2 > no14
ReplyDeletepublic boolean no14(int[] nums) {
ReplyDeleteboolean one = false;
boolean four = false;
for (int n: nums) {
if (n == 1) {
one = true;
}
if (n == 4) {
four = true;
}
}
if(one ^ four) {
return true;
}
if (!(one && four)) {
return true;
}
return false;
}
public boolean no14(int[] nums) {
ReplyDeleteint a=0;
int b=0;
for(int i:nums){if (i==1) a++;}
for(int j:nums){if (j==4) b++;}
if (a*b>0) return false;
return true;
}
public boolean no14(int[] nums) {
ReplyDeletefor(int i=0; i < nums.length; i++){
if(nums[i]==1){
for(int j=0; j < nums.length; j++){
if(nums[j]==4){
return false;
}
}
}
}
return true;
}
With Java Stream
ReplyDeletepublic boolean no14(int[] nums) {
List list = Arrays.stream(nums).boxed().collect(Collectors.toList());
return !list.contains(1) || !list.contains(4);
}