Find Largest Valued Even Integer which is a Non-Empty Substring of S


In this problem, we need to find the largest even valued substring of the given string. The even string always contains the 2, 4, 6, 8, 0 at last. So, we can take all substrings of the given string, and if any substring is even and greater than the maximum substring, we can update the maximum substring value.

Problem statement – We have given string str containing numeric digits only. We need to find the largest substring of the str, which is an even integer value.

Sample examples

Input

str = "1234789"

Output

123478

Explanation – The largest even substring is 123478.

Input

str = ‘3208’

Output

3208

Explanation – The string is already even. So, It prints the string itself.

Input

str = "13579";

Output

‘Not Possible’

Explanation – The string doesn’t contain any even substring. So, it has printed ‘Not Possible’.

Approach 1

In this approach, we will traverse through all substrings of the given string. If the last character of the substring is divisible by 2, it means the string is even. After that, we will check if the current string is greater than the maximum string. If yes, we replace the maximum string.

Algorithm

Step 1 – Initialize the ‘largeEven’ string with the empty string.

Step 2 – Start traversing the string from the 0th index using the for loop. Inside the loop, initialize the ‘temp’ string and start traversing the string from pth index using the nested loop.

Step 3 – Take the character from the index and append it to the ‘temp’ string.

Step 4 – If the last character of the string is divisible by 2, execute the getMaxStr() string to find the maximum string and store its returned value to the largeEven string.

Step 4.1 – In the getMaxStr() function, If the first string's length is greater than the second string's length, return the first string. Otherwise, return the second string.

Step 4.2 – If the length of both strings is the same, start matching all characters of the string using the loop.

Step 4.3 – If the character at the pth index in the first string is greater, return the first string. Otherwise, return the second string. If characters are the same at the same index, continue to the next iteration.

Step 5 – Return ‘largeEven’ string.

Example

#include <bits/stdc++.h>
using namespace std;

string getMaxStr(string num1, string num2) {
    // Comparing the size of both strings
    if (num1.length() > num2.length()){
        return num1;
    } else if (num1.length() < num2.length()) {
        return num2;
    }
    // For the same length of strings
    for (int p = 0; p < num1.length(); ++p) {
        if (num1[p] > num2[p])
            return num1;
        else if (num1[p] < num2[p])
            return num2;
    }
    // If strings are equal
    return num1;
}
string findStr(string str) {
    // string size
    int len = str.size();
    string largeEven = "";
    // get all substrings of the string
    for (int p = 0; p < len; p++) {
        string temp = "";
        for (int q = p; q < len; q++) {
            temp += str[q];
            int tempLen = temp.size();
            // check if the substring is even
            if ((temp[tempLen - 1] - '0') % 2 == 0)
            {
                // Get maximum string
                largeEven = getMaxStr(largeEven, temp);
            }
        }
    }
    return largeEven;
}
int main() {
    string str = "1234789";
    string result = findStr(str);
    if (result == ""){
        cout << "Not possible";
    } else {
        cout << "The largest possible even number is " << result << "\n";
    }
    return 0;
}

Output

The largest possible even number is 123478

Time complexity – O(N*N), as we get all substrings of the given string.

Space complexity – O(N) to store the substring.

Approach 2

In this approach, we will traverse the string from the last and find the first occurrence of an even digit. We can take substring starting from the 0th index to the current digit as the last digit of the string will be even.

Algorithm

Step 1 – Initialize the ‘index’ variable with -1 to store the index of the first even digit from the last.

Step 2 – Traverse the string from the last.

Step 3 – If the current digit is divisible by 2, update the value of the ‘index’ variable with ‘I’ and break the loop.

Step 4 – If the value of the index is -1, return an empty string, as the string contains only odd digits. Otherwise, return the substring from the 0th index and length equal to ‘index + 1’.

Example

#include <bits/stdc++.h>
using namespace std;

string findStr(string str) {
    int len = str.length();
    int index = -1;
    // Traverse string from right to left
    for (int i = len - 1; i >= 0; i--) {
        // Getting the first even digit from the right
        if ((str[i] - '0') % 2 == 0) {
            index = i;
            break;
        }
    }
    if (index == -1)
        return "";
    else
        return str.substr(0, index + 1);
}
int main(){
    string str = "1234879";
    string result = findStr(str);
    if (result == "") {
        cout << "Not possible";
    } else {
        cout << "The largest possible even number is " << result << "\n";
    }
    return 0;
}

Output

The largest possible even number is 12348

Time complexity – O(N) to traverse string in reverse order or get a substring.

Space complexity – O(1) as we don’t use extra space.

The first approach is the naïve approach, and the second approach is an optimized one. In the second approach, we used the logic that the last digit of the even string is divisible by 2, and as we traverse from last, we can get the substring with maximum length.

Updated on: 25-Aug-2023

56 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements