Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

no14 challenge

Posted on 2016-08-03
14
Medium Priority
?
94 Views
1 Endorsement
Last Modified: 2016-08-05
Hi,

I am working on below challenge

http://codingbat.com/prob/p136648

Psedo code description of approach :
1. loop through given array
2. check if no 1 or no 4 is then return true
3. otherwise return false

I wrote my code as below
public boolean no14(int[] nums) {
  boolean result=false;
  for(int i=0;i<nums.length;i++){
    if(nums[i]!=1||nums[i]!=4){
      result=true;
      return result;
    }
    
  }
  return result;
  
}

Open in new window





I am not passing all tests.
Expected      Run            
no14([1, 2, 3]) → true      true      OK      
no14([1, 2, 3, 4]) → false      true      X      
no14([2, 3, 4]) → true      true      OK      
no14([1, 1, 4, 4]) → false      true      X      
no14([2, 2, 4, 4]) → true      true      OK      
no14([2, 3, 4, 1]) → false      true      X      
no14([2, 1, 1]) → true      true      OK      
no14([1, 4]) → false      true      X      
no14([2]) → true      true      OK      
no14([2, 1]) → true      true      OK      
no14([1]) → true      true      OK      
no14([4]) → true      true      OK      
no14([]) → true      false      X      
no14([1, 1, 1, 1]) → true      true      OK      
no14([9, 4, 4, 1]) → false      true      X      
no14([4, 2, 3, 1]) → false      true      X      
no14([4, 2, 3, 5]) → true      true      OK      
no14([4, 4, 2]) → true      true      OK      
no14([1, 4, 4]) → false      true      X      
other tests
X      
Correct for more than half the tests

How to improve my design, approach, code? please advise
1
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 3
14 Comments
 
LVL 4

Accepted Solution

by:
Hammadh Abdul Rahman earned 1000 total points
ID: 41741743
The below condition will return true for all numbers.

#      nums[i]!=1      nums[i]!=4      nums[i]!=1||nums[i]!=4
1      F                        T                        T
2      T                        T                        T
3      T                        T                        T
4      T                        F                        T
5      T                        T                        T


if(nums[i]!=1||nums[i]!=4){

Open in new window


Having return statement within the loop will return the result before checking all the numbers.

This one is kinda hard to explain so try to understand the below code. Two variables are needed to track the absence of 1 and 4 separately. If only one variable is used we can change the variable to false if 1 is found. But it maybe the case that no 4's are present in which case it should return true.

public boolean no14(int[] nums) {
  boolean no1=true, no4=true;
  
  for(int i=0;i<nums.length;i++){
    if(nums[i]==1)
      no1=false; // it contains 1's, so it contains no 1's is false
    else if(nums[i]==4)
      no4=false; // it contains 4's, so it contains no 4's is false
  }
  
  return no1||no4; // return true if it contains no 1's or it contains no 4's.
}

Open in new window

0
 
LVL 16

Assisted Solution

by:krakatoa
krakatoa earned 1000 total points
ID: 41742293
Just do this

return Arrays.toString(nums).indexOf("1")<0||Arrays.toString(nums).indexOf("4")<0;

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 41742424

Having return statement within the loop will return the result before checking all the numbers.

public boolean no14(int[] nums) {
  boolean result=false;
  for(int i=0;i<nums.length;i++){
    if(nums[i]!=1||nums[i]!=4){
      result=true;
      //return result;
    }
    
  }
  return result;
  
}

Open in new window

even putting return outside fails tests. i wonder why?
Expected      Run            
no14([1, 2, 3]) → true      true      OK      
no14([1, 2, 3, 4]) → false      true      X      
no14([2, 3, 4]) → true      true      OK      
no14([1, 1, 4, 4]) → false      true      X      
no14([2, 2, 4, 4]) → true      true      OK      
no14([2, 3, 4, 1]) → false      true      X      
no14([2, 1, 1]) → true      true      OK      
no14([1, 4]) → false      true      X      
no14([2]) → true      true      OK      
no14([2, 1]) → true      true      OK      
no14([1]) → true      true      OK      
no14([4]) → true      true      OK      
no14([]) → true      false      X      
no14([1, 1, 1, 1]) → true      true      OK      
no14([9, 4, 4, 1]) → false      true      X      
no14([4, 2, 3, 1]) → false      true      X      
no14([4, 2, 3, 5]) → true      true      OK      
no14([4, 4, 2]) → true      true      OK      
no14([1, 4, 4]) → false      true      X      
other tests
X      
0
Implementing Azure Infrastructure Exam 70-533

This course is designed to familiarize and instruct students in the content that is covered by Microsoft Exam 70-533, Implementing Microsoft Azure Solutions. It focuses on all the November 2016 objective domain topics.

 
LVL 7

Author Comment

by:gudii9
ID: 41742426
   if(nums[i]==1)
      no1=false; // it contains 1's, so it contains no 1's is false
    else if(nums[i]==4)
      no4=false; // it contains 4's, so it contains no 4's is false
  }
  

