Check whether the given decoded string is divisible by 6
Last Updated :
18 Nov, 2021
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++
#include <bits/stdc++.h>
using namespace std;
int sumDigits( int n)
{
int sum = 0;
while (n > 0) {
int digit = n % 10;
sum += digit;
n /= 10;
}
return sum;
}
bool isDivBySix(string str, int n)
{
int sum = 0;
for ( int i = 0; i < n; i++) {
sum += ( int )(str[i] - 'a' + 1);
}
if (sum % 3 != 0)
return false ;
int lastDigit = (( int )(str[n - 1]
- 'a' + 1))
% 10;
if (lastDigit % 2 != 0)
return false ;
return true ;
}
int main()
{
string str = "ab" ;
int n = str.length();
if (isDivBySix(str, n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG
{
static int sumDigits( int n)
{
int sum = 0 ;
while (n > 0 )
{
int digit = n % 10 ;
sum += digit;
n /= 10 ;
}
return sum;
}
static boolean isDivBySix(String str, int n)
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
{
sum += ( int )(str.charAt(i) - 'a' + 1 );
}
if (sum % 3 != 0 )
return false ;
int lastDigit = (( int )(str.charAt(n - 1 ) -
'a' + 1 )) % 10 ;
if (lastDigit % 2 != 0 )
return false ;
return true ;
}
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" );
}
}
|
Python3
def sumDigits(n) :
sum = 0 ;
while (n > 0 ) :
digit = n % 10 ;
sum + = digit;
n / / = 10 ;
return sum ;
def isDivBySix(string , n) :
sum = 0 ;
for i in range (n) :
sum + = ( ord (string[i]) -
ord ( 'a' ) + 1 );
if ( sum % 3 ! = 0 ) :
return False ;
lastDigit = ( ord (string[n - 1 ]) -
ord ( 'a' ) + 1 ) % 10 ;
if (lastDigit % 2 ! = 0 ) :
return False ;
return True ;
if __name__ = = "__main__" :
string = "ab" ;
n = len (string);
if (isDivBySix(string, n)) :
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG
{
static int sumDigits( int n)
{
int sum = 0;
while (n > 0)
{
int digit = n % 10;
sum += digit;
n /= 10;
}
return sum;
}
static bool isDivBySix(String str, int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
{
sum += ( int )(str[i] - 'a' + 1);
}
if (sum % 3 != 0)
return false ;
int lastDigit = (( int )(str[n - 1] -
'a' + 1)) % 10;
if (lastDigit % 2 != 0)
return false ;
return true ;
}
public static void Main(String []args)
{
String str = "ab" ;
int n = str.Length;
if (isDivBySix(str, n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function sumDigits(n)
{
var sum = 0;
while (n > 0) {
var digit = n % 10;
sum += digit;
n = parseInt(n/10);
}
return sum;
}
function isDivBySix(str, n)
{
var sum = 0;
for ( var i = 0; i < n; i++) {
sum += (str[i].charCodeAt(0) - 'a' .charCodeAt(0) + 1);
}
if (sum % 3 != 0)
return false ;
var lastDigit = ((str[n - 1].charCodeAt(0)
- 'a' .charCodeAt(0) + 1))
% 10;
if (lastDigit % 2 != 0)
return false ;
return true ;
}
var str = "ab" ;
var n = str.length;
if (isDivBySix(str, n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...