Given an array of size N. Choose a random pair of elements from the sequence such that their sum is even, delete those two elements from the sequence and insert their sum into the sequence instead in order to minimize the length of the array. Finally, print the minimum possible size of the array.
Input : 88 98 1 7 3 Output : 2 By following above rules --[88, 98, 1, 10] [98, 88, 1]---[186, 1]--we cannot move further since 186 + 1 = 187, which is not even. So, size = 2. Input : 7 4 3 2 6 Output : 1 delete 7 and 3, insert 10---[10, 4, 2, 6] repeating the process of deleting and inserting---[14, 8]-- size of array becomes 1.
A pair of numbers can sum upto an even number if both the numbers are even or both of them are odd. So, we just need to count the odd numbers present in the given array. Answer can either be 2 or 1 (and nothing else), depending upon the condition. If total number of odds in the array is an odd number then print 2 otherwise print 1.
Time Complexity O(n)
Space Complexity O(1).
- Minimum product pair an array of positive Integers
- Minimum pair sum operations to make array each element divisible by 4
- Minimum element whose n-th power is greater than product of an array of size n
- Minimum cost of choosing 3 increasing elements in an array of size N
- Minimum cost to make array size 1 by removing larger of pairs
- Minimum XOR Value Pair
- Find top three repeated in array
- N/3 repeated number in an array with O(1) space
- Merge an array of size n into another array of size m+n
- Smallest element in an array that is repeated exactly 'k' times.
- Largest element in the array that is repeated exactly k times
- Maximum subarray sum in an array created after repeated concatenation
- Given a sorted array and a number x, find the pair in array whose sum is closest to x
- Find maximum array sum after making all elements same with repeated subtraction
- Minimum cost to reverse edges such that there is path between every pair of nodes
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : vt_m