Divide the remaining sum into n - k elements
temp = diff / k ; rem = diff % k ; if ( temp + rem >= l && temp + rem <= r ) { a [ i ] = temp ; } else if ( temp + rem > r ) { a [ i ] = r ; } else if ( temp + rem < r ) { cout << " - 1" ; return ; } diff = diff - a [ i ] ; k = k - 1 ; } if ( diff ) { cout << " - 1" ; return ; } }