- Related Questions & Answers
- Conway’s Game Of Life using Python?
- How to delete all children's elements using Python's Tkinter?
- Hangman Game in Python?
- Jump Game in Python
- Baseball Game in Python
- What is Python's OS Module
- What is Python's Sys Module
- 1's Complement vs 2's Complement
- Hangman Game in Python Programming
- Jump Game II in Python
- JavaScript equivalent of Python's zip function
- How does Python's Matplotlib.pyplot.quiver exactly work?
- Generating Random id's using UUID in Python
- Kirchoff's Theorem
- Color game using Tkinter in Python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a friend named Bob, and he is playing a game with himself. He gives himself a list of numbers called nums. Now in each turn, Bob selects two elements of the list and replaces them with one positive integer with the same sum as the numbers he selected. Bob declares the victory when all of the number in the array are even. We have to find the minimum number of turns are required to make by Bob, so he can declare victory, if there is no such solution, then return -1.

So, if the input is like [2, 3, 4, 9, 7, 13], then the output will be 2 as he can take 3,9 then replace with 12, then take 7,13 and replace with 20.

To solve this, we will follow these steps −

a := a list by taking only odd elements from numes

if size of a is odd, then

return (size of a)/2

otherwise return -1

Let us see the following implementation to get better understanding −

class Solution: def solve(self, nums): a = [x for x in nums if x %2 == 1] if len(a) %2 == 0: return len(a)/2; return -1; ob = Solution() print(ob.solve([2, 3, 4, 9, 7, 13]))

[2, 3, 4, 9, 7, 13]

2

Advertisements