Sum of permutations | Practice | GeeksforGeeks

Given N distinct digits in an array A (from 1 to 9), Your task is to complete the function getSum which finds sum of all n digit numbers that can be formed using these digits. 

Note: Since the output can be large take modulo 1000000007

Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. The first line of each test case contains an integer N . In the next line are N space separated digits.

Output:
For each test case in a new line output will be the sum of all n digit numbers that can be formed using these digits.

Constraints:
1<=T<=100
1<=N<=9

Example(To be used only for expected output):
Input:
2
3
1 2 3
2
1 2
Output:
1332
33

Explanation:
For first test case
the numbers formed will be 123 , 132 , 312 , 213, 231 , 321
sum = 123 + 132 + 312 + 213 + 231 + 321 = 1332

For second test case
the numbers formed will be 12, 21
sum = 12 + 21 = 33
0
Rishabh Verma9 months ago
Rishabh Verma
 string str;   for(int i = 0;i < n;i++){       str+ = A[i]+'0';   }   long long ans = 0;   sort(str.begin(),str.end());   do{       ans = (ans+stoi(str))%1000000007;   }while(next_permutation(str.begin(),str.end()));   return ans;
0
Pranjal Jain11 months ago
Pranjal Jain
Java Solution :Correct answerExecution time:0.2
long mod = 1000000007;public long fact( long n){    long ans = 1;    for(int i=1; i<=n; i++)    {        ans = (ans*i)%mod;    }    return ans;}public long getSum(int A[],int n){     long ans = 0;    for(int i=0; i<n; i++)="" {="" for(int="" j="0;" j<n;="" j++)="" {="" ans="(ans+A[i]*(int)Math.pow(10,j)*fact(n-1))%mod;" }="" }="" return="" ans;="" }="">
0
Debojyoti Sinha1 year ago
Debojyoti Sinha
Correct Answer.Correct AnswerExecution Time:0.01
long long getSum(int arr[],int N){    string str;        for(int i = 0; i < N; i++)    {        str += arr[i] + '0';    }        sort(str.begin(), str.end());        long long res = 0;        do    {        res = (res + stoi(str)) % 1000000007;    }    while(next_permutation(str.begin(), str.end()));        return res;}
0
Deyyala Srinivas Kumar1 year ago
Deyyala Srinivas Kumar
0.01secslong long getSum(int A[],int n){    //Your code here    string str="";    for(int i=0;i<n;i++) {="" str="str+to_string(A[i]);" }="" sort(str.begin(),str.end());="" long="" long="" sum="0;" do{="" long="" long="" int="" d="0;" for(int="" i="0;i&lt;str.length();i++)" {="" d="d*10+(str[i]-'0');" }="" sum="(sum+d)%1000000007;" }while(next_permutation(str.begin(),str.end()));="" return="" sum;="" }<="" spoiler="">
0
Amit kumar Gupta1 year ago
Amit kumar Gupta
SIMPLEST SOLUTION EVEN.....HAPPY CODING...!!
long long getSum(int arr[],int n){    long long int res=0;    long long int mod=pow(10,9)+7;    sort(arr,arr+n);    do    {       long long int sum=0;       for(int i=0;i<n;i++) {="" sum="sum*10+arr[i];" }="" res="res+(sum%mod);" cout<<res<<endl;="" }while(next_permutation(arr,arr+n));="" return="" (res%mod);="" }="">
0
Debjyoti Chattopadhyay2 years ago
Debjyoti Chattopadhyay
https://ide.geeksforgeeks.o...Java O(N) soln
0
Debi Prasad Sen2 years ago
Debi Prasad Sen
Simple Python 3, Solution using permutations() from itertoolsEx Time=0.05secshttps://ide.geeksforgeeks.o...
0
pratik2 years ago
pratik
there is no need to iterate over every permutation, give a look at it.https://ide.geeksforgeeks.o...
0
gaurav raj2 years ago
gaurav raj
long long getSum(int a[],int n){    long long int mod=1000000007,sum=0;    sort(a,a+n);    int i;    string s;    for(i=0;i<n;i++) {="" s+="to_string(a[i]);" }="" sum="stoi(s)%mod;" do="" {="" sum+="stoi(s)%mod;" }while(next_permutation(s.begin(),s.end()));="" return="" sum%mod;="" }<="" spoiler="">
0
Shivam Kumar Jha2 years ago
Shivam Kumar Jha
using c++long long getSum(int A[],int n){    //Your code here    sort(A,A+n);    long long int md = pow(10,9)+7;    long long int sum1 = 0;    do{        long long int sum = 0;        for(int i = 0; i<n; i++)="" {="" sum="sum*10+A[i];" }="" sum1+="sum%md;" }while(next_permutation(a,a+n));="" return="" (sum1%md);="" }="">
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.