Problem:
Given an array of ints, return true if the value 3 appears in the array exactly 3 times, and no 3's are next to each other.
haveThree({3, 1, 3, 1, 3}) → true
haveThree({3, 1, 3, 3}) → false
haveThree({3, 4, 3, 3, 4}) → false
Solution:
public boolean haveThree(int[] nums) { int count = 0; boolean found = false; for (int i = 0; i < nums.length; i++) { if (nums[i] != 3) found = false; if (nums[i] == 3 && found == true) return false; if (nums[i] == 3 && found == false) { found = true; count++; } } if (count == 3) return true; else return false; }
public boolean haveThree(int[] nums) {
ReplyDeleteint three = 0;
for(int i=0; i<nums.length-1; i++){
if(nums[i] == 3 & nums[i+1] == 3) return false;
}
for(int i=0; i<nums.length; i++){
if(nums[i] == 3) three++;
}
return three == 3;
}
public boolean haveThree(int[] nums) {
ReplyDeleteint count = 0;
if(nums.length < 3) return false;
else {
for(int i = 0; i < nums.length-1; i++) {
if(nums[i] == 3 && nums[i+1] == 3) return false;
else if(nums[i] == 3) ++count;
}
if(nums[nums.length-1] == 3) ++ count;
}
return count == 3;
}
public boolean haveThree(int[] nums) {
ReplyDeleteboolean aux=false,aux2=true;
int cont=0;
for(int i=0;i<nums.length;i++)
{
if(nums[i]==3)
{
cont+=1;
}
}
for(int i=0;i<nums.length-1;i++)
{
if(nums[i]==3 && nums[i+1]==3)
{
aux2=false;
break;
}
}
if(cont==3 && aux2)
{
aux=true;
}
return aux;
}
public boolean haveThree(int[] nums) {
ReplyDeleteint count = 0;
if (nums.length <= 1) return false;
if (nums[0] == 3 && nums[1] != 3) count ++;
if (nums[nums.length-1] == 3 && nums[nums.length-2] != 3) count ++;
for (int x = 1; x < nums.length-2; x++){
if (nums[x-1] != 3 && nums[x]==3 && nums[x+1] != 3 )
count++;
}
return (count == 3);
}
// public boolean haveThree(int[] nums) {
ReplyDelete// int counter = 0;
// for(int i = 0; i < nums.length; i++) {
// int num = nums[i];
// if(num == 3) {
// if(i > 0 && nums[i - 1] == 3) {
// return false;
// }
// counter++;
// }
// }
// if(counter == 3) {
// return true;
// }
// return false;
// }
public boolean haveThree(int[] nums) {
ReplyDeleteint count3 = 0;
boolean next3 = true;
for(int i = 0; i < nums.length; i++){
if(nums[i] == 3){
count3++;
if(i < nums.length - 1 && nums[i+1] == 3){
next3 = false;
}
}
}
return count3 == 3 && next3 == true;
}
public boolean haveThree(int[] nums) {
ReplyDeleteint count = 0;
for (int i = 0; i < nums.length; i++) {
if ( nums[i] == 3 ) {
count++;
i++;
}
}
return count == 3;
}
Very short, but if something like [1, 3, 3, 3, 3, 3, 1] appears it would return "true" instead of "false". Althought the tests in Codingbad miss to cover this case.
Deletenice and yes hopper case is missed
Deletepublic boolean haveThree(int[] nums) {
ReplyDeleteint count = 0;
Boolean index = false;
List lista = new ArrayList<>();
for (int i=0; i<nums.length; i++) {
if (nums[i]==3) {
count++;
lista.add(i);
}
}
for (int i=0; i<lista.size()-1; i++) {
if (lista.get(i+1)-lista.get(i)==1)
index = true;
}
return (count==3 && !index);
}
public boolean haveThree(int[] nums) {
ReplyDeleteboolean a=false;
int i=0;
int count=0;
while(i<nums.length){
if(nums[i]==3){
i+=2;
count++;
}
else if(nums[i]!=3)
i++;
}
if(count==3)
a=true;
return a;
}
Does anyone know why this code fails to work? It passes all the tests except for the hidden ones.
ReplyDeletepublic boolean haveThree(int[] nums) {
int count = 0;
if(nums[0] == 3) count = 1;
for(int i = 1; i < nums.length; i++)
if(nums[i] == 3)
{
count++;
if(nums[i - 1] == 3)
{
return false;
}
}
return count == 3;
}
int count = 0;
ReplyDeleteString arrS = "";
for(int i : nums)
{
if(i==3)count++;
arrS = arrS + String.valueOf(i);
}
return (count==3&&arrS.matches("(.*)3\\d3(.*)")&&!arrS.contains("33"));
This is my solution.
ReplyDeletepublic boolean haveThree(int[] nums) {
boolean NoUpper=false;
int counter=0;
for(int i=0;i<nums.length;i++){
if(i+1<nums.length){
if(nums[i]==3 && nums[i+1]==3) {
NoUpper=true;
}
}
if(nums[i]==3 && !NoUpper) counter++;
}
return counter==3;
}
BEST SOULUTION:
ReplyDeletepublic boolean haveThree(int[] nums) {
int count = 0;
for(int i = 0; i < nums.length; i++){
if(nums[i] == 3){
count++;
i++;
}
}
if(count == 3){
return true;
} else {
return false;
}
}
public boolean haveThree(int[] nums) {
ReplyDeleteint count = 0;
for(int i=0; i<nums.length; i+=1){
if(nums[i]==3){
count++;
i+=1;
}
}
if(count==3)
return true;
return false;
}
int count=0;
ReplyDeletefor (int k = 0; k < nums.length; k++) {
if(nums[k]==3) count++;
}
for (int i = 0; i < nums.length-4; i++) {
if(count>3) return false;
if(nums[i]==3 && nums[i+2]==3 && nums[i+4]==3)
return true;
}
return false;
}
public boolean haveThree(int[] nums) {
ReplyDeleteint count = 0;
for (int i = 0; i < nums.length; i++)
{
if (i + 1 < nums.length && nums[i] == 3 && nums[i + 1] == 3)
{
return false;
}
else if (nums[i] == 3)
{
count += 1;
}
}
return count == 3;
}
public boolean haveThree(int[] nums) {
ReplyDeleteint count = 0;
for(int i = 0; i < nums.length; i++){
if(nums[i] == 3){
count++;
i++;
}
}
return count == 3;
}
public boolean haveThree(int[] nums) {
ReplyDeleteint count = 0;
boolean next = true;
for(int i = 0; i < nums.length; i++){
if(nums[i] == 3){
count++;
if(i<nums.length - 1 && nums[i+1] ==3){
next = false;
}
}
}
return count == 3 && next;
}
int count=0;
ReplyDeletefor(int i=0; i<nums.length; i++){
for(int k=i+1; k<nums.length; k++){
if(nums[i]==3 && nums[k]!=3)
count++;
}
}return count==3;
public boolean haveThree(int[] nums) {
ReplyDeleteint c=0;
for(int i=0;i<nums.length-1;i++){
if(nums[i]==3&&nums[i+1]==3) return false;
}
for(int i=0;i<nums.length;i++){
if(nums[i]==3) c++;
}
return (c==3);
}
for python makers cuz no more python tasks on coding bat:
ReplyDeletedef haveThree(n):
c = 0
b = False
if n[len(n)-1] == 3:
c += 1
for i in range(len(n)-1):
if n[i] == 3 and n[i+1] == 3:
return b
break
if n[i] == 3:
c += 1
if c == 3:
b = True
return b
public boolean haveThree(int[] nums) {
ReplyDeleteboolean x = false;
int c =0;
for(int i =0 ; i<nums.length;i++){
if(i<nums.length-1&&(nums[i]==3&&nums[i+1]==3))
x = true;
if(nums[i]==3)
c++;
}
return(x==false&&c==3);
int count=0;
ReplyDeletefor(int i=0; i<nums.length-4; i++){
if(nums[i]==3 && nums[i+2]==3 && nums[i+4]==3){
count++;}
}if(count==1)return true;
return false;
public boolean haveThree(int[] nums) {
ReplyDeleteint count = 0 ;
for ( int x = 0; x < nums.length; x++){
if (nums[x] == 3){
count++;
if (x > 1 && nums[x-1] == 3){
count = 0;
break;
}
}
}
return (count == 3);
}
more simple to underistand:
ReplyDeletepublic boolean haveThree(int[] nums) {
boolean found=true;
int num=0;
for(int i=0;i<nums.length;i++){
if(nums[i]==3)
num++;
if(i<nums.length-1)
if(nums[i]==3&&nums[i+1]==3)
found=false;
}
if(num==3&&found)
return true;
return false;
}
I think this codes makes work easier and simple to understand
ReplyDeletepublic boolean haveThree(int[] nums) {
int count = 0;
for(int i = 0; i < nums.length; i++) {
if(nums[i] == 3) {
count++;
i++;
}
}
return count == 3;
}
public boolean haveThree(int[] nums) {
ReplyDeleteint count=0,prev=0;
for(int i=0;i<nums.length;i++){
if(prev!=nums[i]&&nums[i]==3){
count++;
}
prev=nums[i];
}
return count==3;
}
public boolean haveThree(int[] nums) {
ReplyDeleteint count =1;
for(int i=0 ; i<nums.length-2 ; i++){
if(nums[i]==3){
if( (nums[i+1]!=3 && nums[i+2]==3)){
count++;
}
}
}
return count==3;
}