In a garden represented as an infinite 2D grid, there is an apple tree planted at every integer coordinate. The apple tree planted at an integer coordinate (i, j) has |i| + |j| apples growing on it.

You will buy an axis-aligned square plot of land that is centered at (0, 0).

Given an integer neededApples, return the minimum perimeter of a plot such that at least neededApples apples are inside or on the perimeter of that plot.

The value of |x| is defined as:


	x if x &gt;= 0
	-x if x &lt; 0


&nbsp;
Example 1:

Input: neededApples = 1
Output: 8
Explanation: A square plot of side length 1 does not contain any apples.
However, a square plot of side length 2 has 12 apples inside (as depicted in the image above).
The perimeter is 2 * 4 = 8.


Example 2:

Input: neededApples = 13
Output: 16


Example 3:

Input: neededApples = 1000000000
Output: 5040


&nbsp;
Constraints:


	1 &lt;= neededApples &lt;= 1015


class Solution:
    def minimumPerimeter(self, nap: int) -> int:
        
        
#         here for n = 2 , there are two series :  
#         (1) Diagnal points for n=3 , diagnal apples = 2*n = 6
#         (2) there is series =   2,3,3 =  2+ (sigma(3)-sigma(2))*2
        
#         how to solve:
            
#             here 3  =  sigma(n+(n-1))-sigma(n) = sigma(2*n-1)-sigma(n) =  0.5*2n*(2n-1)-0.5*n*n-1
#         (3) so our final 2,3,3 =   3*2+2 =   (0.5*2n*(2n-1)-0.5*n*n-1)*2+n
#         (4) so final 2,3,3 =  3*n*n - 2*n
#         (5) we have 4 times repitation of (2,3,3)  = 4*(2,3,3)  =   4*(3*n*n - 2*n)   =  12*n*n - 8*n
#         (6) we have 4 diagnal points so their sum(4 diagnal)  = 4*(2*n)
#         (7)  so final sum(total) = 4 diagnal sum + 4(2,3,3)    =   4(2*n)   + 12*n*n - 8*n    =  12*n*n
        
#         so at nth distance we have total  12*n*n  apples at the circumfrance
        
#         so net sum =  sigma(12*n*n)  = 2*n*(n+1)*(2*n+1)
        
        
        n=1
        val=2*n*(n+1)*(2*n+1)
        while(val<nap):
            n+=1
            val=val=2*n*(n+1)*(2*n+1)
        return n*8
class Solution {
    public long minimumPerimeter(long neededApples) {
        long n = 0;
        long count = 0;
        while(count < neededApples) {
            n++;
            count += (12 * n * n);
        }
        return n * 8;
    }
}
class Solution {
public:
    long long minimumPerimeter(long long neededApples) {
        long long i = 0;
        long long x = 0;
 
        while(x < neededApples){   
            x += (long long) 12*pow(i,2); 
            ++i;
        }
        return (long long) (i-1)*8;
    }
};
/**
 * @param {number} neededApples
 * @return {number}
 */
var minimumPerimeter = function(neededApples) {
    
    
    let start = 2, n = 1, dp = [0];
    
    while(true){
        let cur = start, res = dp[n-1];
		
		//Add the X and Y axis apples multiplied with 4 
        res += (cur + cur - n)*4;
        
		// Add the interior apples using and AP formula of  ( n - 1 ) / 2 * (2a + ( n-1 ) * d ) & multiplying it by 8 as count is 8  
        res += ( ((n-1)/2) *  ( ( 2 * (cur - 1) ) + ( (n-2) * -1 ) )  ) * 8
        
		//Setting the result to DP
        dp[n] = res;
		
		//If current state is greater than or equal to needed then return the perimeter
        if(neededApples <= res) return 8*n;
        n++;
        start +=2;
        
    }
};
Minimum Garden Perimeter to Collect Enough Apples