Click here to Skip to main content
15,789,698 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I got a challenge from my teacher and I would like to have an idea on how to solve it: input from the user int 'n'. Given an array filled in with numbers: every number from '1' to 'n' appears twice in the array. (when n=2: two 1's two 2's) the goal is to sort the numbers in the array so that the distance from every pair of numbers will be the same as the value of the number.
Example:
int n = 4;
int [] sortedArray = {3 , 4 , 2 , 3 , 2 , 4 , 1 , 1};


as you can see, the distance from the first '3' to the next '3' is 3, the distance from the first '2' to the next '2' is 2 and so on.

The length of the array will be n*2.
There are some values of 'n' which would not be possible to sort (like 10,2)

What I have tried:

I have tried to fill the array with n=5, and search for a kind of a pattern.
n=5
int [] sortedArray = {1,1,3,4,5,3,2,4,2,5};


couldn't find any pattern.
Posted
Updated 17-May-17 9:45am
v2

Google translate suggests the following: "I got given my homework and I want you to do it" - hence the duplicate posting on Stack overflow java - filling the array from 1 to n when the distance from number to number is the number itself - Stack Overflow[^]

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.
If nothing else springs to mind, there is always the brute-force-and-ignorance solution of trying every combination until one works...

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
Comments
Member 13206694 17-May-17 12:36pm    
Sorry :3
You don't know a specific way to fill the array.
But you know how to fill the array by hand, you follow an algorithm that you invented.
Your program will follow the same algorithm.
Come back if you get a specific problem on your code.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.

The idea of "development" is as the word suggests: "The systematic use of scientific and technical knowledge to meet specific objectives or requirements." BusinessDictionary.com[^]
That's not the same thing as "have a quick google and give up if I can't find exactly the right code".
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900