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++
// 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[c]);
for (int c = 0; c < n.length(); c++)
{
N = N + (N * 10 + (n[c]));
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.
Java
/*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
Python3
# 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#
// 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[c]);
}
for (int c = 0; c < n.Length; c++) {
N = N
+ (N * 10
+ (int)(Char.GetNumericValue(
n[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
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.
JavaScript
<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[c]);
for (var c = 0; c < n.length; c++)
{
N = N + (N * 10 + (n[c]));
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>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Check if concatenation of first and last digits forms a prime number or not for each array element Given an array Q[] consisting of N integers, the task for each element of the array Q[] is to check whether any of the numbers, formed by concatenating the first and the last digits of Q[i] is a prime number or not. Examples: Input: Q[] = {30, 66}Output: TrueFalseExplanation:Q[0]: Possible combinati
9 min read
Check if a number is formed by Concatenation of 1, 14 or 144 only Given a number N . The task is to check if the number is formed by concatenating the numbers 1, 14 and 144 only any number of times and in any order.If it is possible, print YES otherwise print NO.Example: Input: N = 141411 Output: YES Input: N = 14134 Output: NO The idea is to fetch single digit, d
5 min read
Check if the number formed by the last digits of N numbers is divisible by 10 or not Given an array arr[] of size N consisting of non-zero positive integers. The task is to determine whether the number that is formed by selecting the last digits of all the numbers is divisible by 10 or not. If the number is divisible by 10, then print Yes otherwise print No.Examples: Input: arr[] =
4 min read
Check if an array element is concatenation of two elements from another array Given two arrays arr[] and brr[] consisting of N and M positive integers respectively, the task is to find all the elements from the array brr[] which are equal to the concatenation of any two elements from the array arr[]. If no such element exists, then print "-1". Examples: Input: arr[] = {2, 34,
8 min read
Check whether a binary string can be formed by concatenating given N numbers sequentially Given a sequence of 'n' numbers (without leading zeros), the task is to find whether it is possible to create a binary string by concatenating these numbers sequentially. If possible, then print the binary string formed, otherwise print "-1". Examples : Input: arr[] = {10, 11, 1, 0, 10} Output: 1011
8 min read