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).
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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
- Ceiling in a sorted array
- Equilibrium index of an array
- A Product Array Puzzle
- Find the two repeating elements in a given array
- Sort an array of 0s, 1s and 2s
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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