With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.
public boolean haveThree(int[] nums) {
int count=0;
boolean result=false;
for(int i=0;i<nums.length;i++){
if(nums[i]==3){
count++;
}
if(count==3){
result=true;
}
else{
result=false;
}
}
return result;
}
Expected RunHow to improve my design, approach, code? please advise
haveThree([3, 1, 3, 1, 3]) → true true OK
haveThree([3, 1, 3, 3]) → false true X
haveThree([3, 4, 3, 3, 4]) → false true X
haveThree([1, 3, 1, 3, 1, 2]) → false false OK
haveThree([1, 3, 1, 3, 1, 3]) → true true OK
haveThree([1, 3, 3, 1, 3]) → false true X
haveThree([1, 3, 1, 3, 1, 3, 4, 3]) → false false OK
haveThree([3, 4, 3, 4, 3, 4, 4]) → true true OK
haveThree([3, 3, 3]) → false true X
haveThree([1, 3]) → false false OK
haveThree([3]) → false false OK
haveThree([1]) → false false OK
other tests
X
prev_num=nums[i]; // Remember current num for next time through loop
public class TestTwo {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] ar = { 3, 1, 3, 2, 3,3 };
System.out.println("value is-->" + haveThree(ar));
}
public static boolean haveThree(int[] nums) {
int count = 0;
int prev_num = 0;
for (int i = 0; i < nums.length; i++) {
// Increment count only if current num is 3 and previous num is not
// 3
if (nums[i] == 3 && prev_num != 3) {
count++;
}
prev_num = nums[i]; // Remember current num for next time through
// loop
}
return count == 3; // Returns "true" if count is 3, otherwise returns
// false
}
}
public boolean haveThree(int[] nums) {
int count=0;
int prev_value=0;
// boolean result=false;
for(int i=0;i<nums.length;i++){
if(nums[i]==3&&prev_value!=3){
count++;
}
prev_value=nums[i];
}
return count==3;
}
public boolean haveThree(int[] nums) {
int count=0;
int prev_num=0;
for(int i=0;i<nums.length;i++){
if(nums[i]==3){
count++;
if(nums[i]==prev_num){
count=9; // Just some non-3 number to signify failure
break; // Break out of loop. Optional (just to save time looping through rest of elements for nothing)
}
}
prev_num=nums[i]; // Remember current num for next time through loop
}
return count==3; // Return true if count is 3, otherwise return false
}
i see one aditional if loop forif(nums[i]==prev_num){
count=9; // Just some non-3 number to signify failure
break; // Break out of loop. Optional (just to save time looping through rest of elements for nothing)
}
i got it. In later case
when it nums[i] is 3 count is incremented by 1.
Now you are checking if prev_value ==3 if yes put count as some dumy 9 value and breaking away from loop so that below tests also pass
{3,1,3,2,3,3}
other wise checking prev_value!=3 after num[i] with && is not 100 % case
if(nums[i]==3&&prev_value!=3){
public class HaveThree {
public static void main(String[] args) {
int[] ar={3,1,3,3};
// TODO Auto-generated method stub
System.out.println("is-->"+haveThree(ar));
}
public static boolean haveThree(int[] nums) {
int cnt = 0;
if (nums[0] == 3) {
cnt++;
}
for (int i = 1; i < nums.length; i++) {
if (nums[i] == 3) {
cnt++;
}
if (nums[i] == 3 && nums[i - 1] == 3) {
return false;
}
}
return cnt == 3;
}
}
If you are experiencing a similar issue, please ask a related question
Join the community of 500,000 technology professionals and ask your questions.