Java > Array-3 > maxSpan (CodingBat Solution)

Problem:

Consider the leftmost and righmost appearances of some value in an array. We'll say that the "span" is the number of elements between the two inclusive. A single value has a span of 1. Returns the largest span found in the given array. (Efficiency is not a priority.)

maxSpan({1, 2, 1, 1, 3}) → 4
maxSpan({1, 4, 2, 1, 4, 1, 4}) → 6
maxSpan({1, 4, 2, 1, 4, 4, 4}) → 6


Solution:

public int maxSpan(int[] nums) {
  int span = 0;
  int tmp = 0;
  
  for (int i = 0; i < nums.length; i++) {
    for (int j = 0; j < nums.length; j++) {
      if (nums[i] == nums[j]) {
        tmp = j-i+1;
        span = Math.max(tmp,span);
      }
    }
  }
  return span;
}


11 comments :

  1. if(nums.length <1) return 0;
    int span = 0;

    for(int i=0; ii; j--){
    if(nums[i] == nums[j]){
    span = j-i > span ? j-i : span;
    }
    }
    }

    return span +1;

    ReplyDelete
    Replies
    1. public int maxSpan(int[] nums) {

      if(nums.length <1) return 0;
      int span = 0;

      for(int i=0; ii; j--){
      if(nums[i] == nums[j]){
      span = j-i > span ? j-i : span;
      }
      }
      }

      return span +1;
      }

      Delete
  2. public int maxSpan(int[] nums) {
    if(nums.length>0&&nums[0]!=nums[nums.length-1])
    {
    return nums.length-1;
    }
    return nums.length;
    }

    ReplyDelete
  3. With simple logic:

    public int maxSpan(int[] nums) {
    int counter = 0;
    if(nums.length == 0) {
    return 0;
    }
    if(nums[0] != nums[nums.length - 1]) {
    for(int i = 1; i < nums.length; i++) {
    counter++;
    }
    }
    if(nums[0] == nums[nums.length - 1]) {
    for(int i = 0; i < nums.length; i++) {
    counter++;
    }
    }
    return counter;
    }

    ReplyDelete
  4. public int maxSpan(int[] nums) {
    if(nums.length<=1) return nums.length;
    if(nums[0]==nums[nums.length-1]) return nums.length;
    return nums.length - 1;
    }

    ReplyDelete
    Replies
    1. It only works for the few tests at coding bat. It's wrong as a solution, if you run your code for int[]{1, 4, 2, 1, 4, 4, 3} it returns 6 which is wrong. The correct answer is 5.

      Delete
  5. public int maxSpan(int[] nums) {

    int count = 0;



    if (nums.length<=0) {

    return 0;

    }


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

    for (int j = nums.length - 1; j >= 0; j--) {

    if (nums[i] == nums[j]) {

    if (j - i > count) {
    count = j - i;
    }

    }

    }

    }

    return count+1;

    ReplyDelete
  6. public int maxSpan(int[] nums) {

    int max = Math.min(nums.length, 1);

    for (int i = 0; i < nums.length; i++) {
    for (int j = nums.length - 1 ; j > i; j--) {
    if (nums[i] == nums[j]) max = Math.max(j - i + 1 ,max);
    }
    }
    return max ;
    }

    ReplyDelete
  7. public int maxSpan(int[] nums) {
    int max = 0;
    for (int i = 0; i < nums.length; i++) {
    int j = nums.length - 1;
    while (j > i && nums[j] != nums[i]) {
    j--;
    }
    max = Math.max(max, j - i + 1);
    }
    return max;
    }

    ReplyDelete
  8. all the point is to use 2 loops to practise.. idk what ya doin

    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