通过反转任何长度的前缀来使 N 甚至所需的最小操作
给定一个整数N ,任务是找到使N相等所需的最小操作数,以便在一个操作中任何长度的前缀都可以反转。如果不可能将N设为偶数,则打印-1。
例子:
Input: N = 376502
Output: 0
Explanation:
– N is already divisible by 2, hence it is an even number so the total number of prefix swaps will be 0.
Input: N = 36543
Output: 2
Explanation:
– N is not an even number so to make it even first swap the prefix of length 2. Now N=63543
– Now swap the prefix of length 5 and N=34536.
方法:
可以有2种情况。
- N is an even number
- N is an odd number.
现在,
- 如果N是偶数,则使N成为偶数的最小步数将为 0。
- 如果N是奇数,则可能有 3 种情况。
- N不包含任何偶数位。在这种情况下,不可能使N成为偶数,因此答案将为 -1。
- N包含偶数位,并且N的第一个数字是偶数。在这种情况下,我们可以交换整数,因此使N成为偶数的最小步数将是 1。
- N包含偶数位,并且N的第一个数字不是偶数。在这种情况下,我们可以先将前缀交换到任何偶数位,然后交换整数,因此使N成为偶数的最小步数将是 2。
请按照以下步骤解决问题:
- 将字符串s[]初始化为N的字符串表示。
- 将变量ans初始化为-1 ,将len初始化为字符串s[] 的长度。
- 如果(s[len-1] – '0')%2==0则设置为0。
- 否则,请执行以下任务:
- 如果字符串s[]的第一个字符是偶数,则将ans设置为1。
- 使用变量i遍历范围[0, len)并执行以下任务:
- 如果s[i]是偶数,则将ans设置为2并中断。
- 执行上述步骤后,打印ans的值作为答案。
以下是上述方法的实现:
C++
// C++ code for the above approach
#include
using namespace std;
// Function to find minimum number
// of steps required to make N an even number
void MinSteps(int N)
{
// Converting N into string
string s = to_string(N);
int ans = -1;
// Number of digits in N
int len = s.size();
// If the number is even
if ((s[len - 1] - '0') % 2 == 0) {
ans = 0;
}
else {
// If the first digit is even
if ((s[0] - '0') % 2 == 0) {
ans = 1;
}
else {
// Checking if s contains
// any even digits
for (int i = 0; i < len; i++) {
if ((s[i] - '0') % 2 == 0) {
ans = 2;
break;
}
}
}
}
// Printing the minimum number
// of steps to make N an even number
cout << ans << "\n";
}
// Driver Code
int main()
{
int N;
N = 36543;
MinSteps(N);
}
Java
// Java code for the above approach
import java.util.*;
class GFG
{
// Function to find minimum number
// of steps required to make N an even number
static void MinSteps(int N)
{
// Converting N into String
String s = String.valueOf(N);
int ans = -1;
// Number of digits in N
int len = s.length();
// If the number is even
if ((s.charAt(len - 1) - '0') % 2 == 0) {
ans = 0;
}
else {
// If the first digit is even
if ((s.charAt(0) - '0') % 2 == 0) {
ans = 1;
}
else {
// Checking if s contains
// any even digits
for (int i = 0; i < len; i++) {
if ((s.charAt(i) - '0') % 2 == 0) {
ans = 2;
break;
}
}
}
}
// Printing the minimum number
// of steps to make N an even number
System.out.print(ans+ "\n");
}
// Driver Code
public static void main(String[] args)
{
int N;
N = 36543;
MinSteps(N);
}
}
// This code is contributed by 29AjayKumar
Python3
# python3 code for the above approach
# Function to find minimum number
# of steps required to make N an even number
def MinSteps(N):
# Converting N into string
s = str(N)
ans = -1
# Number of digits in N
le = len(s)
# If the number is even
if ((ord(s[le - 1]) - ord('0')) % 2 == 0):
ans = 0
else:
# If the first digit is even
if ((ord(s[0]) - ord('0')) % 2 == 0):
ans = 1
else:
# Checking if s contains
# any even digits
for i in range(0, le):
if ((ord(s[i]) - ord('0')) % 2 == 0):
ans = 2
break
# Printing the minimum number
# of steps to make N an even number
print(ans)
# Driver Code
if __name__ == "__main__":
N = 36543
MinSteps(N)
# This code is contributed by rakeshsahni
C#
// C# code for the above approach
using System;
class GFG
{
// Function to find minimum number
// of steps required to make N an even number
static void MinSteps(int N)
{
// Converting N into String
String s = Convert.ToString(N);
int ans = -1;
// Number of digits in N
int len = s.Length;
// If the number is even
if ((s[len - 1] - '0') % 2 == 0)
{
ans = 0;
}
else
{
// If the first digit is even
if ((s[0] - '0') % 2 == 0)
{
ans = 1;
}
else
{
// Checking if s contains
// any even digits
for (int i = 0; i < len; i++)
{
if ((s[i] - '0') % 2 == 0)
{
ans = 2;
break;
}
}
}
}
// Printing the minimum number
// of steps to make N an even number
Console.Write(ans + "\n");
}
// Driver Code
public static void Main()
{
int N;
N = 36543;
MinSteps(N);
}
}
// This code is contributed by Saurabh Jaiswal
Javascript
输出
2
时间复杂度: O(len),其中 len 是字符串的长度。
辅助空间: O(len)