Wednesday, January 13, 2016

(ENG) Algorithm to convert integers each other with minimum number of operations

Question: Convert integer m to integer n with minimum number of operations(m < n). Operations allowed are (-1) and (*2) Eg. 4 and 6.
-1 -> 4 - 1 = 3
*2 -> 3 * 2 = 6
2 operations.


Resource: http://www.careercup.com/page?pid=algorithm-interview-questions
Question Owner: Rahul Sharma

ANSWER

Thank you Emre Sermutlu for your feedbacks about the question.

Let's give one more example: 7 and 10
(-1) (-1) and (*2)

One more example: 6 and 10
(-1) and (*2)

We can see that if (m >= n%2), we use (-1) up to (m = n%2), then (*2)

If (m<n%2),...Let's try to see:

ex 1: 3 and 10
(*2) (-1) (5 is half of 10 this can be key!) and (*2)

ex 2: 4 and 10
(-1) we get 3!!!(2.5 =3 is half of 5) Then continue with same way (*2) (-1) and (*2)

ex 3: 1 and 10
(*2) (*2) (-1) we get 3!!!(2.5 =3 is half of 5) Then continue with the same way again!

Why did we try to get 3 every time? What is 3 for these numbers(4 and 10 or 1 and 10)? That is the main question. (2.5 =3 is half of 5; 5 is half of 10)
5
ex 4: 7 and 17
(-1) (-1)(4.5 = 5 is half of 9) (*2) (-1) This time we get 9!(8.5 = 9 is half of 17) (*2) (-1)

ex 5: 4 and 17
(-1)(2.5 = 3 is half of 5) (*2) (-1)(8.5 = 9 is half of 17) (*2) (-1)

Can you understand the regularity? Every time our "secondary" target is half of "main" target. To reach our secondary target, we always use (-1). And whenever our m is smaller than our secondary target, we update our secondary target with its half. However we shouldn't forget our second targets. Because we will use them again. How many secondary targets is there? We can count them in loop with if statement, or we can use dynamic memory allocation ways.

Let's use this way for one more example and see if it works:
ex 6: 11 and 29

11 < (29%2 = 15)
11 > (15%2 = 8)
11 (-1) (-1) (-1) = 8
When we reach our secondary target, then we update m by (*2), and up to our "next" secondary target(if we couldn't reach) we update m by (-1).
8 (*2) (-1) = 15
15 (*2) (-1) = 29


(ENG/TR) Algorithm to draw diamond shape/ Elmas çizen algoritma

1            *
2           **
3          ***
4         ****
5        *****
6       ******
7      *******
8     ********
9    *********
0   **********
1  ***********
2   **********
3    *********
4     ********
5      *******
6       ******
7        *****
8         ****
9          ***
0           **
1            *


ENGLISH

Whenever we try to find an algorithm, first we should find "regularity". Our diamond's middle which has 11 character length, is the largest entry. We see that we have "direct proportion" from up to middle. Then from middle to last line, we have "inverse proportion". I mean;
1. line, we write 1 * into 10. space
2. line, we write 2 * into 9. space
3. line, we write 3 * into 8. space
...
11. line, we write 11 * into 0. space
12. line, we write 10 * into 1. space
13. line, we write 9 * into 2. space
14. line, we write 8 * into 3. space
...
21. line, we write 1 * into 10. space

Now, it is easy to see the way to write this code: up to line 11 decrease space by 1, increase * by 1; from line 11 up to 21 increase space by 1, decrease * by 1.

TÜRKÇE

Her ne zaman bir algoritma bulmak istesek, ilk olarak "düzen"i bulmalıyız. Elmasımızın 11 karakter uzunluğundaki ortası, en uzun girdimiz. Orta kısma kadar "doğru orantı" olduğunu görüyoruz. Sonra ortadan, son satıra doğru "ters orantı"mız var. Yani:
1. satırda, 1 *'i 10. boşluğa yazıyoruz
2. satırda, 2 *'i 9. boşluğa yazıyoruz
3. satırda, 3 *'i 8. boşluğa yazıyoruz
...
11. satırda, 11 *'i 0. boşluğa yazıyoruz
12. satırda, 10 *'i 1. boşluğa yazıyoruz
13. satırda, 9 *'i 2. boşluğa yazıyoruz
14. satırda, 8 *'i 3. boşluğa yazıyoruz
...
21. satırda, 1 *'i 10. boşluğa yazıyoruz

Şimdi kodumuzu yazmanın yolu kolay: 11. satıra kadar boşluğu 1 azalt, *'i 1 arttır; 11. satırdan 21. satıra kadar boşluğu 1 arttır, *'i 1 azalt.