The Wayback Machine - https://web.archive.org/web/20210128232712/https://www.geeksforgeeks.org/minimum-deletions-to-convert-given-integer-to-an-odd-number-whose-sum-of-digits-is-even-set-2/
Related Articles

Related Articles

Minimum deletions to convert given integer to an odd number whose sum of digits is even | Set 2
  • Last Updated : 28 Jan, 2021

Given a positive integer N, the task is to find the minimum number of digits required to be removed to convert N to an odd number whose sum of digits is even. If it is impossible to do so, then print “Not Possible”.

Examples:

Input: N = 12345
Output: 1
Explanation:
Removing the digit 3 modifies N to 1245.
Since the sum of digits of N is 14 and N is odd, the required output is 1.

Input: N = 222
Output: Not Possible

Approach: The idea is to use the fact that summation of even count of odd numbers results in an even number. Follow the steps below to solve the problem:



  • Count total odd and even digits present in the integer N and store them in variables, say Odd and Even.
  • If Odd = 0, then one cannot convert the integer to an odd integer by deleting any number of digits. Hence, print “Not Possible”.
  • Otherwise, if Odd = 1, then to make the sum of digits even, one will have to delete that odd digit, which results in an even number. Therefore, print “Not Possible”.
  • Now, count the number of digits to be deleted after the last occurrence of an odd digit and store it in a variable, say ans.
  • If Odd is odd, then increment the count of ans by one, as one will have to delete an odd digit to make the sum even.
  • Finally, print ans if none of the above cases satisfies.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find minimum count of digits
// required to be remove to make N odd and
// the sum of digits of N even
void minOperations(string& N)
{
  
    // Stores count of even digits
    int even = 0;
  
    // Stores count of odd digits
    int odd = 0;
  
    // Iterate over the digits of N
    for (auto it : N) {
  
        // If current digit is even
        if ((it - '0') % 2 == 0) {
  
            // Update even
            even++;
        }
  
        // Otherwise
        else {
  
            // Update odd
            odd++;
        }
    }
  
    // Base conditions
    if (odd == 0 || odd == 1) {
        cout << "Not Possible"
             << "\n";
    }
  
    else {
  
        // Stores count of digits required to be
        // removed to make N odd and the sum of
        // digits of N even
        int ans = 0;
  
        // Iterate over the digits of N
        for (auto it : N) {
  
            // If current digit is even
            if ((it - '0') % 2 == 0) {
  
                // Update ans
                ans++;
            }
  
            // Otherwise,
            else {
  
                // Update ans
                ans = 0;
            }
        }
  
        // If count of odd digits is odd
        if (odd % 2) {
  
            // Update ans
            ans++;
        }
  
        // Finally print the ans
        cout << ans << endl;
    }
}
  
// Driver code
int main()
{
  
    // Input string
    string N = "12345";
  
    // Function call
    minOperations(N);
}

chevron_right


Output:

1

Time Complexity: O(log10(N))
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :