给定正整数N ,任务是找到将N转换为位数为偶数的奇数所需的最小位数。如果无法这样做,请打印“不可能”。
例子:
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
方法:想法是利用这样的事实,即奇数的偶数之和会产生偶数。请按照以下步骤解决问题:
- 计算存在于整数N中的奇数和偶数总数 并将它们存储在变量中,例如Odd和Even。
- 如果Odd = 0 ,则无法通过删除任意数字将整数转换为奇数整数。因此,打印“不可能” 。
- 否则,如果Odd = 1 ,则要使数字之和为偶数,必须删除该奇数位,这将导致偶数。因此,打印“不可能”。
- 现在,计算最后一次出现奇数位后要删除的位数,并将其存储在变量中,例如ans 。
- 如果Odd为奇数,则将ans的计数加1,因为必须删除一个奇数以使总和为偶数。
- 最后,如果上述情况都不满足,请打印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 prthe 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
输出:
1
时间复杂度: O(log 10 (N))
辅助空间: O(1)