Testing if a multiset is entirely consecutive numbers I want to test if a multiset $S$ of $N$ positive integers contains the values $\{1,2,...,N\}$. If $\sum{S_i} = \sum{i}$ and $\sum{S_i^2} = \sum{i^2}$, does it follow that $S$ must be the multiset $\{1,2,...,N\}$?  I'm looking for a proof or counterexample.
Background:
I'm trying write a fast test without sorting the multiset or using extra memory. The approach above appears to pass all the tests I've tried but I can't find any proof that it will always work or discussion of the topic. Pointers to relevant material would also be appreciated.
Implementation in python:
def sum_series(n): return int(n*(n+1)//2)

def sum_series_of_sq(n): return int((n*(n+1)*(2*n+1))/6)

def multiset_is_consecutive_ints(l):
  n = len(l)
  sumseq = sum([ i for i in l ])
  sumseqsq = sum([ i*i for i in l ])
  return sumseq == sum_series(n) and sumseqsq == sum_series_of_sq(n)

 A: The set of vectors in $\mathbb R^n$ $(a_1,a_2\dots a_n)$ that satisfy $a_1+a_2+\dots + a_n=\frac{n(n+1)}{2}$ is a plane.
The set of vectors in $\mathbb R^n$ that satisfy $a_1^2+a_2^2+\dots + a_n=\frac{n(n+1)(2n+1)}{6}$ is a sphere.
The intersection of the plane and the sphere is a sphere of dimension $n-1$ which contains infinite points. (when $n=2$ you get a sphere of dimension $1$, which consists of two points $(1,2)$ and $(2,1)$.
Your problem boils down to figuring out whether this $n-1$ sphere contains another lattice point, this seems really hard to determine.

My proposed method to solve your problem:
Step $1$ is to check whether the maximum element is $n$ and the minimum element is $1$, this clearly can be done in $\mathcal O(n)$ time and no additional space.
If the array passes this first test we just need to check whether it contains repeated elements, here is a way to do it ( the trick is to change thesign of A[abso(A[i])] when we pass by i, if this value is negative before changing it it means $abso(A[i])= abso(A[j])$, which implies a repeat)
#include <bits/stdc++.h>
using namespace std;

const int INF=2e9; //a surrogate infinity

int abso(int x){ // absolute value function
    if(x>0) return(x);
    return(-x);
}

int main(){
    int m=INF,M=-INF,n; //temporary min,max and the value of n
    scanf("%d",&n); // read the size of array
    int A[n]; // the array
    for(int i=0;i<n;i++){ // fill the array
        scanf("%d",&n);
    }
    for(int i=0;i<n;i++){
        m=min(m,A[i]);
        M=max(M,A[i]);
    }
    if(m!=1) return(0);
    if(M!=n) return(0);
    int maximum=0; // counts the number of times n appears
    for(int i=0;i<n;i++){// now we check it does not repeat elements
        if(A[i]==n) maximum++;
        else{
            if(A[ abso(A[i]) ]<0) return(0); // this means A[abso ( A[i] )] was modified before
            A[ abso(A[i]) ]*=-1;// we modify A[abso( A[i] )]
        }
        if(maximum > 1) return(0);
    }
    return(1);
}

