Given an array arr[] consisting of N integers, the task is to check if the number formed by concatenating all the array elements is a Harshad number or not.
Examples:
Input: arr[] = { 1, 35, 69, 60}
Output: Yes
Explanation:
The number formed by concatenating array elements is “1356960”.
Sum of digits of the number = 1 + 3 + 5 + 6 + 9 + 6 + 0 = 30.
Since, the number is divisible by sum of its digits, the number is a “Harshad number”.Input: arr[] = {1, 563, 9, 59, 7, 8}
Output: Yes
Approach: The idea is to convert all array elements to their equivalent strings and concatenate those strings. Follow the steps below to solve the problem:
- Traverse the array arr[] and convert each array element to its equivalent string.
- Concatenate all the strings in a variable, say S.
- Initialize a variable, say sum, to store the sum of digits of the generated number.
- Traverse the string S and update sum as sum += int (s[i]).
- Initialize a variable, say N = 0, to store the number formed by joining all the characters of the string S mod sum.
- Traverse the string S and update N as N = (N * 10 + int (S[i])) % sum.
- Print Yes, if N = 0.
- Otherwise, print No.
Below is the implementation of the above approach:
// CPP implementation // of above approach #include<bits/stdc++.h> using namespace std;
// Function to check whether n // is a harshad number or not int checkHarshad(string n)
{ // Stores the sum of digits
int sum = 0;
// Stores the remainder
int N = 0;
// Increment sum
for ( int c = 0; c < n.length(); c++)
sum += (n);
for ( int c = 0; c < n.length(); c++)
{
N = N + (N * 10 + (n));
N %= sum;
}
return (N != 0);
} // Function to check if concatenation // of elements from the array arr[] // is a harshad number or not bool combineArray(vector< int > lis)
{ // Stores the concatenated number
string st = "" ;
// Traverse the array
for ( auto el: lis)
{
// Concatenate the string
st += to_string(el);
}
if (checkHarshad(st))
return true ;
else
return false ;
} // Driver Code int main()
{ // Input
vector< int >arr{1, 35, 69, 60};
// Function call to check if
// concatenation of elements of
// arr[] is a harshad number
if (combineArray(arr))
cout << "Yes" ;
else
cout << "No" ;
} // This code is contributed by ipg2016107. |
/*package whatever //do not write package name here */ import java.io.*;
import java.util.ArrayList;
class GFG {
// Function to check whether n
// is a harshad number or not
public static boolean checkHarshad(String n)
{
// Stores the sum of digits
int sum = 0 ;
// Stores the remainder
int N = 0 ;
// Increment sum
for ( int c = 0 ; c < n.length(); c++) {
sum += Character.getNumericValue(n.charAt(c));
}
for ( int c = 0 ; c < n.length(); c++) {
N = N
+ (N * 10
+ (Character.getNumericValue(
n.charAt(c))));
N %= sum;
}
if (N == 0 ) {
return true ;
}
return false ;
}
// Function to check if concatenation
// of elements from the array arr[]
// is a harshad number or not
public static boolean
combineArray(ArrayList<Integer> list)
{
// Stores the concatenated number
String st = "" ;
// Traverse the array
for ( int i = 0 ; i < list.size(); i++)
{
// Concatenate the string
st += Integer.toString(list.get(i));
}
if (checkHarshad(st)) {
return true ;
}
return false ;
}
// Driver Code
public static void main(String[] args)
{
// Input
ArrayList<Integer> list = new ArrayList<>();
list.add( 1 );
list.add( 35 );
list.add( 69 );
list.add( 60 );
// Function call to check if
// concatenation of elements of
// arr[] is a harshad number
if (combineArray(list))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
} // This code is contributed by aditya7409 |
# Python implementation # of above approach # Function to check whether n # is a harshad number or not def checkHarshad(n):
# Stores the sum of digits
sum = 0
# Stores the remainder
N = 0
# Increment sum
for c in n:
sum + = int (c)
for c in n:
N = N + (N * 10 + int (c))
N % = sum
return N = = 0
# Function to check if concatenation # of elements from the array arr[] # is a harshad number or not def combineArray(lis):
# Stores the concatenated number
string = ""
# Traverse the array
for el in lis:
# Convert to equivalent string
el = str (el)
# Concatenate the string
string = string + el
if (checkHarshad(string)):
return True
else :
return False
# Driver Code # Input arr = [ 1 , 35 , 69 , 60 ]
# Function call to check if # concatenation of elements of # arr[] is a harshad number if (combineArray(arr)):
print ( "Yes" )
else :
print ( "No" )
|
// C# program for the above approach using System;
using System.Collections.Generic;
class GFG
{ // Function to check whether n
// is a harshad number or not
public static bool checkHarshad( string n)
{
// Stores the sum of digits
int sum = 0;
// Stores the remainder
int N = 0;
// Increment sum
for ( int c = 0; c < n.Length; c++) {
sum += ( int )Char.GetNumericValue(n);
}
for ( int c = 0; c < n.Length; c++) {
N = N
+ (N * 10
+ ( int )(Char.GetNumericValue(
n)));
N %= sum;
}
if (N == 0) {
return true ;
}
return false ;
}
// Function to check if concatenation
// of elements from the array arr[]
// is a harshad number or not
static bool
combineArray(List< int > list)
{
// Stores the concatenated number
string st = "" ;
st += string .Join( "" , list);
if (checkHarshad(st)) {
return true ;
}
return false ;
}
// Driver code static void Main()
{ List< int > list = new List< int >();
list.Add(1);
list.Add(35);
list.Add(69);
list.Add(60);
// Function call to check if
// concatenation of elements of
// arr[] is a harshad number
if (combineArray(list))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
} } // This code is contributed susmitakundugoaldanga. |
<script> // Javascript implementation // of above approach // Function to check whether n // is a harshad number or not function checkHarshad(n)
{ // Stores the sum of digits
var sum = 0;
// Stores the remainder
var N = 0;
// Increment sum
for ( var c = 0; c < n.length; c++)
sum += (n);
for ( var c = 0; c < n.length; c++)
{
N = N + (N * 10 + (n));
N %= sum;
}
return (N != 0);
} // Function to check if concatenation // of elements from the array arr[] // is a harshad number or not function combineArray(lis)
{ // Stores the concatenated number
var st = "" ;
// Traverse the array
for ( var el in lis)
{
// Concatenate the string
st += (el.toString());
}
if (checkHarshad(st))
return true ;
else
return false ;
} // Driver Code // Input var arr = [1, 35, 69, 60];
// Function call to check if // concatenation of elements of // arr[] is a harshad number if (combineArray(arr))
document.write( "Yes" );
else document.write( "No" );
</script> |
Yes
Time Complexity: O(N)
Auxiliary Space: O(N)