Skip to content

Instantly share code, notes, and snippets.

View pavelnganpi's full-sized avatar

pavey nganpi pavelnganpi

  • truex
  • World
View GitHub Profile
{
type: "FeatureCollection",
features: [
{
type: "Feature",
geometry: {
type: "Point",
coordinates: [
-112.11971469843907,
33.36944420834164
var get_missing_letters = function (str) {
var alphabets = new Array(26);
var missing = [];
//initialize data
for (var j = 0; j < alphabets.length; j++) {
alphabets[j] = 0;
}
@pavelnganpi
pavelnganpi / LRE.java
Created August 14, 2014 04:52
Run Length Encoding (RLE) is an algorithm for compressing strings, which is specifically useful in compressing binary values. aabccaabaad <--> 2a1b2c2a1b2a1d Implement RLE algorithm for compression and decompression of strings.
public class RunLengthEncoding {
public static String LRE(String str){
StringBuilder test = new StringBuilder();
int count = 1;
char[] chars = str.toCharArray();
for(int i=0;i<chars.length;i++){
char c = chars[i];
#include<stdio.h>
int max(int, int); /* to get maximum of two integers */
int min(int, int); /* to get minimum of two integeres */
int median(int [], int); /* to get median of a sorted array */
/* This function returns median of ar1[] and ar2[].
Assumptions in this function:
Both ar1[] and ar2[] are sorted arrays
Both have n elements */
@pavelnganpi
pavelnganpi / NonAdjacentMaximumSumSubsequence .java
Last active August 29, 2015 14:04
Given an array of positive numbers, find the maximum sum of a subsequence with the constraint that no 2 numbers in the sequence should be adjacent in the array. So 3 2 7 10 should return 13 (sum of 3 and 10) or 3 2 5 10 7 should return 15 (sum of 3, 5 and 7).Answer the question in most efficient way.
//method 1, works only for postive numbers
public class NonAdjacentMaximumSumSubsequence {
public static int NoAdjacentSum(int a[], int n)
{
int[] dp =new int[n];
dp[0] = a[0];
dp[1] =a[1];
@pavelnganpi
pavelnganpi / ProgramForArrayRotation .java
Last active August 29, 2015 14:04
Write a function rotate(ar[], d, n) that rotates arr[] of size n by d elements. * @author paveynganpi
/**
*
* Write a function rotate(ar[], d, n) that rotates arr[] of size n by d elements.
* @author paveynganpi
*
*/
//reversal solution
/**
*
@pavelnganpi
pavelnganpi / getMedianOfTwoSortedArrays.java
Created August 5, 2014 13:57
Median of two sorted arrays Question: There are 2 sorted arrays A and B of size n each. Write an algorithm to find the median of the array obtained after merging the above 2 arrays(i.e. array of length 2n). The complexity should be O(log(n))
public class getMedianOfTwoSortedArrays {
public static int getMedian(int ar1[], int ar2[], int n)
{
int i = 0; /* Current index of i/p array ar1[] */
int j = 0; /* Current index of i/p array ar2[] */
int count;
int m1 = -1, m2 = -1;
/* Since there are 2n elements, median will be average
@pavelnganpi
pavelnganpi / MergeAnArrayOfSizeNIntoAnotherArrayOfSizeMPlusN.java
Created August 4, 2014 15:18
There are two sorted arrays. First one is of size m+n containing only m elements. Another one is of size n and contains n elements. Merge these two arrays into the first array of size m+n such that the output is sorted
public class MergeAnArrayOfSizeNIntoAnotherArrayOfSizeMPlusN {
static int NA=-1;
//move m elements to end of array
public static int[] moveToEnd(int[] mPlusN){
int size = mPlusN.length;
int i = size-1;
@pavelnganpi
pavelnganpi / Searchanelementinasortedandpivotedarray.java
Created August 4, 2014 03:31
An element in a sorted array can be found in O(log n) time via binary search. But suppose I rotate the sorted array at some pivot unknown to you beforehand. So for instance, 1 2 3 4 5 might become 3 4 5 1 2. Devise a way to find an element in the rotated array in O(log n) time
/**
*
* An element in a sorted array can be found in O(log n) time via
* binary search. But suppose I rotate the sorted array at some pivot
* unknown to you beforehand. So for instance, 1 2 3 4 5 might become
* 3 4 5 1 2. Devise a way to find an element in the rotated array in O(log n) time
* @author paveynganpi
*
*/
@pavelnganpi
pavelnganpi / NumberAppearingOddNumberOfTimesInArray.java
Created August 4, 2014 02:34
Given an array of positive integers. All numbers occur even number of * times except one number which occurs odd number of times. Find the number * in O(n) time & constant space.
/**
*
* Given an array of positive integers. All numbers occur even number of
* times except one number which occurs odd number of times. Find the number
* in O(n) time & constant space.
Example:
I/P = [1, 2, 3, 2, 3, 1, 3]
O/P = 3