📜  将给定整数转换为数字总和为偶数的奇数的最小删除| 2套

📅  最后修改于: 2021-10-26 05:56:39             🧑  作者: Mango

给定一个正整数N ,任务是找到将N转换为数字总和为偶数的奇数所需去除的最小位数。如果不可能,则打印“不可能”。

例子:

方法:这个想法是利用奇数的偶数相加得到偶数这一事实。请按照以下步骤解决问题:

  • 计算整数N 中存在的总奇数偶数位数 并将它们存储在变量中,比如奇数偶数。
  • 如果Odd = 0 ,则不能通过删除任意数量的数字将整数转换为奇数。因此,打印“不可能”
  • 否则,如果Odd = 1 ,则要使数字之和为偶数,则必须删除该奇数,从而产生偶数。因此,打印“不可能”。
  • 现在,计算最后一次出现奇数后要删除的位数,并将其存储在变量中,例如ans
  • 如果Odd是奇数,则将ans的计数加一,因为必须删除一个奇数才能使和为偶数。
  • 最后,如果以上情况都不满足,则打印ans

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
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);
}


Java
// Java implementation of the above approach
import java.util.*;
class GFG
{
 
// Function to find minimum count of digits
// required to be remove to make N odd and
// the sum of digits of N even
static 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 (int it : N.toCharArray())
    {
 
        // 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)
    {
        System.out.print("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 (int it : N.toCharArray())
        {
 
            // 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 != 0)
        {
 
            // Update ans
            ans++;
        }
 
        // Finally print the ans
        System.out.print(ans +"\n");
    }
}
 
// Driver code
public static void main(String[] args)
{
 
    // Input String
    String N = "12345";
 
    // Function call
    minOperations(N);
}
}
 
// This code is contributed by shikhasingrajput.


Python3
# Python implementation of the above approach
 
# Function to find minimum count of digits
# required to be remove to make N odd and
# the sum of digits of N even
def minOperations(N):
   
    # Stores count of even digits
    even = 0;
 
    # Stores count of odd digits
    odd = 0;
 
    # Iterate over the digits of N
    for it in  N:
 
        # If current digit is even
        if (int(ord(it) - ord('0')) % 2 == 0):
 
            # Update even
            even += 1;
 
        # Otherwise
        else:
 
            # Update odd
            odd += 1;
 
    # Base conditions
    if (odd == 0 or odd == 1):
        print("Not Possible" + "");
 
    else:
 
        # Stores count of digits required to be
        # removed to make N odd and the sum of
        # digits of N even
        ans = 0;
 
        # Iterate over the digits of N
        for it in N:
 
            # If current digit is even
            if (int(ord(it) - ord('0')) % 2 == 0):
 
                # Update ans
                ans += 1;
 
            # Otherwise,
            else:
 
                # Update ans
                ans = 0;
 
        # If count of odd digits is odd
        if (odd % 2 != 0):
           
            # Update ans
            ans += 1;
 
        # Finally print the ans
        print(ans, end=" ");
 
# Driver code
if __name__ == '__main__':
   
    # Input String
    N = "12345";
 
    # Function call
    minOperations(N);
 
# This code is contributed by shikhasingrajput


C#
// C# implementation of the above approach
using System;
class GFG
{
 
// Function to find minimum count of digits
// required to be remove to make N odd and
// the sum of digits of N even
static 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
    foreach (int it in N.ToCharArray())
    {
 
        // 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)
    {
        Console.Write("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
        foreach (int it in N.ToCharArray())
        {
 
            // 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 != 0)
        {
 
            // Update ans
            ans++;
        }
 
        // Finally print the ans
        Console.Write(ans +"\n");
    }
}
 
// Driver code
public static void Main(String[] args)
{
 
    // Input String
    String N = "12345";
 
    // Function call
    minOperations(N);
}
}
 
// This code is contributed by shikhasingrajput


Javascript


输出:
1

时间复杂度: O(log 10 (N))
辅助空间: O(1)