Category Archives: Data Structures

Quick Sort

  /** * QuickSort using Java * @author asharda * */ public class QuickSort { public void qsort(int []arr,int left,int right) { // int left=0; // int right=arr.length-1; //System.out.println(“Lowest element is “+arr[left] + ” Highest element is “+arr[right]); int pivot=arr[right]; … Continue reading

Posted in Data Structures, Java, Uncategorized | Tagged | Leave a comment

Knapsack Problem using Java

/** * Knapsack Problem to maximize value given the weight * @author asharda * */ public class Knapsack { public int maxValue(int []weight,int []value,int maxWeight) { int sum[]=new int [value.length]; sum[0]=value[0]; int max=value[0]; for(int i=1;i<value.length;i++) { if(weight[i-1]+weight[i]<=maxWeight) { sum[i]=Math.max(value[i], value[i-1]+value[i]); … Continue reading

Posted in Data Structures, Java, LeetCode Solutions, Uncategorized | Tagged , | Leave a comment

Find depth of binary tree

import java.util.Stack; /** * Find Depth of Binary Tree * @author asharda * */ class TreeNodes { int data; TreeNodes left; TreeNodes right; TreeNodes(int data) { this.data=data; } } public class TreeDepth { static int count=0; public void depth(Stack<TreeNodes> st) … Continue reading

Posted in Data Structures, Java, Java 8, Uncategorized | Tagged , | Leave a comment

Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0

/** * @author asharda * */ public class Matrix { public int [][] resetMatrix(int [][]a) { int row[]=new int [a.length]; int []column=new int[a[0].length]; for(int i=0;i<a.length;i++) { for(int j=0;j<a[0].length;j++) { if(a[i][j]==0) { row[i]=1; column[j]=1; }//end of if }//end of j }//end … Continue reading

Posted in Data Structures, Design Pattern, Uncategorized | Tagged , | Leave a comment

Fibonnaic series using Java

  /** * Fibonnaci Series * @author asharda * */ public class Fibonnaci { public int getFib(int num) { if(num<2) { return num; } return getFib(num-1)+getFib(num-2); //return f; } public static void main(String[] args) { Fibonnaci f =new Fibonnaci(); System.out.println(“Fibonaci … Continue reading

Posted in Data Structures, Design Pattern, Java, Java 8, Uncategorized | Tagged , | Leave a comment

What is a Brute Force solution

Nice article on Brute Force : https://stackoverflow.com/questions/8103050/what-exactly-is-the-brute-force-algorithm?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

Posted in Data Structures, Java, Uncategorized | Tagged | Leave a comment

Find Peak Element in an array Java

  import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; /** * @author asharda * */ public class Peak { public int findPeakElement(int[] nums) { Integer[] num = new Integer[nums.length]; for (int i = 0; i < nums.length; i++) { num[i] = Integer.valueOf(nums[i]); … Continue reading

Posted in Data Structures, Java, Java 8, Uncategorized | Tagged | Leave a comment