Wednesday, June 14, 2017

Nth Digit

Problem Statement

Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...
Note:
n is positive and will fit within the range of a 32-bit signed integer (n < 231).

Link to gitHub :Code

Add Strings

Problem Statement

Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.
Note:
  1. The length of both num1 and num2 is < 5100.
  2. Both num1 and num2 contains only digits 0-9.
  3. Both num1 and num2 does not contain any leading zero.
  4. You must not use any built-in BigInteger library or convert the inputs to integer directly.
Link to GitHub :Code

Sunday, June 11, 2017

Minimum Moves to Equal Array Elements



Problem Statement
Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1.

Link to GitHubCode

Wednesday, June 7, 2017

Max consecutive ones


Problem Statement

Given a binary array, find the maximum number of consecutive 1s in this array.

Link to GitHub :Code


Reshape Matrix array manipulations

Problem statement

In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data.
You're given a matrix represented by a two-dimensional array, and two positive integers r and c representing the row number and column number of the wanted reshaped matrix, respectively.
The reshaped matrix need to be filled with all the elements of the original matrix in the same row-traversing order as they were.
If the 'reshape' operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.
Link to GitHub Code

Missing Number

Problem Statement

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.

Link to GitHub code

Tuesday, June 6, 2017

Array partition



Problem Statement

Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.

Link to Github :Code

Find All Numbers Disappeared in an Array


Problem Statement

Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
Find all the elements of [1, n] inclusive that do not appear in this array.
Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.

K diff pairs in an Array

Problem Statement

Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in the array and their absolute difference is k.

Link to GitHubCode

Monday, June 5, 2017

Move Zeros

Problem Statement

Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.

Example


For example, given nums = [0, 1, 0, 3, 12], after calling your function, 
nums should be [1, 3, 12, 0, 0].

Two Sum

Problem Statement

Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.

Link to GitHub : code

Remove element


Problem Statement

Given an array and a value, remove all instances of that value in place and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.

Link to GitHubCode

Remove duplicates from the sorted array

Problem Statement

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.

Contains Duplicate II


Problem Statement


Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.

Basically we need to validate if we have near by duplicate

Link to Github :code

Plus one

Problem Statement


Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
You may assume the integer do not contain any leading zero, except the number 0 itself.
The digits are stored such that the most significant digit is at the head of the list.

Link to GitHub :Code

Example
input = {1, 4, 9, 9}
output ={1, 5, 0, 0 }

Sunday, June 4, 2017

Best time to buy and sell a stock II

Problem Statement

Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
Solution using python

Time Complexity O(n)^2
Space Complexity 0(1)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
stocks=[7,1,5,2,4]
   
def buy_sell_once(s):
    maxprofit=0
    #first loop
    for i in range(len(s)-1):
        for j in range(i+1,len(s)):
            if s[j] - s[i] > maxprofit:
                maxprofit=s[j] - s[i]
    return maxprofit

m=buy_sell_once(stocks)
print(m)



Time Complexity O(n)
Space Complexity 0(1)


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
stocks =[7,1,5,2,4]

def buy_sell_once_ef(s):
    max_profit=0
    min_price=stocks[0]
    for price in range(len(stocks)-1):
        min_price = min(min_price,price)
        profit    =(price-min_price)
        max_profit=max(profit,max_profit)
    return max_profit
m=buy_sell_once(stocks)
print(m)

Solution using Java

Link to GitHibCode

Solution

  • We need a variable to store the maxProfit
  • We need to iterate through the array and Get the difference between selling price and buying price  maxprofit+=prices[i+1]-prices[i]
  • We need to make sure that selling price is greater than buying price. This check can be done using if condition if(prices[i+1] >prices[i])
 public static int get(int [] prices)
 {
  int maxprofit=0;
  //loop through the array set . We need to keep on adding the profit
  //We need to buy and sell before buying others
  for(int i=0;i<prices.length-1;i++)
  {
   //We need to make sure that selling is greater than profit
   if(prices[i+1] >prices[i])
   {
    maxprofit+=prices[i+1]-prices[i];
   }
  }
  return maxprofit;
  
 }

ContainDuplicates


Problem statement

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

Link to GitHub :Code

Best time to buy a stock

Problem Statement

Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Link to GitHub :Code


Saturday, June 3, 2017

Client side PDF generation


We can generate PDF in JavaScript using jsPDF library.

Edit in plkr

Steps involved
  • Use CDN reference to jsPDF 
  • Open the document
  • Pass the Json Object to the document
  • Save the file

Friday, June 2, 2017

Palindrome Number


Problem statement

Determine whether an integer is a palindrome. Do this without extra space.

Link to GitHub :Code 

Example
Input 12321 is a palindrome number
Input 1231 is not a palindrome number



Perfect Number


Problem Statement

We define the Perfect Number is a positive integer that is equal to the sum of all its positive divisors except itself.
Now, given an integer n, write a function that returns true when it is a perfect number and false when it is not.

Link to GitHub :Code

Thursday, June 1, 2017

Css for centering elements



There is no option to float center. However by thinking carefully about how elements behave, we can use positioning to achieve the same result

Center horizontally

we can center the element by specifying margin : 0 auto. This would make sure that it would be perfectly centered horizontally. This is telling the browser to move left and right in equal amounts (Would have equal amount of margin to the left and right).This would have a effect of centering it perfectly in the page

Note :
The values for the top and bottom could be anything and we would get the same result
we need to have to set width for the div. If not it would take the full width

Link to GitHub :Code

Using Css Float


Floating left

  • Removes an element from the normal document flow.
  • It then takes that element and pushes it to the far left as possible.
  • Other element would flow far left as possible to floe around the element and takes its original space
.floatRight{
    width:200px;
    height:200px;
    float:right;
    background-color: red;
    
}

Nesting CSS



If we want to inherit css property from other classes we can have nested css classes

Link to Github :Code

Image with rounded corners




  • We need to use the image tag
  • Need to be careful while specifying the relative path as shown below
  • Create css class with border-radius
  • Use the css class in the html 

CSS positioning


Allows to move from its normal position in the document modal.

Default position

Link to Github :Code