Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

merge two array with particular position that position is given by user in java

0 votes
asked Jun 27, 2021 by kunjal kanani (640 points)

ex=

a[0]=1;

a[1]=2;

b[0]=3;

then position is 1 after output is

a[0]=1;

a[1]=3;

a[2]=2;

please tell me what is coding for this output in java .

1 Answer

0 votes
answered Jun 28, 2021 by Peter Minarik (84,720 points)
edited Jun 28, 2021 by Peter Minarik

Try to do it first

This looks like a school assignment to me. If others give you the(ir) answer, you won't learn anything from it. So you really should (at least) try to solve it first.

Ask for help for existing code

Don't worry, we're here to help!

Post your solution and we can point out mistakes and help you make it better.

Clean the requirements

For starter, the instruction is not clear.

Why? Well, how do you merge the two arrays? What are the actual requirements?

Given array a (a1, a2, ..., an) and b (b1, b2, ..., bm) and start index in array a: k, where b will start to be inserted; what is the merge logic?

  1. It could be that the whole b is put into that k location within a: a1, a2, ..., ak, b1, b2, ..., bm, ak+1, ak+2, ..., an.
  2. Or starting from index k, you put elements of a and b alternating into the result array: a1, a2, ..., ak, b1, ak+1, b2, ak+2, b3, ..., an-1, bn-k, an, bn-k+1, bn-k+2, ..., bm.
  3. Or starting from index k, you merge into the result by taking the smallest of the next elements from a or b. So for instance for a = [1, 3, 7, 9] and b = [2, 4, 5, 6, 8] and k = 2 you would have the following result: a1, a2, b1, b2, b3, b4, a3, b5, a4, which is 1, 3, 2, 4, 5, 6, 7, 8, 9.

And one could come up with many more logics for merge. I think clearing up the requirements is the first step.

When you know what is exactly needed, try to write the code. If you need help, please, share what you have done so far (including what exactly the requirements are) and we'll be more than happy to help.

Good luck!

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...