Check if the concatenation of first N natural numbers is divisible by 3
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 No if not.
Examples:
Input: N = 3
Output: Yes
Explanation:
The concatenated number = 123
Since it is divisible by 3, the output is Yes
Input: N = 7
Output: No
Explanation: The concatenated number = 1234567
Since it is not divisible by 3, the output is No.
Brute Force Approach:
A brute force approach to solve this problem would be to generate the concatenation of the first N natural numbers and then check if it is divisible by 3 or not. We can use a string to store the concatenation and then convert it into an integer for checking the divisibility.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isDivisible( int N)
{
string concat = "" ;
for ( int i = 1; i <= N; i++){
concat += to_string(i);
}
int num = stoi(concat);
return (num % 3 == 0);
}
int main()
{
int N = 6;
if (isDivisible(N))
cout << ( "Yes" );
else
cout << ( "No" );
return 0;
}
|
Java
import java.util.*;
public class Main {
public static boolean isDivisible( int N) {
String concat = "" ;
for ( int i = 1 ; i <= N; i++) {
concat += Integer.toString(i);
}
int num = Integer.parseInt(concat);
return (num % 3 == 0 );
}
public static void main(String[] args) {
int N = 6 ;
if (isDivisible(N)) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
}
}
|
Python3
def isDivisible(n : int ) - > bool :
concat = ""
for i in range ( 1 ,n + 1 ):
concat + = str (i)
num = int (concat)
return num % 3 = = 0
n = 6
res = isDivisible(n)
if res = = False :
print ( "No" )
else :
print ( "Yes" )
|
C#
using System;
public class MainClass
{
public static bool isDivisible( int N)
{
string concat = "" ;
for ( int i = 1; i <= N; i++){
concat += i.ToString();
}
int num = int .Parse(concat);
return (num % 3 == 0);
}
public static void Main()
{
int N = 6;
if (isDivisible(N))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
function isDivisible(N) {
let concat = "" ;
for (let i = 1; i <= N; i++) {
concat += i.toString();
}
let num = parseInt(concat);
return (num % 3 === 0);
}
let N = 6;
if (isDivisible(N))
console.log( "Yes" );
else
console.log( "No" );
|
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 Nth 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++
#include <bits/stdc++.h>
using namespace std;
bool isDivisible( int N)
{
return (N - 1) % 3 != 0;
}
int main()
{
int N = 6;
if (isDivisible(N))
cout << ( "Yes" );
else
cout << ( "No" );
return 0;
}
|
Java
class GFG{
static boolean isDivisible( int N)
{
return (N - 1 ) % 3 != 0 ;
}
public static void main(String[] args)
{
int N = 6 ;
if (isDivisible(N))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python 3
def isDivisible(N):
return (N - 1 ) % 3 ! = 0
if __name__ = = "__main__" :
N = 6
if (isDivisible(N)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool isDivisible( int N)
{
return (N – 1) % 3 != 0;
}
public static void Main()
{
int N = 6;
if (isDivisible(N))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function isDivisible(N)
{
return (N - 1) % 3 != 0;
}
var N = 6;
if (isDivisible(N))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
06 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...