Reddit Experience · 2026 Q1

POTD Day 2 - I had to look at the editorialđŸ˜­

6 upvotes 4 replies

Interview Experience

[https://leetcode.com/problems/construct-product-matrix/description/?envType=daily-question&envId=2026-03-24](https://leetcode.com/problems/construct-product-matrix/description/?envType=daily-ques

Full Details

[https://leetcode.com/problems/construct-product-matrix/description/?envType=daily-question&envId=2026-03-24](https://leetcode.com/problems/construct-product-matrix/description/?envType=daily-question&envId=2026-03-24) I need to look at the number theory approach (MMI doesn't work, because the number 12345 is funny and for inverse we need to make sure that the inverse and modulo have GCD = 1). I was initially storing the whole array as prefix/suffix did not come to mind, but after seeing the editorial, here is that solution. Also, can anyone comment whether this prefix/suffix will work on even tougher constraints? (I think not) class Solution { public: int N = 12345; vector<vector<int>> constructProductMatrix(vector<vector<int>>& grid) { long long x = 1; int m = grid.size(); int n = grid[0].size(); vector<vector<long long>>prefix(m,vector<long long>(n,1)); for (int i = 0; i < m; i++){ for (int j = 0; j < n; j++){ prefix[i][j] = x; x *= grid[i][j]; x%=N; } } x=1; vector<vector<long long>>suffix(m,vector<long long>(n,1)); for (int i = m-1; i >= 0; i--){ for (int j = n-1; j >= 0; j--){ suffix[i][j] = x; x *= grid[i][j]; x%=N; } } vector<vector<int>>res(m,vector<int>(n,1)); for (int i = 0; i < m; i++){ for (int j = 0; j < n; j++){ res[i][j] = (int)((prefix[i][j] * 1LL* suffix[i][j])%N); } } return res; } }; >
Free preview — Unlock all questions →

Topics

Arrays Math