Given the marginals of a contingency table, what is the maximum observable value for the $\chi^2$ Pearson statistic? I know that given a $R\times C$ contingency table observed on $N$ subjects, the maximum value of $\chi^2$ statistics is $N\cdot [\min(R,C)-1]$. But, this is independent of given margins (namely, row totals and column totals. 
Is there a closed formula or an easy way for computing such a constrained maximum? 
For example: in a $2\times 2$ contingency table with margins $[50, 50]$ and $[10,90]$, it is easy to observe that there the table
$$\begin{array}{*{20}{c}}
0&{50}\\
10&{40}
\end{array}$$
is the only one having the highest $\chi^2$ ($11.11$ and not $100$) among all the tables with the same marginals.
Are there papers or algorithms for searching it in a feasible time also for a  generic $R\times C$ table given its marginals?
$R\times C$ table is a rectangular contingency table where $R$ is a given number of rows and $C$ a given number of columns, each one greater than 1 and not necessarily the same. 
The question is: if I know only the marginal frequency distributions (namely, the row and the column totals), what is the maximum value of the $\chi^2$ statistics that can be obtained? This value can be obtained via the solution of an optimization problem algebraically or there exist some algorithms? Example with a $3\times4$ table:
$$\begin{array}{*{20}{c}}
{{n_{11}}}&{{n_{12}}}&{{n_{13}}}&{{n_{14}}}&{20}\\
{{n_{21}}}&{{n_{22}}}&{{n_{23}}}&{{n_{24}}}&{100}\\
{{n_{31}}}&{{n_{32}}}&{{n_{33}}}&{{n_{34}}}&{380}\\
{50}&{80}&{170}&{200}&{400}
\end{array}$$
what are the $n_{ij}$ values that allow for the maximum $\chi^2$ Pearson statistics?
 A: I think a general formula for anything but a 2x2 table would be messy at best.
A brute force method would be for a specific set of marginals to generate a million or more tables with those marginals and use the maximum of those simulations as a lower bound for the maximum chi-square value.  Here is an R implementation of that:
# Marginals
  rowTotals = c(20,100,380)
  colTotals = c(50,80,170,200)

  nsim = 1000000
  chi2max = 0
  nrows = length(rowTotals)
  ncols = length(colTotals)
  observed = matrix(rep(NA,nrows*ncols), nrow=nrows, ncol=ncols, byrow=TRUE)
  p = rep(1,ncols)/ncols
  for (i in 1:nsim) {
      for (j in 1:(nrows-1)) {
          observed[j,] = rmultinom(1,rowTotals[j],p)
      }
      observed[nrows,] = colTotals - 
        colSums(matrix(observed[c(1:(nrows-1)),], nrow=nrows-1, byrow=TRUE))
      if (min(observed[nrows,]) >= 0) {
         chi2 = chisq.test(observed,correct=FALSE)$statistic
         if (chi2 > chi2max) {
            maxCounts = observed
            chi2max = chi2
         }
      }
  }
  chi2max
  # X-squared 
  # 629.4737 

  maxCounts
  #     [,1] [,2] [,3] [,4]
  #[1,]   20    0    0    0
  #[2,]   20   80    0    0
  #[3,]   10    0  170  200

(Note that I don't use "<-" for setting values I guess because I'm lazy and stubborn.) 
I suspect that there will be a substantial number of zeros and rows or columns with a single number associated with the maximum chisquared value.      
Using Mathematica (because it does algebra way better than I can) the maximum chisquare value given row and column marginals $r_1$, $r_2$, $c_1$, and $c_2$ (with $n=r_1+r_2=c_1+c_2$) is
$$\begin{array}{cc}
 \{ & 
\begin{array}{cc}
 \frac{n c_1 r_1}{c_2 r_2} & c_1<n\land (r_1<n\lor n>2 r_1)\land (2 c_1>n\lor n\leq 2 r_1) \\
 \frac{n c_1 r_2}{r_1 c_2} & n>2 r_1\land c_1\leq r_1 \\
 \frac{n r_1 c_2}{c_1 r_2} & (2 c_1=n\lor (c_1>r_1\land 2 c_1<n))\land n>2 r_1 \\
\end{array}
 \\
\end{array}$$
A: I found a closed formula for $2\times 2$ table, 
$$Max(\chi^2)=n_{..}\frac{p}{(1-p)}\frac{\left[1-(p+\delta)\right]}{(p+\delta)}\leq n_{..}$$
where $p$ and $1-p$ are the marginal relative frequencies of the first variable ($p<.5$) and $p+\delta$ and $1-p-\delta$ are the marginal relative frequencies of the second variable ($0\leq\delta<0.5$, $p+\delta\leq 0.5\leq 1-p-\delta$), and $n_{..}$ is the sum of joint frequencies. Equality holds only if $\delta=0$.
I wrote an algorithm that does not require a brute force search, but I would prove that the algorithm result if an effective maximum. The algorithm in R is more or less this
    max_possible_chi_2=function(rows,cols){
    if (sum(rows)!=sum(cols)){
    stop("sum of rows must be equal to the sum of cols")
    }
      #define frechet bounds
      M1=matrix(rows,length(rows),length(cols))
      M2=matrix(cols,length(rows),length(cols), byrow = T)
      RESU=M1*0
      R1=rows
      C1=cols
      while ((sum(R1)+sum(C1))>1e-16){
        TM1=matrix(R1,length(rows),length(cols))
        TM2=matrix(C1,length(rows),length(cols), byrow = T)
        FB=pmin(TM1,TM2)
        els=FB^2/(M1*M2)
        WH=which(els == max(els), arr.ind = TRUE)[1,]
        RESU[WH[1],WH[2]]=FB[WH[1],WH[2]]
        R1[WH[1]]=R1[WH[1]]-FB[WH[1],WH[2]]
        C1[WH[2]]=C1[WH[2]]-FB[WH[1],WH[2]]        
      }
      chi=sum(rows)*(sum(RESU^2/(M1*M2))-1)
      v=sqrt(chi/(sum(rows)*(min(length(rows),length(cols))-1)))
      print(chi)
      print(v)
      return(RESU)
    }

