Java > Array-2 >isEverywhere (CodingBat Solution)

Problem:

We'll say that 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
isEverywhere({1, 2, 1, 3}, 2) → false
isEverywhere({1, 2, 1, 3, 4}, 1) → false


Solution:

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



11 comments :

  1. the length is actually -1 not -2

    ReplyDelete
  2. public boolean isEverywhere(int[] nums, int val) {

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

    ReplyDelete
    Replies
    1. for (int i = 0; i <=nums.length-2;i++)

      and

      for(int i=0;i<nums.length-1;i++)

      is the same thing if you look closely.

      Delete
  3. why for this 2 expected results:
    isEverywhere([3], 1) → true
    isEverywhere([], 1) → true

    result should be true? is it correct?

    ReplyDelete
    Replies
    1. technically there are no pairs so it is present in every pair. Also the for loop does not work for these cases so it simplifies the desired code for an easy problem like an Array-2

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

    ReplyDelete
  5. Look how many lines did it take to get solve for me))
    public static boolean isEverywhere(int[] nums, int val) {
    boolean isEveryWhere = true;
    if(nums.length==0 || nums.length==1){
    return true;
    }
    if(nums[0]!=val && nums[1]!=val){
    isEveryWhere = false;
    }
    if(nums[0]==val){
    for(int i=0; i<nums.length; i+=2){
    if(nums[i]!=val){
    isEveryWhere = false;
    }
    }
    }
    if(nums[1]==val){
    for(int i=1; i<nums.length; i+=2){
    if(nums[i]!=val){
    isEveryWhere = false;
    }
    }
    }

    return isEveryWhere;


    }

    ReplyDelete
  6. public boolean isEverywhere(int[] nums, int val) {
    int len = nums.length;
    if(len < 2) return true;
    boolean res = false;
    for(int i = 0; i<nums.length-1; i++){
    if(nums[i] == val || nums[i+1] == val){
    res = true;
    }else {
    return false;
    }
    }
    return res;
    }

    ReplyDelete
  7. public boolean isEverywhere(int[] nums, int val) {
    int len = nums.length;
    if(len < 2) return true;
    boolean res = false;
    for(int i = 0; i<nums.length-1; i++){
    if(nums[i] == val || nums[i+1] == val){
    res = true;
    }else {
    return false;
    }
    }
    return res;
    }

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

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact