Maximum sub array problem – Kadane’s Algorithm

Given a sequence of n real numbers A(1) … A(n), program to find a contiguous subarray with the largest sum For example, var arr = new int[] {-2, 1,-3, 4,-1, 2, 1,-5, 4 }; the contiguous sub array with the largest sum is [4, −1, 2, 1], with sum 6. Solution Solution via iterating over all array elements repeatedly, try each key elements Execute above … Continue reading Maximum sub array problem – Kadane’s Algorithm

Sock Merchant – HackerRank Problem Solution

Question: John works at a clothing store. He has a large pile of socks that he must pair by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are. For example, there are n=7  socks  with colors  ar = [1,2,1,2,1,3,2]. There is one pair of color 1 and one of color 2 . There … Continue reading Sock Merchant – HackerRank Problem Solution

Array vs ArrayList

Array: An array is a collection of similar data types that can be accessed by index. Array allocates then static memory with consecutive section of memory blocks. ArrayList: ArrayList is a collection of objects of same or different types. ArrayList size can be dynamically increased or decreased as per the requirement. Here is difference table of Array vs ArrayList Array ArrayList Array stores a fixed number of elements. ArrayList is dynamic … Continue reading Array vs ArrayList