700355. Sum of permutations

Medium

Here is the detailed Markdown blog post for the "Sum of permutations" problem:

Sum of permutations

Summary

The problem is to calculate the sum of all possible permutations of a given array of integers. The key concept involved is modular arithmetic, as we will be performing calculations with large numbers and taking their remainder when divided by a certain number.

Detailed Explanation

Given an array arr of size n, our task is to find the sum of all possible permutations of this array modulo some prime number p. This problem can be solved using dynamic programming. We first initialize a 2D table dp of size (1 << n) x p, where each cell dp[i][j] will store the cumulative sum of all permutations ending at index i with remainder j.

Here's a step-by-step breakdown of the solution:

  1. Initialize the table dp with zeros.
  2. Iterate over the array arr. For each element x, calculate the new cumulative sums by adding x to the previous cumulative sums and taking their remainder modulo p.
  3. Update the table dp accordingly.
  4. Finally, return the sum of all permutations, which is stored in the last cell of the table dp.

Here's a diagram illustrating this approach:

     +---------------+
     |  arr = [1, 2, 3]  |
     +---------------+
                  |
                  v
     +---------------+
     |  dp[0][0] = 0   |
     |  dp[0][1] = 1   |
     |  ...            |
     +---------------+
                  |
                  v
     +---------------+
     |  dp[1][0] = 1   |
     |  dp[1][1] = 2   |
     |  ...            |
     +---------------+
                  |
                  v
     +---------------+
     |  dp[2][0] = 3   |
     |  dp[2][1] = 4   |
     |  ...            |
     +---------------+
                  |
                  v
     +---------------+
     |  Return sum of all  |
     |  permutations: dp[(1 << n) - 1][p-1]  |
     +---------------+

The time complexity of this solution is O(n * p), where n is the size of the array and p is the prime number. The space complexity is O(p).

Optimized Solutions

Java

View on GeeksforGeeks
public int sumOfPermutations(int[] arr, int p) {
    int n = arr.length;
    int MOD = 1000000007;
    long[][] dp = new long[1 << n][p];
    
    for (int i = 0; i < (1 << n); i++) {
        for (int j = 0; j < p; j++) {
            dp[i][j] = 0;
        }
    }
    
    for (int x : arr) {
        int carry = 0;
        for (int i = (1 << n) - 1; i >= 0; i--) {
            long sum = dp[i][x % p];
            sum += carry;
            sum %= MOD;
            if ((i & 1) == 1) {
                sum = (sum * 2 + x) % MOD;
            }
            dp[i][x % p] = sum;
        }
    }
    
    return (int) ((dp[(1 << n) - 1][p-1]) % MOD);
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.