+ 2
Changing a list to another in minimum steps
Can you help me in converting a list of 3 numbers from to another list of 3 number using either addition or multipication (of 1, 2 or all the 3 elements in the list) by any integer. I want to calculate the minimum no of operations to do so... e.g. [3 8 7] to [8 9 14] (given in question) Ans: 2 First add 1 to 3 and 8 which makes it [4 9 7] and then multiply 4 and 7 by 2 which makes it [8 9 14] I have tried to do it using if else statement but I need an optimized solution. I don't need any code I just need an optimised method or algorithm to solve it.
24 ответов
+ 2
Oma Falk Abhay Leo the Learner
Thanks a lot guys for your contribution in helping me solve this question. I have finally constructed an optimal method for the above problem and i feel overwelmed to share it with you all. Special thanks to Oma Falk
Here is the code link: https://code.sololearn.com/cyFXSYTeERam/?ref=app
I request you all to check the code with various inputs.
Dear [𝐀𝐩𝐩 𝐮𝐧𝐢𝐧𝐬𝐭𝐚𝐥𝐥𝐞𝐝] (whatever your name is), i hope i could teach you the correct method to solve this question but to my displeasure you have uninstalled the app.😂😂😂
+ 5
Can you show what you have attempted so far?
+ 5
Abhay i think before code to show some algorithm thoughts are required.
+ 4
interesting🤔🤔🤔
+ 4
Amulya Varshney
please check my program.
I hope it gives correct strategies to mimimize steps.
Please test with different values.
+ 4
Oma Falk
output of your code is:
The lists are [3, 8, 7] and [8, 9, 14]
The points are [(3, 8), (8, 9), (7, 14)]
[(7.4, 0.2), (49.0, -5.0), (3.5, 1.5)]
Sorry three steps required
instead the solution is:
only 2 steps required.
first add 1 to 3 and 8 which makes it [4, 9, 7]
then multiply 4 and 7 by 2 which results in [8,9,14]
you need to update your code so that it gives an optimised result.
+ 3
In javascript can be used for it e.g. the function map().
+ 3
Guys I have tried explained the question complete even then if you have any query regarding the question, you can ask and I don't need any code I just need an optimised method or algorithm to solve it
+ 2
I have tried to done it using if else statement but I need an optimized solution
+ 2
Oma Falk the answer is already in the question or he has mentioned ,first add 1 and then multiply by 2 ,and that does in 2 operations,
+ 2
Amulya Varshney then paste the code here so we could see what you did pls!
+ 2
Leo the Learner I need to do it in minimum number of operations and require optimal solution for the same
+ 2
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 Abhay Oma Falk Leo the Learner Ja Play
Please read the question properly, it's a list of 3 numbers
Eg.
[2 3 4] to [4 8 8]
Step 1: I will add 1 to 3 and make it 4 such that
new list is [2 4 4]
Step 2: now multiply all the 3 elements by 2
New list is [4 8 8]
And it is the answer (output should be 2 i.e. the number of minimum steps)
Otherwise there can be various ways to convert the list, ( i can add any random number to any elements and then subtract it, this will only increase the steps)
Clearly stating --> *I need minimum number of steps(operations i.e either multipication or addition) and the list needs to be of 3 elements
+ 2
Leo the Learner this can't be done in less than 3 steps but there will be some sequences where it is possible to reduce the number of steps. My focus is on those problems.
+ 2
Do you mean something like this ?is it performing two operations for all list in items ,or you did some other way using for loop?
a=[3,8,7]
j=0
b=[]
c=[]
for i in a:
if j<2:
i+=1
b.append(i)
i=0
else:
b.append(i)
j+=1
j=0
for k in b:
if j%2==0:
k*=2
c.append(k)
k=0
else:
c.append(k)
j+=1
print(c)
+ 2
https://code.sololearn.com/cil0XKWyn5dx/#py
a first step, try with different values on l0,l1
+ 2
Amulya Varshney
ok thanks for testing
I have an idea for update.
+ 1
Ok... if you get initial and final version of the list why don't you subtract from second list elements first list elements...
For your e.g.
[3 8 7] [8 9 14]
8-3=5 -> add 5 to first element
9-8=1 -> add 1 to second element
14-7=7 -> add 7 to third element
You said "using either addition or multiplication". This is the simplest way to do it. In case you have some more complex conditions than getting two arrays and converting first array in second array by addition OR multiplication.
+ 1
Amulya Varshney Ok, minimum number of operations, let's count.
The way I thought it.
Has 3 subtractions for differences then 3 additions to convert first array in second one.
Question: in what case you can break this 6 operations?
Until you find an algorithm to deal with general case... that has less than 3 operations to transform 3 numbers in other 3 numbers...
+ 1
Amulya Varshney ... add 1 to 3 (first operation)
Multiply 2 by 2 (second)
Multiply 4 by 2 (third)
Multiply 4 by 2 (fourth)
Isn't it right?
If you have [9 129 4] to [81 0 7]
How do you do it?