#Need Help With Java method code

1 messages · Page 1 of 1 (latest)

ornate muskBOT
#

@restive tulip

Godhero810 Uploaded Some Code

can someone help me with the merge method
here is my code so far

Uploaded these files to a Gist
restive tulip
#

<@&934250633580658788>

cunning compass
#

Hello!

#

One moment while I read your message

restive tulip
#

ok

cunning compass
#

Question: does your solution have to be the most efficient?

restive tulip
#

not really

#

i know you can do it in one line someone told me but its stuff we havent learned

#
public static int[] merge(int[] a, int[] b){
    int IndexInResult = 0, IndexA = 0, IndexB = 0;
    int [] FinalOutcome = new int[b.length + a.length];


    while((b.length > IndexB) || (a.length > IndexA))
    {
      if(IndexA > a.length)
      {
        FinalOutcome[IndexInResult] = b[IndexB];


        IndexB++;
      }
      else if(IndexB > b.length)
      {
        FinalOutcome[IndexInResult] = a[IndexA];


        IndexA++;
      }
      else if(b[IndexB] > a[IndexA])
      {
        FinalOutcome[IndexInResult] = a[IndexA];


        IndexA++;
      }
      else if(b[IndexB] < a[IndexA])
      {
        FinalOutcome[IndexInResult] = b[IndexB];


        IndexB++;
      }
      IndexInResult++;
    }

    return FinalOutcome;
  }
#

i did this so far

cunning compass
#

Are you allowed to use Arrays.sort(arr);

restive tulip
cunning compass
#

Lol dang

restive tulip
#

yeah idk

cunning compass
#

One moment please I'm actually about to wash the dishes 😅 but I'm not ignoring you! I will be back

restive tulip
#

ok!

cunning compass
#

I'm back!

restive tulip
#

Hi i was actually able to figure this one out

#

im just stuck on this one now

#

its part of that its just the last method

cunning compass
restive tulip
#

oh lol

#

would you have any idea how to do this one?

cunning compass
#

Yeah, so one way you could go about doing it is by keeping track of the current largest sequence, and as you find more sorted sequences, keep track of which length is longer

#

You could have variables for the current longest sorted sequence, and the current sequence