Find permutation of n which is divisible by 3 but not divisible by 6
Last Updated :
05 Sep, 2022
Given an integer . The task is to find another integer which is permutation of n, divisible by 3 but not divisible by 6. Given that n is divisible by 6. If no such permutation is possible print -1.
Examples:
Input: n = 336
Output: 363
Input: n = 48
Output: -1
For a number to be divisible by 6, it must be divisible by 3 as well as 2, means every even integer divisible by 3 is divisible by 6. So, an integer which is divisible by 3 but not 6 is odd integer divisible by 3.
So, if integer n contains any odd integer then there exists a permutation which is divisible by 3 but not 6, else no such permutation exist.
Algorithm:
- let LEN is length of integer (i.e. ceil(log10(n))).
- iterate over LEN and check whether n is even or odd.
- if n is odd return n
- else right – rotate n once. and continue.
- if LEN is over return -1
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findPermutation( int n)
{
int len = ceil ( log10 (n));
for ( int i = 0; i < len; i++) {
if (n % 2 != 0) {
return n;
}
else {
n = (n / 10) + (n % 10) * pow (10, len - i - 1);
continue ;
}
}
return -1;
}
int main()
{
int n = 132;
cout << findPermutation(n);
return 0;
}
|
Java
import java.lang.*;
import java.util.*;
class GFG
{
static int findPermutation( int n)
{
int len = ( int )Math.ceil(Math.log10(n));
for ( int i = 0 ; i < len; i++)
{
if (n % 2 != 0 )
{
return n;
}
else
{
n = (n / 10 ) + (n % 10 ) *
( int )Math.pow( 10 , len - i - 1 );
continue ;
}
}
return - 1 ;
}
public static void main(String args[])
{
int n = 132 ;
System.out.println(findPermutation(n));
}
}
|
Python3
from math import log10, ceil, pow
def findPermutation(n):
len = ceil(log10(n))
for i in range ( 0 , len , 1 ):
if n % 2 ! = 0 :
return n
else :
n = ((n / 10 ) + (n % 10 ) *
pow ( 10 , len - i - 1 ))
continue
return - 1
if __name__ = = '__main__' :
n = 132
print ( int (findPermutation(n)))
|
C#
using System;
class GFG
{
static int findPermutation( int n)
{
int len = ( int )Math.Ceiling(Math.Log10(n));
for ( int i = 0; i < len; i++)
{
if (n % 2 != 0)
{
return n;
}
else
{
n = (n / 10) + (n % 10) *
( int )Math.Pow(10, len - i - 1);
continue ;
}
}
return -1;
}
public static void Main()
{
int n = 132;
Console.WriteLine(findPermutation(n));
}
}
|
PHP
<?php
function findPermutation( $n )
{
$len = ceil (log10( $n ));
for ( $i = 0; $i < $len ; $i ++)
{
if ( $n % 2 != 0)
{
return (int) $n ;
}
else
{
$n = ( $n / 10) + ( $n % 10) *
pow(10, $len - $i - 1);
continue ;
}
}
return -1;
}
$n = 132;
echo findPermutation( $n );
?>
|
Javascript
<script>
function findPermutation(n)
{
let len = Math.ceil(Math.log10(n));
for (let i = 0; i < len; i++)
{
if (n % 2 != 0)
{
return parseInt(n);
}
else
{
n = (n / 10) + (n % 10) *
Math.pow(10, len - i - 1);
continue ;
}
}
return -1;
}
let n = 132;
document.write( findPermutation(n));
</script>
|
Time complexity: O(logn) for given input number n
Auxiliary space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...