+ 1

My code is not passing all the test cases..I updated my code but can it give the correct output on all possible test test cases

Question: Sumit has always believed in his lucky number. Since childhood, his favorite and been lucky number has been 4 So, his teacher tried to test his skills. She gave Sumit a sequence of an integer numbers. As Sumit's lucky number is 4, she gave the following conditions to solve the sequence problem: A sequence A [] of N integer numbers is given. For each step, you can pick 2 elements in the sequence, remove them, and compute their sum. Then append this sum at the end of the list You can iterate the above pair of steps any number of times. In the end, you need to make sure, that each element of the sequence is divisible by 4. Output the minimum number of steps you need to perform to make sure that all the elements of the updated sequence are divisible by 4. If it is not possible to convert the entire sequence Test case: input 7 1 2 3 1 2 3 8 output: 3 input 4 2 4 6 8 output 1 code!!!! https://code.sololearn.com/cm17N4NOaFcA

30th Aug 2022, 2:31 AM
gokul N
gokul N - avatar
18 Respostas
+ 1
Quantum See my code 1st test case passed the index was wrong i changed it now
30th Aug 2022, 9:40 AM
gokul N
gokul N - avatar
+ 1
gokul N Where do you have that task from? - because I want to test my solution on those test cases, you are talking about.
30th Aug 2022, 11:20 AM
Jan
Jan - avatar
+ 1
Quantum Can u share ur solution here? because only when i run the second test cases iam getting error
30th Aug 2022, 12:11 PM
gokul N
gokul N - avatar
+ 1
I have now corrected my program so it performs the right processes during runtime, and the final output is actually the frequency of 4 or minimum of the list when all the elements in the list are divisible to 4. [8, 4, 4, 4] The first example. [4, 8, 8] The second example. The frequency of 4 in the first is that 4 occurs three times and is also the lowest number. Final output: 3 The frequency of 4 in the second is that 4 occurs one time and is also the lowest number. Final output: 1 The count or the min function will produce the same output with the two examples, but if the list had looked differently with bigger numbers, would minimum or the frequency of the lowest number still be the same. In that case, only the minimum of the list would be the right output. Another problem is that if you cannot pick two numbers where the sum is divisible to 4, then the list is not valid, since it will be impossible to create a list where all the numbers are divisible to 4.
31st Aug 2022, 9:17 AM
Jan
Jan - avatar
+ 1
Well, I made a strange test with the following input: "10 26 40 38 48 70 84" I ended up with a list like that: [84, 36, 88, 108] Minimum of that list is 36, so that must be the final output!
31st Aug 2022, 11:00 AM
Jan
Jan - avatar
+ 1
I have researched a little bit more in that funny task, actually with pen and paper this time, and it seems that it doesn't make sense testing for inner pairs of divisibles, because if the first range of iterations with the sum of the first number and one of the other numbers is not divisible to 4 or whatever number you choose, then you will never be able to get a list where all the remaining numbers are divisible with the chosen number. Extending a program to test for that is waste of time, when it comes to this specific task.
31st Aug 2022, 6:57 PM
Jan
Jan - avatar
0
I think the explanation of how to solve that task is some kind of nonsense. You need to go back and request a new explanation from whereever you got that task.
30th Aug 2022, 6:59 AM
Jan
Jan - avatar
0
Quantum Explanation: Step 1: A[0] + A[2] = 1+3=4, appendag it to the end, Updated array A = [212384] Step 2 A[1]+A[3] = 1+3=4, appending it to the end. Updated array A = [22844] Step 3 A[0]+A[1] = 2+2 = 4, appending it to the end, Updated array A = [8444] Now all the elements of this array are divisible by 4. And it required 3 steps to make it happen. Hence the answer is 3.
30th Aug 2022, 7:10 AM
gokul N
gokul N - avatar
0
Quantum In the above scenario, we can perform below steps: Step 1: A[0] A[2] = 2+6=8, appending it to the end, Updated array A = [488]
30th Aug 2022, 7:11 AM
gokul N
gokul N - avatar
0
gokul N Okay, I got it now, but the first explanation was quite confusing.
30th Aug 2022, 9:23 AM
Jan
Jan - avatar
0
gokul N It also means that the final output would be the length of the array - 1, as long as all elements are divisible by 4, so the last operation down to A = [488] shouldn't be performed, because it would destroy the right output.
30th Aug 2022, 9:40 AM
Jan
Jan - avatar
0
gokul N When you have this array, A = [212384] - Why are you omitting A[0] + A[2]?
30th Aug 2022, 10:27 AM
Jan
Jan - avatar
0
gokul N I made a test solution where I used A[0] + A[2] after the first step, 2 + 2, and then the final output would be the length of the array - 1, and it works on both input examples.
30th Aug 2022, 11:07 AM
Jan
Jan - avatar
0
Quantum This is the second test case that I was talking about 4 2 4 6 8
30th Aug 2022, 12:14 PM
gokul N
gokul N - avatar
0
Quantum This task was taken from a website it has only two test cases
30th Aug 2022, 12:16 PM
gokul N
gokul N - avatar
0
gokul N After a little research about that task, then the code was not correct, even though it produced the right output on those two examples, but I'll have a look at it later. It was also written quite quickly without proper testing of the processes during runtime.
30th Aug 2022, 5:18 PM
Jan
Jan - avatar
0
Quantum I updated my code now it's working on both the test cases..but can u tell me if this is the right way ?I am a beginner so i don't know which is better
31st Aug 2022, 6:50 AM
gokul N
gokul N - avatar
0
Bob_Li Thank u
31st Aug 2022, 7:19 AM
gokul N
gokul N - avatar