Check if the number formed by concatenating all array elements is a Harshad number or not
Last Updated :
01 Jul, 2021
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:
C++
#include<bits/stdc++.h>
using namespace std;
int checkHarshad(string n)
{
int sum = 0;
int N = 0;
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);
}
bool combineArray(vector< int > lis)
{
string st = "" ;
for ( auto el: lis)
{
st += to_string(el);
}
if (checkHarshad(st))
return true ;
else
return false ;
}
int main()
{
vector< int >arr{1, 35, 69, 60};
if (combineArray(arr))
cout << "Yes" ;
else
cout << "No" ;
}
|
Java
import java.io.*;
import java.util.ArrayList;
class GFG {
public static boolean checkHarshad(String n)
{
int sum = 0 ;
int N = 0 ;
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 ;
}
public static boolean
combineArray(ArrayList<Integer> list)
{
String st = "" ;
for ( int i = 0 ; i < list.size(); i++)
{
st += Integer.toString(list.get(i));
}
if (checkHarshad(st)) {
return true ;
}
return false ;
}
public static void main(String[] args)
{
ArrayList<Integer> list = new ArrayList<>();
list.add( 1 );
list.add( 35 );
list.add( 69 );
list.add( 60 );
if (combineArray(list))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def checkHarshad(n):
sum = 0
N = 0
for c in n:
sum + = int (c)
for c in n:
N = N + (N * 10 + int (c))
N % = sum
return N = = 0
def combineArray(lis):
string = ""
for el in lis:
el = str (el)
string = string + el
if (checkHarshad(string)):
return True
else :
return False
arr = [ 1 , 35 , 69 , 60 ]
if (combineArray(arr)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static bool checkHarshad( string n)
{
int sum = 0;
int N = 0;
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 ;
}
static bool
combineArray(List< int > list)
{
string st = "" ;
st += string .Join( "" , list);
if (checkHarshad(st)) {
return true ;
}
return false ;
}
static void Main()
{
List< int > list = new List< int >();
list.Add(1);
list.Add(35);
list.Add(69);
list.Add(60);
if (combineArray(list))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function checkHarshad(n)
{
var sum = 0;
var N = 0;
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 combineArray(lis)
{
var st = "" ;
for ( var el in lis)
{
st += (el.toString());
}
if (checkHarshad(st))
return true ;
else
return false ;
}
var arr = [1, 35, 69, 60];
if (combineArray(arr))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...