Given an array arr[] positive integers, the task is to find the number of ways to convert the array sum even if we are allowed to remove only one element.
Examples:
Input: arr[] = { 1, 3, 3, 2 }
Output: 3
Explanation:
1. Remove 1, then sum is 3 + 3 + 2 = 8.
2. Remove 3, then sum is 1 + 3 + 2 = 6.
3. Remove 3, then sum is 1 + 3 + 2 = 6.
Input: arr[] = { 4, 8, 3, 3, 6 }
Output: 3
Explanation:
1. Remove 4, then sum is 8 + 3 + 3 + 6 = 20.
2. Remove 8, then sum is 4 + 3 + 3 + 6 = 16.
3. Remove 6, then sum is 4 + 8 + 3 + 3 = 18.
Approach: The key observation to the above problem statement is:
- If we have an odd number of odd elements then the sum is always odd then we have to remove one odd number from the array arr[] to make the sum even. Since we have to remove one element, therefore, the total number of ways of making the sum even is the count of odd elements in the array arr[].
- If we have an even number of odd elements then the sum is always even. Since we have to remove one element to make the sum even, therefore, the total number of ways of making the sum even is the count of even elements in the array arr[]
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include<bits/stdc++.h> using namespace std; // Function to find a number of ways // to make array element sum even by // removing one element int find_num_of_ways( int arr[], int N) { int count_even = 0, count_odd = 0; // Finding the count of even // and odd elements for ( int i = 0; i < N; i++) { if (arr[i] % 2) { count_odd++; } else { count_even++; } } // If count_odd is odd then // no. of ways is count_odd if (count_odd % 2) { return count_odd; } // Else no. of ways is count_even else { return count_even; } } // Driver Code int main() { // Given array arr[] int arr[] = { 1, 3, 3, 2 }; int N = sizeof (arr) / sizeof (arr[0]); // Function Call cout << find_num_of_ways(arr, N); return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to find a number of ways // to make array element sum even by // removing one element static int find_num_of_ways( int arr[], int N) { int count_even = 0 , count_odd = 0 ; // Finding the count of even // and odd elements for ( int i = 0 ; i < N; i++) { if (arr[i] % 2 == 1 ) { count_odd++; } else { count_even++; } } // If count_odd is odd then // no. of ways is count_odd if (count_odd % 2 == 1 ) { return count_odd; } // Else no. of ways is count_even else { return count_even; } } // Driver Code public static void main (String[] args) { // Given array arr[] int arr[] = { 1 , 3 , 3 , 2 }; int N = 4 ; // Function call System.out.print(find_num_of_ways(arr, N)); } } // This code is contributed by Ritik Bansal |
Python3
# Python3 program for the above approach # Function to find a number of ways # to make array element sum even by # removing one element def find_num_of_ways(arr, N): count_even = 0 count_odd = 0 # Finding the count of even # and odd elements for i in range (N): if (arr[i] % 2 ): count_odd + = 1 else : count_even + = 1 # If count_odd is odd then # no. of ways is count_odd if (count_odd % 2 ): return count_odd # Else no. of ways is count_even else : return count_even # Driver Code if __name__ = = '__main__' : # Given array arr[] arr = [ 1 , 3 , 3 , 2 ] N = len (arr) # Function call print (find_num_of_ways(arr, N)) # This code is contributed by mohit kumar 29 |
C#
// C# program for the above approach using System; class GFG{ // Function to find a number of ways // to make array element sum even by // removing one element static int find_num_of_ways( int []arr, int N) { int count_even = 0, count_odd = 0; // Finding the count of even // and odd elements for ( int i = 0; i < N; i++) { if (arr[i] % 2 == 1) { count_odd++; } else { count_even++; } } // If count_odd is odd then // no. of ways is count_odd if (count_odd % 2 == 1) { return count_odd; } // Else no. of ways is count_even else { return count_even; } } // Driver Code public static void Main( string [] args) { // Given array arr[] int []arr = { 1, 3, 3, 2 }; int N = 4; // Function call Console.Write(find_num_of_ways(arr, N)); } } // This code is contributed by Rutvik |
Javascript
<script> // javascript program for the above approach // Function to find a number of ways // to make array element sum even by // removing one element function find_num_of_ways(arr , N) { var count_even = 0, count_odd = 0; // Finding the count of even // and odd elements for (i = 0; i < N; i++) { if (arr[i] % 2 == 1) { count_odd++; } else { count_even++; } } // If count_odd is odd then // no. of ways is count_odd if (count_odd % 2 == 1) { return count_odd; } // Else no. of ways is count_even else { return count_even; } } // Driver Code // Given array arr var arr = [ 1, 3, 3, 2 ]; var N = 4; // Function call document.write(find_num_of_ways(arr, N)); // This code is contributed by Amit Katiyar </script> |
3
Time Complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!