Make All Even with Minimum Operations

Last Updated : 30 Jul, 2026

Given a string s consisting of the characters 'e' and 'o', where 'e' represents an even number and 'o' represents an odd number, find the minimum number of operations required to make all elements even.

  • In one operation, you can choose any odd element and add 1 to it, making it even.
  • Whenever an element becomes even, its adjacent odd elements (if any) also become even automatically without requiring any additional operation and the conversion propagates through all odd adjacent.

Examples:

Input: s = "eooe"
Output: 1
Explanation: Perform the operation on the second character. It becomes even, which also makes the third character even.

Input: s = "eooooe"
Output: 1
Explanation: Perform the operation on the second character. The conversion propagates through all adjacent odd characters until all of them become even.

Try It Yourself
redirect icon

[Naive Approach] Traverse Every Odd Segment - O(n) Time and O(1) Space

The idea is to traverse the string and whenever an odd element is found, treat it as the start of an odd segment. Count one operation for this segment and skip all consecutive odd elements, since one operation is sufficient to convert the entire segment into even.

Working of Approach:

  • Traverse the string from left to right.
  • If an 'o' is found, increment the operation count.
  • Skip all consecutive 'o' characters of the current segment.
  • Continue scanning the remaining string.
  • Return the total number of odd segments.
C++
#include <iostream>
#include <string>
using namespace std;

int convertToEven(string &s)
{
    int n = s.size();
    int operations = 0;

    for (int i = 0; i < n; i++)
    {

        // Found the start of an odd segment.
        if (s[i] == 'o')
        {
            operations++;

            // Skip the remaining odd elements of this segment.
            while (i < n && s[i] == 'o')
                i++;

            i--;
        }
    }

    return operations;
}

int main()
{

    string s = "eooooe";
    cout << convertToEven(s);

    return 0;
}
Java
import java.util.*;

public class GFG {
    public static int convertToEven(String s)
    {
        int n = s.length();
        int operations = 0;

        for (int i = 0; i < n; i++) {

            // Found the start of an odd segment.
            if (s.charAt(i) == 'o') {
                operations++;

                // Skip the remaining odd elements of this
                // segment.
                while (i < n && s.charAt(i) == 'o')
                    i++;

                i--;
            }
        }

        return operations;
    }

    public static void main(String[] args)
    {
        String s = "eooooe";
        System.out.println(convertToEven(s));
    }
}
Python
def convertToEven(s):
    n = len(s)
    operations = 0
    i = 0

    while i < n:

        # Found the start of an odd segment.
        if s[i] == 'o':
            operations += 1

            # Skip the remaining odd elements of this segment.
            while i < n and s[i] == 'o':
                i += 1
        else:
            i += 1

    return operations


if __name__ == "__main__":
    s = "eooooe"
    print(convertToEven(s))
C#
using System;

public class GFG {
    public static int convertToEven(string s)
    {
        int n = s.Length;
        int operations = 0;

        for (int i = 0; i < n; i++) {

            // Found the start of an odd segment.
            if (s[i] == 'o') {
                operations++;

                // Skip the remaining odd elements of this
                // segment.
                while (i < n && s[i] == 'o')
                    i++;

                i--;
            }
        }

        return operations;
    }

    public static void Main()
    {
        string s = "eooooe";
        Console.WriteLine(convertToEven(s));
    }
}
JavaScript
function convertToEven(s)
{
    let n = s.length;
    let operations = 0;

    for (let i = 0; i < n; i++) {

        // Found the start of an odd segment.
        if (s[i] === "o") {
            operations++;

            // Skip the remaining odd elements of this
            // segment.
            while (i < n && s[i] === "o")
                i++;

            i--;
        }
    }

    return operations;
}

// Driver Code
let s = "eooooe";
console.log(convertToEven(s));

Output
1

[Expected Approach] Count the Start of Every Odd Segment - O(n) Time and O(1) Space

The idea is to observe that one operation converts an entire contiguous segment of odd elements into even. Therefore, the answer is simply the number of odd segments in the string.

Working of Approach:

  • Traverse the string once from left to right.
  • Check whether the current character is 'o'.
  • If it is the first character or the previous character is 'e', a new odd segment starts.
  • Count this segment as one operation.
  • Return the total count.

Let us understand with an example:
Input: s = "eooooe"

  • Traverse the string from left to right and count the start of every odd segment.
  • At index 1, 'o' is preceded by 'e', so a new odd segment starts. Increment the answer to 1.
  • At indices 2, 3, and 4, the previous character is also 'o', so they belong to the same segment and are not counted again.
  • The last character is 'e', so no new odd segment is formed.
  • Hence, there is only one odd segment, so only one operation is required.

Output: 1.

C++
#include <iostream>
#include <string>
using namespace std;

int convertToEven(string &s)
{
    int res = 0;
    for (int idx = 0; idx < s.size(); idx++)
    {

        // Count the starting position of every contiguous odd segment.
        if (s[idx] == 'o' && (idx == 0 || s[idx - 1] == 'e'))
        {
            res++;
        }
    }
    return res;
}

int main()
{

    string s = "eooooe";
    cout << convertToEven(s);

    return 0;
}
Java
import java.util.Scanner;

public class GFG {
    public static int convertToEven(String s)
    {
        int res = 0;
        for (int idx = 0; idx < s.length(); idx++) {

            // Count the starting position of every
            // contiguous odd segment.
            if (s.charAt(idx) == 'o'
                && (idx == 0 || s.charAt(idx - 1) == 'e')) {
                res++;
            }
        }
        return res;
    }

    public static void main(String[] args)
    {
        String s = "eooooe";
        System.out.println(convertToEven(s));
    }
}
Python
def convertToEven(s):
    res = 0
    for idx in range(len(s)):

        # Count the starting position of every contiguous odd segment.
        if s[idx] == 'o' and (idx == 0 or s[idx - 1] == 'e'):
            res += 1
    return res


if __name__ == "__main__":
    s = "eooooe"
    print(convertToEven(s))
C#
using System;

public class GFG {
    public static int convertToEven(string s)
    {
        int res = 0;
        for (int idx = 0; idx < s.Length; idx++) {

            // Count the starting position of every
            // contiguous odd segment.
            if (s[idx] == 'o'
                && (idx == 0 || s[idx - 1] == 'e')) {
                res++;
            }
        }
        return res;
    }

    public static void Main()
    {
        string s = "eooooe";
        Console.WriteLine(convertToEven(s));
    }
}
JavaScript
function convertToEven(s)
{
    let res = 0;
    for (let idx = 0; idx < s.length; idx++) {

        // Count the starting position of every contiguous
        // odd segment.
        if (s[idx] === "o"
            && (idx === 0 || s[idx - 1] === "e")) {
            res++;
        }
    }
    return res;
}

// Driver Code
let s = "eooooe";
console.log(convertToEven(s));

Output
1
Comment