Finding the middle element(s) of an array in Java

前端 未结 7 1890
小蘑菇
小蘑菇 2021-02-14 13:03

Given an array of integers, I need to return a new array containing the middle element(s) from the original array. Specifically, the result will have one element if the length o

相关标签:
7条回答
  • 2021-02-14 13:41

    I was going through Java Array docs and found that. It is perfect solution to get mid of an array.

    int low = startIndexOfArray;      // 0 Normally but can be anything
    int high = endIndexOfArray - 1;       
    
    int mid = (low + high) >>> 1;
    System.out.print("Mid Value OF Array Is "+ mid);
    
    0 讨论(0)
  • 2021-02-14 13:48

    Try this code:

    public int[] makeMiddle(int[] nums) {
        int[] a;
        if (nums.length %2 == 0) {
            // even-length array (two middle elements)
            a = new int[2];
            a[0] = nums[(nums.length/2) - 1];
            a[1] = nums[nums.length/2];
        } else {
            // odd-length array (only one middle element)
            a = new int[1];
            a[0] = nums[nums.length/2];
        }
        return a;
    }
    

    In your original code, you were not checking whether the length of nums be even or odd.

    0 讨论(0)
  • 2021-02-14 13:49

    I've seen:

    Integer midElement(int[] ary, int start, int end) {
        if (start < end) {
            return null;
        }
        int mid = (start + end)/2;
        return ary[mid];
    

    The above works for any start index and any end index. It even checks that invalid inputs were not passed it. The book Cracking The Coding Interview uses this approach throughout the book in the various relevant problems

    0 讨论(0)
  • 2021-02-14 14:02
    public int[] makeMiddle(int[] nums) {
    
    
            if(nums.length>=2){
    
            if(nums[nums.length-1]%2==0) {  
              int[] arrEven=new int[2];
              arrEven[0]=nums[(nums.length/2)-1];
              arrEven[1]=nums[(nums.length/2)];
              return arrEven;                                                
             }
            else {              
                int[] arrOdd=new int[1];
                arrOdd[0]=nums[(nums.length/2)];                
                return arrOdd;
            }
            }
            return nums;
     }
    
    0 讨论(0)
  • 2021-02-14 14:03

    A slightly general solution:

    public static int[] midArray(int[] arr) {
        int extra = arr.length % 2 == 0? 1 : 0;
    
        int[] a = new int[1 + extra];
    
        int startIndex = arr.length / 2 - extra;
        int endIndex = arr.length / 2;
    
        for (int i = 0; i <= endIndex - startIndex; i++) {
            a[i] = arr[startIndex + i];
        }
    
        return a;
    
    }
    

    Test run:

    public static void main(String[] args) {
        int[] a = new int[]{1, 2, 3, 4};
        int[] b = new int[]{1, 2, 3};
        int[] c = new int[]{1, 2};
        int[] d = new int[]{1};
    
        System.out.println(Arrays.toString(midArray(a)));
        System.out.println(Arrays.toString(midArray(b)));
        System.out.println(Arrays.toString(midArray(c)));
        System.out.println(Arrays.toString(midArray(d)));
    
    }
    

    Output:

    [2, 3]
    [2]
    [1, 2]
    [1]
    
    0 讨论(0)
  • 2021-02-14 14:04

    int mid = firstIndex + (lastIndex-firstIndex)/2 , will give you the mid of the array.

    0 讨论(0)
提交回复
热议问题