Sum of all numbers that can be formed with permutations of n digits
Given n distinct digits (from 0 to 9), find sum of all n digit numbers that can be formed using these digits. It is assumed that numbers formed with leading 0 are allowed.
Example:
Input: 1 2 3
Output: 1332
Explanation
Numbers Formed: 123 , 132 , 312 , 213, 231 , 321
123 + 132 + 312 + 213 + 231 + 321 = 1332
Total numbers that can be formed using n digits is total number of permutations of n digits, i.e factorial(n). Now, since the number formed is a n-digit number, each digit will appear factorial(n)/n times at each position from least significant digit to most significant digit. Therefore, sum of digits at a position = (sum of all the digits) * (factorial(n)/n).
Considering the example digits as 1 2 3
factorial(3)/3 = 2
Sum of digits at least significant digit = (1 + 2 + 3) * 2 = 12
Similarly sum of digits at tens, hundreds place is 12.
(This sum will contribute as 12 * 100)
Similarly sum of digits at tens, thousands place is 12.
(This sum will contribute as 12 * 1000)
Required sum of all numbers = 12 + (10 * 12) + (100 * 12) = 1332
Implementation:
C++
#include<stdio.h>
int factorial( int n)
{
int f = 1;
if (n==0||n==1)
return 1;
for ( int i=2; i<=n; i++)
f = f*i;
return f;
}
int getSum( int arr[], int n)
{
int fact = factorial(n);
int digitsum = 0;
for ( int i=0; i<n; i++)
digitsum += arr[i];
digitsum *= (fact/n);
int res = 0;
for ( int i=1, k=1; i<=n; i++)
{
res += (k*digitsum);
k = k*10;
}
return res;
}
int main()
{
int arr[] = {1, 2, 3};
int n = sizeof (arr)/ sizeof (arr[0]);
printf ( "%d" , getSum(arr, n));
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int factorial( int n)
{
int f = 1 ;
if (n == 0 || n == 1 )
return 1 ;
for ( int i = 2 ; i <= n; i++)
f = f * i;
return f;
}
static int getSum( int arr[], int n)
{
int fact = factorial(n);
int digitsum = 0 ;
for ( int i = 0 ; i < n; i++)
digitsum += arr[i];
digitsum *= (fact / n);
int res = 0 ;
for ( int i = 1 , k = 1 ;
i <= n; i++)
{
res += (k * digitsum);
k = k * 10 ;
}
return res;
}
public static void main (String[] args)
{
int arr[] = { 1 , 2 , 3 };
int n = arr.length;
System.out.println(getSum(arr, n));
}
}
|
Python3
def factorial(n):
f = 1
if (n = = 0 or n = = 1 ):
return 1
for i in range ( 2 , n + 1 ):
f = f * i
return f
def getSum(arr, n):
fact = factorial(n)
digitsum = 0
for i in range (n):
digitsum + = arr[i]
digitsum * = (fact / / n)
res = 0
i = 1
k = 1
while i < = n :
res + = (k * digitsum)
k = k * 10
i + = 1
return res
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 ]
n = len (arr)
print (getSum(arr, n))
|
C#
using System;
class GFG
{
static int factorial( int n)
{
int f = 1;
if (n == 0|| n == 1)
return 1;
for ( int i = 2; i <= n; i++)
f = f * i;
return f;
}
static int getSum( int []arr,
int n)
{
int fact = factorial(n);
int digitsum = 0;
for ( int i = 0; i < n; i++)
digitsum += arr[i];
digitsum *= (fact / n);
int res = 0;
for ( int i = 1, k = 1;
i <= n; i++)
{
res += (k * digitsum);
k = k * 10;
}
return res;
}
static public void Main ()
{
int []arr = {1, 2, 3};
int n = arr.Length;
Console.WriteLine(getSum(arr, n));
}
}
|
PHP
<?php
function factorial( $n )
{
$f = 1;
if ( $n == 0|| $n == 1)
return 1;
for ( $i = 2; $i <= $n ; $i ++)
$f = $f * $i ;
return $f ;
}
function getSum( $arr , $n )
{
$fact = factorial( $n );
$digitsum = 0;
for ( $i = 0; $i < $n ; $i ++)
$digitsum += $arr [ $i ];
$digitsum *= ( $fact / $n );
$res = 0;
for ( $i = 1, $k = 1; $i <= $n ; $i ++)
{
$res += ( $k * $digitsum );
$k = $k * 10;
}
return $res ;
}
$arr = array (1, 2, 3);
$n = sizeof( $arr );
echo getSum( $arr , $n );
?>
|
Javascript
<script>
function factorial(n)
{
let f = 1;
if (n == 0 || n == 1)
return 1;
for (let i = 2; i <= n; i++)
f = f * i;
return f;
}
function getSum(arr, n)
{
let fact = factorial(n);
let digitsum = 0;
for (let i = 0; i < n; i++)
digitsum += arr[i];
digitsum *= (fact / n);
let res = 0;
for (let i = 1, k = 1;
i <= n; i++)
{
res += (k * digitsum);
k = k * 10;
}
return res;
}
let arr = [1, 2, 3];
let n = arr.length;
document.write(getSum(arr, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
11 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...