Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
728 views

I want to invert an array, such that when I enter: 1 4 5 6

the software returns: 6 5 4 1
This is what I've written, however, I keep receiving an arrayIndexOutOfBoundException.

import java.io.*;
import java.util.*;

public class Oef9
{

    public int[] array_oef9 (int[] array1){
        int lengte=array1.length;
        int[] array= new int[lengte];

        for (int i=0; i<lengte;i++ ){
            array[lengte]=array1[i];
            lengte=lengte-1;
        }
        return array;

    }
}

I discovered this solution online that claimed "don't update lengte in the loop, because it's also utilizing in the loop condition, or else I and lengte would "meet" halfway through the iteration." Is that correct? Can somebody assist me?

in Competitive-Programming by Expert (650 points) | 728 views

Please log in or register to answer this question.