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).
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Given an array A and a number x, check for pair in A with sum as x
- Search an element in a sorted and rotated array
- Merge an array of size n into another array of size m+n
- Median of two sorted arrays of same size
- k largest(or smallest) elements in an array | added Min Heap method
- Program for array rotation
- Write a program to reverse an array or string
- Reversal algorithm for array rotation
- Block swap algorithm for array rotation
- Leaders in an array
- Count Inversions in an array | Set 1 (Using Merge Sort)
- Find the smallest and second smallest elements in an array
- Maximum and minimum of an array using minimum number of comparisons
- Check for Majority Element in a sorted array
- Segregate 0s and 1s in an array
Improved By : vt_m