Given an integer **N**, the task is to check if the concatenation of first **N** natural numbers is *divisible* by 3 or not. Print * Yes* if divisible and

*if not.*

**No****Examples**:

Input: N = 3Output:YesExplanation:

The concatenated number = 123

Since it is divisible by 3, the output is YesInput: N = 7Output:NoExplanation:The concatenated number = 1234567

Since it is not divisible by 3, the output is No.

**Naive Approach**:

The simplest approach is to concatenate the first** N **natural numbers and calculate the sum of digits of the resultant number and check if it is divisible by 3 or not. **Time Complexity:** O(N) **Auxiliary Space: **O(1)**Efficient Approach:**

To optimize the above approach, we can observe a pattern. The concatenation of first **N** natural numbers is not divisible by 3 for the following series *1, 4, 7, 10, 13, 16, 19*, *and so on. *The **N ^{th}**

**term**of the series is given by the formula

**3×n +1**

*Hence, if*

**.****(N – 1)**is not divisible by

**3**, then the resultant number is divisible by 3, so print

*Yes*. Otherwise, print

*No.*

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// Function that returns True if ` `// concatenation of first N natural ` `// numbers is divisible by 3 ` `bool` `isDivisible(` `int` `N) ` `{ ` ` ` `// Check using the formula ` ` ` `return` `(N - 1) % 3 != 0; ` `} ` ` ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given Number ` ` ` `int` `N = 6; ` ` ` ` ` `// Function Call ` ` ` `if` `(isDivisible(N)) ` ` ` `cout << (` `"Yes"` `); ` ` ` ` ` `else` ` ` `cout << (` `"No"` `); ` ` ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `class` `GFG{ ` ` ` `// Function that returns True if ` `// concatenation of first N natural ` `// numbers is divisible by 3 ` `static` `boolean` `isDivisible(` `int` `N) ` `{ ` ` ` `// Check using the formula ` ` ` `return` `(N - ` `1` `) % ` `3` `!= ` `0` `; ` `} ` ` ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `// Given Number ` ` ` `int` `N = ` `6` `; ` ` ` ` ` `// Function Call ` ` ` `if` `(isDivisible(N)) ` ` ` `System.out.println(` `"Yes"` `); ` ` ` ` ` `else` ` ` `System.out.println(` `"No"` `); ` `} ` `} ` ` ` `// This code is contributed by Ritik Bansal` |

*chevron_right*

*filter_none*

## Python 3

`# Python program for the above approach ` ` ` `# Function that returns True if ` `# concatenation of first N natural ` `# numbers is divisible by 3 ` `def` `isDivisible(N): ` ` ` ` ` `# Check using the formula ` ` ` `return` `(N ` `-` `1` `) ` `%` `3` `!` `=` `0` ` ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `# Given Number ` ` ` `N ` `=` `6` ` ` ` ` `# Function Call ` ` ` `if` `(isDivisible(N)): ` ` ` `print` `(` `"Yes"` `) ` ` ` ` ` `else` `: ` ` ` `print` `(` `"No"` `) ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` `class` `GFG{ ` ` ` `// Function that returns True if ` `// concatenation of first N natural ` `// numbers is divisible by 3 ` `static` `bool` `isDivisible(` `int` `N) ` `{ ` ` ` `// Check using the formula ` ` ` `return` `(N – 1) % 3 != 0; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `// Given Number ` ` ` `int` `N = 6; ` ` ` ` ` `// Function Call ` ` ` `if` `(isDivisible(N)) ` ` ` `Console.Write(` `"Yes"` `); ` ` ` ` ` `else` ` ` `Console.Write(` `"No"` `); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

**Output:**

Yes

**Time Complexity: **O(1) **Auxiliary Space: **O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Check whether factorial of N is divisible by sum of first N natural numbers
- Check if factorial of N is divisible by the sum of squares of first N natural numbers
- Check if product of first N natural numbers is divisible by their sum
- Sum of first N natural numbers which are divisible by 2 and 7
- Sum of first N natural numbers which are divisible by X or Y
- Number of pairs from the first N natural numbers whose sum is divisible by K
- Find the first natural number whose factorial is divisible by x
- Subarray of length K having concatenation of its elements divisible by X
- Check if a given number can be expressed as pair-sum of sum of first X natural numbers
- Check if permutation of first N natural numbers exists having Bitwise AND of adjacent elements non-zero
- Count of digits after concatenation of first N positive integers
- Count natural numbers whose factorials are divisible by x but not y
- Print squares of first n natural numbers without using *, / and -
- Print all increasing sequences of length k from first n natural numbers
- Sum of cubes of first n odd natural numbers
- Find m-th summation of first n natural numbers.
- Program to find sum of first n natural numbers
- Program for cube sum of first n natural numbers
- Sum of all subsets of a set formed by first n natural numbers
- Sum of squares of first n natural numbers

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.