Maximum Product of First and Last Character of String after Rotation


In this problem, we will find the maximum possible product of the first and last digits of the string by rotating the string multiple times.

The naïve approach is that find all rotations of the string, take the product of the first and last character, and choose the maximum product as an answer.

Another approach is to find the product of every adjacent pair of characters and choose the maximum product.

Problem statement − We have given a string num containing the numeric digits. We need to find the maximum product of the first and last character of the given string after multiple left or right rotations.

Sample examples

Input

num = "9834546";

Output

72

Explanation − We can rotate the string by 1 left rotation. So, the string becomes 8345469, and if we take the product of the first and last characters, we get 72.

Input

num = "235467";

Output

42

Explanation − If we make 1 right rotation, the string becomes 723546. So, the first character is 7, and the last character is 6. The product of both is 42.

Input

num = "11278654";

Output

56

Explanation − The one rotation of the given string is 78654112. So, the product of the first and last characters is 56.

Approach 1

In this approach, we will make the left rotation of the string for the total N number of times. After that, we will take the product of the first and last characters and track the maximum product for each rotation.

Algorithm

Step 1 − Initialize the ‘len’ variable with the string length, the ‘first’ variable with the first character, and the ‘last’ variable with the last character of the original string. Also, initialize the ‘maximum’ with the product of the first and last characters.

Step 2 − Start traversing the ‘num’ string.

Step 3 − Rotate the substring to the left using the substr() method.

Step 4 − Take the first and last digit value of the current rotation.

Step 5 − Update the ‘maximum’ value if the product of the first and last character is greater than the value of the 'maximum’.

Step 6 − Return the ‘maximum’ value.

Example

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

int getMaxProduct(string num) {
    int len = num.size();
    int first = num[0] - '0';
    int last = num[len - 1] - '0';
    // Get the product of the first and last character of the original string
    int maximum = first * last;
    // Rotate string by p rotations
    for (int p = 0; p < num.size() - 1; p++) {
        // Make left rotation by 1 unit using the substr() method
        num = num.substr(1) + num[0];
        first = num[0] - '0';
        last = num[len - 1] - '0';
        maximum = max(maximum, first * last);
    }
    return maximum;
}
int main() {
    string num = "9834546";
    cout << "The maximum product of the first and last character after rotation is - " << getMaxProduct(num);
    return 0;
}

Output

The maximum product of the first and last character after rotation is − 72

Time complexity − O(N*N) to make total N rotations of the given string.

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

Approach 2

In this approach, we will take the maximum product of the adjacent digits of the given string.

Here, the logic is when we rotate the string by the left, the end of the string, and the next character goes at the first position of the string. So, it is enough to find the maximum product of adjacent characters.

Algorithm

Step 1 − Initialize the ‘len’, ‘first’, ‘second’ and ‘maximum’ with respected values.

Step 2 − Start traversing the string from the 2nd position.

Step 3 − Take the current character in the ‘first’ variable and the previous character in the ‘last’ variable.

Step 4 − Update the value of the ‘maximum’ variable if the first and last product is greater than the ‘maximum’ value.

Step 5 − Return the ‘maximum’ variable’s value at the end.

Example

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

int getMaxProduct(string num) {
    int len = num.size();
    int first = num[0] - '0';
    int last = num[len - 1] - '0';
    // Get the product of the first and last character of the original string
    int maximum = first * last;
    for (int p = 1; p < len; p++) {
        first = num[p] - '0';
        last = num[p - 1] - '0';
        // Get the product of first and last after rotation by p.
        maximum = max(maximum, first * last);
    }
    return maximum;
}
int main() {
    string num = "9834546";
    cout << "The maximum product of the first and last character after rotation is " << getMaxProduct(num);
    return 0;
}

Output

The maximum product of the first and last character after rotation is 72

Time complexity − O(N) to take the product of all adjacent characters.

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

The second approach is more efficient in terms of the time and space complexity. Sometimes, we require to observe the problem solution and need to write the code based on the observation to optimize the code further.

Updated on: 17-Jul-2023

36 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements