Why is ArrayIndexOutOfBounds Exception Occuring?

Hey guys, I am getting AIOB Exception(i know why this exception occurs, also i know i can handle them) but in my code i cant figure out why is it occurring.
Thanks for Any and Every Help.
And any critics are highly encouraged regarding the way i code and any(even slightest) improvement,that u can help me with.

Problem Statement:

Code:


    import java.util.Scanner;

class Array
{ 
    int i,fls=0;// variable as a flag to check if an array is beautiful or not.
    static int size=new Scanner(System.in).nextInt();
    static int[] array = new int[size];
    
 public void getArray()
  {
      for (i=0;i<array.length;i++)
      {
         array[i]=new Scanner(System.in).nextInt();
      }
          primaryOp();            
  }

  void primaryOp()
  {
      if(fls==0)
      {
      for(i=0;i<array.length;i++)
      {
        for(int j=1;j<array.length;j++)
        {
            scanArray(array[i],array[j]);
        }
      }
          System.out.println("yes");
      }
      else
      System.out.println("no");
  }




    boolean scanArray(int x, int y)
    {
        for(i=0;i<array.length;i++)
        {
          if(array[i]==(x*y))
          {
              return true;  
          }
        }
        fls++;
        return false;
    }
}


class BeautifulArrays
{
    public static void main(String[] args)
    {
        new Array().getArray();
    }
}

try to make a change in primaryop second for loop array.length-1

1 Like

Lol! Thanks it Worked!
Although i did the same for both the primaryOp’s and scanArray for loops for it to be workin.
Thanks man!

this is because when we are last value of i then there is no value of j

no problem (Y)

You are sure that you arent getting any input/output exception as well?

Yeah,not until now.Thanks for the concern. :slight_smile:

although i think i have confused the question of beautiful arrays,
what i think its askin is that in an array for every two int a,b there must exist a int c which is a times b. and after struggling for a day and a half i think that cant be possible.

Is that so or have i misunderstood??
most of the question are too confusing for a newbie like me.

would anyone Explain me Beautiful Arrays?

Its not a beginner Q to be honest. Try other Q, it might be too tough for you atm.

Yes, if you have more than 2 elements such that abs(arr[i])>1 , then its impossible for the array to be beautiful, since product of maximum x second_maximum will not be there.

A will give you a big hint, a beautiful array can be a combination of 0,1,-1, only_1_element_with_abs(arr[i])>1

1 Like

Try running it on codechef IDE before submitting. Codechef IDE can tell if you will get a exception or not, for the custom input you give.

ohhh yeah…that makes sense.
Thanks alot Vijju!! :slight_smile:

Suggestion Noted!!
Thanks :slight_smile:

//