Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi fellow developers I'm participating in a developer contest in my county and I have got practice tasks for the exam, I have been successful with most of them however I'm having issues with this one in particular could you please take a look any ideas or assistance is welcomed, the programming language doesn't matter it can be solved in any language, here is the task:

There is an arithmetic progression written on a black-board. Having nothing better to do, you invented a game involving it. Namely, you erase all the even numbers and replace them with the numbers two times smaller.
You repeat this procedure until there is no more even numbers on the board.
For example, you transform the progression {1, 2, 3, 4, 5, 6} into {1, 1, 3, 2, 5, 3} and then once again into {1, 1, 3, 1, 5, 3}.
When your friends come over, you ask them to guess the original progression.
e.g.
Let’s take progression 6, 5, 4, 3, 2, 1
In the first step, it becomes 3, 5, 2, 3, 1, 1
Then it becomes 3, 5, 1, 3, 1, 1
This means for the numbers 3, 5, 1, 3, 1, 1 the solution is 6, 5, 4, 3, 2, 1, resulting in a sum of 21.
Input parameters:
progression - an array of integers representing the original progression after
all the numbers have been brought to odd elements
Constraints:
The number of elements in progression is between 4 and 500 inclusive.
Each element of progression is between 1 and 1000000 inclusive.
All elements of progression are odd.
Return value:
The sum of the elements of the original arithmetic progression
When there is more than one solution, return the one that has smaller sum of all the elements.
Note that there will always be a solution where each number of the original progression is smaller than 1000000.
Class Name:
Progression
Method signature:
public int reverse(int[] progression)
TEST CASES
Test Case 1: reverse({1, 1, 3, 1, 5, 3}) = 21
Test Case 2: reverse({1, 1, 1, 1, 1, 1}) = 6
Test Case 3: reverse({3, 3, 3, 3}) = 12
Test Case 4: reverse({3, 5, 1, 3, 1, 1}) = 21
Test Case 5: reverse({21, 27, 33, 39, 45, 51}) = 216
Test Case 6: reverse({37, 15, 23, 1, 9, 1}) = 117
Test Case 7: reverse({1019, 1077, 1135, 1193, 1251, 1309, 1367, 1425, 1483, 1541}) = 12800
Test Case 8: reverse({1103, 2067, 241, 1789, 825, 1511, 343, 1233, 547, 955, 51, 677, 269, 399, 65, 121}) = 18616
Test Case 9: reverse({5, 5, 5, 5, 5}) = 25
Test Case 10: reverse({5, 1, 11, 7, 17}) = 55

What I have tried:

I first tried when all elements in the array are the same, just to return their sum and I make another array with the difference of all the elements from the fist one and if the second array has all the same elements, just return their sum. I have no idea for the other test cases, which number should be multiplied by two which not.
Posted
Updated 4-Jul-17 14:43pm
Comments
PIEBALDconsult 4-Jul-17 21:37pm    
I wonder what your program will do with:
1 , 1 , 1 , 3 , 5 , 1 , 13 , 21 , 17 , 55

(Corrected. But now I see that this is an Arithmetic Series, but not an Arithmetic Sequence.)
MarijaMKBT 8-Jul-17 19:29pm    
I have no idea. If I succeed to write the correct algorithm that is solving correct all of the test cases, we can try your example too. Thanks for posting this.
Patrice T 8-Jul-17 21:49pm    
As far as I can see, this is impossible.
PIEBALDconsult 8-Jul-17 23:17pm    
It's not one I'm likely to try myself.
Patrice T 8-Jul-17 23:27pm    
I am curious to see the solution, because just 1, 1, 1, 3 can't lead to an arithmetic progression.

1 solution

Quote:
I'm participating in a developer contest in my county

The principle is that you find the algorithms yourself.

Take a sheet of paper and a pencil and try to solve the tests examples by hand.
How do you proceed ? your solving method is the algorithm, you just have to translate in a program.

If we solve it for you, we can as well participate to the contest. And if you use our solution, it would be cheating.
question cross posted on Developer contest algorithm needed - Stack Overflow[^]
 
Share this answer
 
v2
Comments
MarijaMKBT 8-Jul-17 19:26pm    
Thanks @ppolymorphe for the answer. Just to make clear this task is given on the contest page as a example for practicing and won't be given on the contest, I don't see why if someone help me it would be a cheating.
I tried with sheet and pencil and I found the solution, but I need help to figure it out how to make an algorithm. I only have idea for the cases I described in the "What I have tried:" part.
I need help or some hint for the other cases like: which number should be multiplied with two, which shouldn't or how many times it should be multiplied with two.
I think it would be great if I can determine the difference between each element, and then the program can assume the next element and if the current one is smaller then that one it will multiply by two until they become equal. But I have no idea how to do that.
Patrice T 8-Jul-17 21:07pm    
You can solve the exercises by hand, and you don't use magic.
Think about it: you follow a procedure !
- note the procedure.
- take an exercise and apply the procedure mechanically.
- if you fail to solve the exercise, refine the procedure and start again.
- when all exercises are solved, you got algorithm/procedure.

If you can solve by hand but unable to translate to algorithm, it probably mean you are thought enough to participate to such a contest.
Algorithm design - Wikipedia[^]

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