- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find permutation of n which is divisible by 3 but not divisible by 6 in C++

Suppose we have a number n, and we have to find the permutation of this number, that is divisible by 3, but not divisible by 6. If no such value can be made, then return -1. For example, if n is 336, then the output can be 363.

As we know a number is divisible by 6 means it is divisible by 3 and 2. So each even number that is divisible by 3, will be divisible by 6. If we interchange the digits of a number which is divisible by 3 and also even, to make it odd, it will be the result.

## Example

#include<iostream> #include<cmath> using namespace std; int findNumber(int n) { int digit_count = ceil(log10(n)); for (int i = 0; i < digit_count; i++) { if (n % 2 != 0) { return n; } else { n = (n / 10) + (n % 10) * pow(10, digit_count - i - 1); continue; } } return -1; } int main() { int n = 132; cout <<"The permutation of "<<n << " that is divisible by 3 but not by 6 is:"<< findNumber(n); }

## Output

The permutation of 132 that is divisible by 3 but not by 6 is:213

- Related Questions & Answers
- Sum which is divisible by n in JavaScript
- Find N digits number which is divisible by D in C++
- Count numbers in range 1 to N which are divisible by X but not by Y in C++
- Number of substrings divisible by 8 and not by 3 in C++
- Check if any permutation of a number is divisible by 3 and is Palindromic in Python
- Find elements of an array which are divisible by N using STL in C++
- Check if a large number is divisible by 3 or not in java
- Check if a large number is divisible by 3 or not in C++
- Number is divisible by 29 or not in C++
- Sum of first N natural numbers which are divisible by X or Y
- Number of Substrings divisible by 6 in a String of Integers in C++
- Rearrangement of a number which is also divisible by it in C++
- Check if any permutation of a large number is divisible by 8 in Python
- Check if a large number is divisible by 2, 3 and 5 or not in C++
- Generating a random number that is divisible by n in JavaScript

Advertisements