Open in new window


what is difference between above and below

 if(nums[i]!=1||nums[i]!=4){
      result=true;
      //return result;
    }
    

Open in new window

i thought both same?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41742428
return Arrays.toString(nums).indexOf("1")<0||Arrays.toString(nums).indexOf("4")<0;

Open in new window

above also passed all tests
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41742486
above also passed all tests

I know that already.

The question is : did you and if so why.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41742738
   if(nums[i]==1)
      no1=false; // it contains 1's, so it contains no 1's is false
    else if(nums[i]==4)
      no4=false; // it contains 4's, so it contains no 4's is false
  }

Open in new window

 



what is difference between above and below. please advise

 if(nums[i]!=1||nums[i]!=4){
      result=true;
      //return result;
    }

Open in new window

0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41742926
Read the other expert's comment again.
0
 
LVL 4

Expert Comment

by:Hammadh Abdul Rahman
ID: 41743077
 if(nums[i]!=1||nums[i]!=4){
      result=true;
      //return result;
    }

Open in new window


If you check the results you will see that the above return true for any given number.
But the below code uses two variables not one. Variable "no1" is true is no 1's are found. Variable "no4" is true if no 4's are found. And the code returns true if either variable is true. That is if either no 1's or no 4's were found.

   if(nums[i]==1)
      no1=false; // it contains 1's, so it contains no 1's is false
    else if(nums[i]==4)
      no4=false; // it contains 4's, so it contains no 4's is false
  }

Open in new window

0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41743120
This is a more pedestrian way of breaking it down too :

boolean b = true;
  for(int i : nums){if((i==1)){b = false;}}
  if(b==true)return b;
  b = true;
  for(int i : nums){if((i==4)){b =  false;}}
  return b;

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 41743285
If you check the results you will see that the above return true for any given number.//i see that
But the below code uses two variables not one. Variable "no1" is true is no 1's are found. Variable "no4" is true if no 4's are found. //how to decide to take as two separate variables???

And the code returns true if either variable is true. That is if either no 1's or no 4's were found.//this is clear
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41743322
//how to decide to take as two separate variables???

because the question asks you to return true if EITHER 1 or 4 is not found. So a test like 2,3,4 should return true, because there are no 1s in it. A test like 2,3,1 would return true because . . . well, you tell us.

So you need to keep track of their existence *separately*.
0
 
LVL 4

Expert Comment

by:Hammadh Abdul Rahman
ID: 41743499
Let's say the numbers are 1,2,3,4. If we know a given number is present, but don't know the rest of the numbers, can we determine whether the statement "contains no 1's or contains no 4's" is true or false?

#      "contains no 1's or contains no 4's"            "contains no 1's"            "contains no 4's"
1      Can't determine                                                      False                              Can't determine
2      Can't determine                                                      Can't determine            Can't determine
3      Can't determine                                                      Can't determine            Can't determine
4      Can't determine                                                      Can't determine            False


1. If we only know 1 is present, we can't be sure whether the statement is true or false. But we can be sure that part of the statement that is "contains no 1's" is false. However, we can't be sure whether the other part of the statement that is "contains no 4's" is true or false.
2. If we only know 2 is present, we can't be sure whether the statement is true or false. And we can't be sure that any part of the statement is true or false either.
3. If we only know 3 is present, we can't be sure whether the statement is true or false. And we can't be sure that any part of the statement is true or false either.
4.If we only know 4 is present, we can't be sure whether the statement is true or false. But we can be sure that part of the statement that is "contains no 4's" is false. However, we can't be sure whether the other part of the statement that is "contains no 1's" is true or false.

Since by looking at any one given number we can't be sure whether the statement is true or false, we can't use a single variable to represent whether the statement is true or false. But since by looking at any one given number, and if the number is 1, we can be sure that the statement "contains no 1's" is false, we can use a variable to represent whether the statement "contains no 1's" is true or false. Likewise since by looking at any one given number, and if the number is 4, we can be sure that the statement "contains no 4's" is false, we can use a variable to represent whether the statement "contains no 4's" is true or false.

And as shown by krakatoa 1 variable could be technically used. But this variable can only represent that a part of the statement is true or false. And since if one part of an OR statement is true the whole statement must be true, we don't need to check the other part. But if the first part was false we would need to check whether the second part is true or false.

A single boolean variable cannot be used to represent whether the whole statement is true or false. But an integer variable could be used. But it would make things complex and hard to understand.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41745128
//how to decide to take as two separate variables???

because the question asks you to return true if EITHER 1 or 4 is not found. So a test like 2,3,4 should return true, because there are no 1s in it. A test like 2,3,1 would return true because . . . well, you tell us.

So you need to keep track of their existence *separately*.


//how to decide to take as two separate variables???

because the question asks you to return true if EITHER 1 or 4 is not found. So a test like 2,3,4 should return true, because there are no 1s in it. A test like 2,3,1 would return true because ...there are no 4s

So you need to keep track of their existence *separately*.

above is clear now.
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question