3

In This Question A value is "everywhere" in an array if for every pair of adjacent elements in the array, at least one of the pair is that value. Return true if the given value is everywhere in the array.

isEverywhere({1, 2, 1, 3}, 1) → true,coz 1 is present in(1,2) and (1,3) isEverywhere({1, 2, 1, 3}, 2) → false,coz 2 is in (1,2) but not in (1,3) isEverywhere({1, 2, 1, 3, 4}, 1) → false,coz 1 is present in 2 pairs (1,2) and (1,3) but 4 is not having a pair of 1

My Partial working code is below,could you help me out with this problem,stuck up for a long time.

::Code::

public boolean isEverywhere(int[] nums, int val) {    
    boolean flag = false;    
    for(int i=0;i<nums.length;i++){    
      for(int j=i+2;j<nums.length;j++){    
            if(nums[i] == nums[j]){    
                 flag = true;    
            }    
      }    
    }      
  return flag;     
}         

Result expected:

                           `Expected` `This` `Run`       

isEverywhere({1, 2, 1, 3}, 1) → true true OK
isEverywhere({1, 2, 1, 3}, 2) → false true X
isEverywhere({1, 2, 1, 3, 4}, 1) → false true X

8
  • case 2 and Case 3 are failing Commented Dec 21, 2010 at 18:57
  • 1
    @Deepak: Why are they failing? Commented Dec 21, 2010 at 18:58
  • looks suspiciously like the Homework tag should be here. Commented Dec 21, 2010 at 18:59
  • 1
    You don't need to nest for loops for this. Break down the problem. First try to enumerate through the pairs you are considering and doing a System.out.println to see that you are considering the right pairs. Even tiny problems can be taken step by step. Commented Dec 21, 2010 at 19:00
  • 2
    @Yar,Thanks for that small sentence. Commented Dec 21, 2010 at 19:19

8 Answers 8

3

Try this:

boolean isEverywhere(int[] nums, int val) {    

        // use i+=2 to get start index of pair.
        for(int i=0;i<nums.length;i+=2) {

                // other index in the pair.
                int j = i + 1;

                // make sure the other index really exists.
                if(j < nums.length) { 

                        // if it exists..and val is not equal to 
                        // either in the pair..return false.
                        if(nums[i] != val && nums[j] != val) {
                                return false;
                        }       
                } else {
                        // no pair..one element case.
                        // return true if that element is val..else return false.
                        return nums[i] == val; 
                }       
        }

        // array has no unpaired element..and all pairs have val.
        return true;     
}    
Sign up to request clarification or add additional context in comments.

9 Comments

@codaddict,i have one question,what the exception scenario isEverywhere({3}, 1) → true false X
@Deepak: ` isEverywhere({3}, 1)` returns false as expected as there is no 1. Is that not you want?
yes,correct,probably i need to check my test cases once,Thanks once again,sorry for the trouble,@coaddict
@Deepak: what about a case such as isEverywhere({1, 3, 4, 1}, 1) If I'm not mistaken this would be expected to return false, but this code would return true. This is because it would only check (1, 3) and (4, 1) isn't the pair (3, 4) considered?
@shaded,only the adjacency pairs are considered and will return true.@codaddict is correct,i just checked the algorithm i have last clarification,why have u return nums[i] == val;
|
2

The key is

for every pair of adjacent elements in the array

Print out i and j before if(nums[i] == nums[j]) and you'd see what's going on.

For an array of length 2, you need (0, 1), for array of length 3, you need (0, 1), (1, 2), and so on.

Comments

1

You never use val in your code
Try figuring out the logic for that first

And this can be done with a single loop, try to figure out how you would as a person apply an algorithm, then code it

Comments

0

Your code is failing because the flag does not turn off in the case that the second loop goes beyond the array length.

Comments

0

Not a full solution, but if the length of the array is odd, check that the last value equals the target val

...
    if( !(nums.length % 2) == 0 ){
       if( nums[length-1] != val){
          return false;
       }
    }
...

Comments

0

I did mine by simply counting if there are spaces: Here it is in it's simple glory:

public boolean isEverywhere(int[] nums, int val) {
    //Create an int to count the number of spaces between each instance of val.
    int counter = 0;
    //Create a for loop that checks val;
    for (int i = 0; i < nums.length; i++)
    {
      //If the number at iteration i != val;
      if (nums[i] != val)
      {
        //We increment the counter by one.
        counter++;
      }
      //If the number does equal val, we put the counter back to 0;
      else
      {
        counter = 0;
      }
      //If the counter is greater than or equal to two,
      //We know that there is a space of more than 2, and so
      //We return false;
      if (counter >= 2)
      {
        return false;
      }
    }
    //Return true if not false;
    return true;
  }

Comments

0

This code works fine:-

public boolean isEverywhere(int[] nums, int val) {
  for(int i=0;i<=nums.length-2;i++){
    if(nums[i]!=val&&nums[i+1]!=val){
      return false;
    }
  }
  return true;
}

Comments

0
public class IE {
    static boolean isEverywhere(int[] a, int val) {   
      for (int i = 0; i < a.length; i++) {
        System.out.println("*** iteration: " + i);
        System.out.println("a[" + (i-1) + "]=" + (i > 0 ? a[i-1] : "n/a"));
        System.out.println("a[" + i + "]=" + a[i]);
        System.out.println("a[" + (i+1) + "]=" + (i < a.length -1 ? a[i+1] : "n/a"));
        if (a[i] != val && (i > 0 ? a[i-1] != val : true) && (i < a.length-1 ? a[i+1] != val : true)) {
          return false;
        }   
      }   
      return true; 
    }
    
    public static void main(String[] args) {
        int[] a = new int[] {1, 2, 1, 3};
        System.out.println(isEverywhere(a, 1));
        a = new int[] {1, 2, 1, 3};
        System.out.println(isEverywhere(a, 2));
        a = new int[] {1, 2, 1, 3, 4};
        System.out.println(isEverywhere(a, 1));
    }
}

Basically, for a given element in the array, the test fails if the value isn't adjacent, either before or after it.

2 Comments

This doesn't solve the problem at all, and you shouldn't just be doing his homework for him.
he didn't say it was homework did he? and yes it does work for the given input (now that i edited it).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.