Find Maximum Subarray Sum (Kadane’s algorithm)

In this tutorial, I am going to discuss a very famous interview problem find maximum subarray sum (Kadane’s algorithm).

Given an integer array of N elements, find the maximum sum contiguous subarray (containing at least one element).

For example –

Example 1 –

Input: { 1, 2, -5, 4, 3, 8 , 5 }

Output: 20

The maximum sum of a subarray in this array is 20.  And the subarray is (4, 3, 8, 5).

Example 2 –

Input: {-2, -1}

Output: -1

Example 3 –

Input: { 1, -3, 2, -5, 7, 6, -1, -4, 11, -23}

Output: 19

The largest sum contiguous subarray  in this array is 19 ( 7, 6, -1, -4, 11).

Find Factorial of a Number in PHP using For Loop & Recursion

How to calculate factorial of a number in PHP. In this tutorial, I am going to discuss factorial program in PHP using recursion as well as iterative approach.

Before solving this problem let’s understand what is factorial?

What is Factorial?

Factorial of a non-negative integer n (n!), is the product of all positive integers less than or equal to n.

For example –

5! = 5 * 4 * 3 * 2 * 1 = 120

4! = 4 * 3 * 2 * 1 = 24