Home > database >  index 4 is out of bounds for 4 length
index 4 is out of bounds for 4 length

Time:07-05

package dsa450;

import java.util.*;

public class countInversion {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the size of Array");
        int n=sc.nextInt();
        int arr[]=new int[n];
        System.out.println("Enter the size of Array");
        for(int i=0;i<n;i  ){
            arr[i]=sc.nextInt();
        }

        int p=arr.length;
        int temp[]=new int[n];
        int ans=mergeSort(arr,temp,0,n-1);
        System.out.println(ans);

    }

    static int mergeSort(int[] arr,int[]temp,int l,int r) {
         int inv=0;
         int mid;
        if (l<r) {
        
            mid=(l r)/2;
            inv = mergeSort(arr,temp,l, mid);
            inv = mergeSort(arr,temp,mid 1,r);
            inv = merge(arr,temp,l,mid 1,r);
        }
           return inv;
    }

    private static int merge(int []arr,int []temp,int l,int mid,int r) {
        
           
        int i=0;
        int j=0;
        int k=0;
        int swap=0;
        while(i<=mid-1 && j<=r) {
            if(arr[i]<=arr[j]) {
                temp[k  ]=arr[i  ];
                
            }
            else {
                temp[k  ]=arr[j  ];
                swap =(mid-i);
            }
        }
        while(i<=mid-1) {
            temp[k  ]=arr[i  ];
            
        }
        
        while(j<=r) {
            temp[k  ]=arr[j  ];
            
        }
        
        for(i=l;i<=r;i  ) {
            arr[i]=temp[i];
        }
        
        return swap;
    }

}

enter image description here

this is my code I am trying to solve it for 5 hours but I am not getting what is the problem can anybody help. I check resources also but the code is the same as a source but it is not running.

CodePudding user response:

Hints:

  1. The actual error is that you are attempting to get an array element 1 position beyond the end of the array. (Array indexes go from 0 to array.length - 1.)

  2. Use the line numbers in the stacktrace to work out exactly which line of your code the exception occurs on. It is happening in an array indexing operation in the merge method. There are 6 lines where that could be happening.

  3. Add some trace prints to print out the values of critical variables at key points in the merge method; e.g. what l, r and mid are, and what i, j and k are.

  4. Run the modified code, look at the values output by the trace prints, and try to visualize what the code is doing.

  5. Spot where and why you are going beyond the end of an array ... and figure out what you need to change.

CodePudding user response:

Your merge method is failing because you are not validating k against size of temp array.

I am suggesting below small modifications in your merge method which will give you correct answer:

while (i <= mid - 1 && j <= r && k<temp.length) {
            if (arr[i] <= arr[j] ) {
                temp[k  ] = arr[i  ];

            } else {
                temp[k  ] = arr[j  ];
                swap  = (mid - i);
            }
        }
        while (i <= mid - 1 && k < temp.length) {
            temp[k  ] = arr[i  ];

        }

        while (j <= r && k < temp.length) {
            temp[k  ] = arr[j  ];

        }
  • Related