Wednesday, October 8, 2025
HomeData Modelling & AICheck whether the given decoded string is divisible by 6

Check whether the given decoded string is divisible by 6

Given string str consisting of lowercase characters, the task is to check whether the string is divisible by 6 after changing it according to the given rules:  

  • ‘a’ gets changed to 1.
  • ‘b’ gets changed to 2
  • and similarly, ‘z’ gets changed to 26.

For example, the string “abz” will be changed to 1226.

Example: 

Input: str = “ab” 
Output: Yes 
“ab” is equivalent to 12 which is divisible by 6.

Input: str = “abc” 
Output: No 
123 is not divisible by 6. 

Approach: It can be solved by using a simple math trick that a number is divisible by 6 only if the sum of all of its digits is divisible by 3 and the last digit of the number is divisible by 2. Find the sum of the digits of the formed number and store it in a variable sum. Also, find the last digit of the number and store it in lastDigit
Now, if the sum is divisible by 3 and the lastDigit is divisible by 2 then print “Yes” else print “No”.

Below is the implementation of the above approach: 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the sum
// of the digits of n
int sumDigits(int n)
{
    int sum = 0;
    while (n > 0) {
        int digit = n % 10;
        sum += digit;
        n /= 10;
    }
    return sum;
}
 
// Function that return true if the
// decoded string is divisible by 6
bool isDivBySix(string str, int n)
{
    // To store the sum of the digits
    int sum = 0;
 
    // For each character, get the
    // sum of the digits
    for (int i = 0; i < n; i++) {
        sum += (int)(str[i] - 'a' + 1);
    }
 
    // If the sum of digits is
    // not divisible by 3
    if (sum % 3 != 0)
        return false;
 
    // Get the last digit of
    // the number formed
    int lastDigit = ((int)(str[n - 1]
                           - 'a' + 1))
                    % 10;
 
    // If the last digit is
    // not divisible by 2
    if (lastDigit % 2 != 0)
        return false;
    return true;
}
 
// Driver code
int main()
{
    string str = "ab";
    int n = str.length();
 
    if (isDivBySix(str, n))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java




// Java implementation of the approach
class GFG
{
 
// Function to return the sum
// of the digits of n
static int sumDigits(int n)
{
    int sum = 0;
    while (n > 0)
    {
        int digit = n % 10;
        sum += digit;
        n /= 10;
    }
    return sum;
}
 
// Function that return true if the
// decoded string is divisible by 6
static boolean isDivBySix(String str, int n)
{
    // To store the sum of the digits
    int sum = 0;
 
    // For each character, get the
    // sum of the digits
    for (int i = 0; i < n; i++)
    {
        sum += (int)(str.charAt(i) - 'a' + 1);
    }
 
    // If the sum of digits is
    // not divisible by 3
    if (sum % 3 != 0)
        return false;
 
    // Get the last digit of
    // the number formed
    int lastDigit = ((int)(str.charAt(n - 1) -
                                    'a' + 1)) % 10;
 
    // If the last digit is
    // not divisible by 2
    if (lastDigit % 2 != 0)
        return false;
    return true;
}
 
// Driver code
public static void main(String []args)
{
    String str = "ab";
    int n = str.length();
 
    if (isDivBySix(str, n))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
 
// This code is contributed by Rajput-Ji


Python3




# Python3 implementation of the approach
 
# Function to return the sum
# of the digits of n
def sumDigits(n) :
 
    sum = 0;
    while (n > 0) :
        digit = n % 10;
        sum += digit;
        n //= 10;
 
    return sum;
 
# Function that return true if the
# decoded string is divisible by 6
def isDivBySix(string , n) :
 
    # To store the sum of the digits
    sum = 0;
 
    # For each character, get the
    # sum of the digits
    for i in range(n) :
        sum += (ord(string[i]) -
                ord('a') + 1);
     
    # If the sum of digits is
    # not divisible by 3
    if (sum % 3 != 0) :
        return False;
 
    # Get the last digit of
    # the number formed
    lastDigit = (ord(string[n - 1]) -
                 ord('a') + 1) % 10;
 
    # If the last digit is
    # not divisible by 2
    if (lastDigit % 2 != 0) :
        return False;
    return True;
 
# Driver code
if __name__ == "__main__" :
 
    string = "ab";
    n = len(string);
 
    if (isDivBySix(string, n)) :
        print("Yes");
    else :
        print("No");
 
# This code is contributed by AnkitRai01


C#




// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to return the sum
// of the digits of n
static int sumDigits(int n)
{
    int sum = 0;
    while (n > 0)
    {
        int digit = n % 10;
        sum += digit;
        n /= 10;
    }
    return sum;
}
 
// Function that return true if the
// decoded string is divisible by 6
static bool isDivBySix(String str, int n)
{
    // To store the sum of the digits
    int sum = 0;
 
    // For each character, get the
    // sum of the digits
    for (int i = 0; i < n; i++)
    {
        sum += (int)(str[i] - 'a' + 1);
    }
 
    // If the sum of digits is
    // not divisible by 3
    if (sum % 3 != 0)
        return false;
 
    // Get the last digit of
    // the number formed
    int lastDigit = ((int)(str[n - 1] -
                             'a' + 1)) % 10;
 
    // If the last digit is
    // not divisible by 2
    if (lastDigit % 2 != 0)
        return false;
    return true;
}
 
// Driver code
public static void Main(String []args)
{
    String str = "ab";
    int n = str.Length;
 
    if (isDivBySix(str, n))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
 
// This code is contributed by 29AjayKumar


Javascript




<script>
 
// Javascript implementation of the approach
 
// Function to return the sum
// of the digits of n
function sumDigits(n)
{
    var sum = 0;
    while (n > 0) {
        var digit = n % 10;
        sum += digit;
        n = parseInt(n/10);
    }
    return sum;
}
 
// Function that return true if the
// decoded string is divisible by 6
function isDivBySix(str, n)
{
    // To store the sum of the digits
    var sum = 0;
 
    // For each character, get the
    // sum of the digits
    for (var i = 0; i < n; i++) {
        sum += (str[i].charCodeAt(0) - 'a'.charCodeAt(0) + 1);
    }
 
    // If the sum of digits is
    // not divisible by 3
    if (sum % 3 != 0)
        return false;
 
    // Get the last digit of
    // the number formed
    var lastDigit = ((str[n - 1].charCodeAt(0)
                           - 'a'.charCodeAt(0) + 1))
                    % 10;
 
    // If the last digit is
    // not divisible by 2
    if (lastDigit % 2 != 0)
        return false;
    return true;
}
 
// Driver code
var str = "ab";
var n = str.length;
if (isDivBySix(str, n))
    document.write( "Yes");
else
    document.write( "No");
 
</script>


Output

Yes

Time Complexity: O(N)

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32341 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6709 POSTS0 COMMENTS
Nicole Veronica
11875 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11937 POSTS0 COMMENTS
Shaida Kate Naidoo
6832 POSTS0 COMMENTS
Ted Musemwa
7092 POSTS0 COMMENTS
Thapelo Manthata
6785 POSTS0 COMMENTS
Umr Jansen
6787 POSTS0 COMMENTS