NP vs P

Advertisements
Posted in Uncategorized | Leave a comment

Time Complexity Nice Article

Posted in Uncategorized | Leave a comment

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];
int current=left;
while(current<right)
{
if(arr[current] <=pivot)
{
int temp=arr[left];
arr[left]=arr[current];
arr[current]=temp;
left++;
current++;
}
else
{
current++;
}//end of else
}//end of while

// current++;
if(current<=right)
{
int temp=arr[left];
arr[left]=arr[current];
arr[current]=temp;
}
if(left<right)
{
qsort(arr,left,right);
}
}
/**
* @param args
*/
public static void main(String[] args) {

QuickSort q=new QuickSort();
int []arr= {6,5,1,3,8,4,7,9,2};
q.qsort(arr,0,arr.length-1);
for(int i:arr)
{
System.out.println(i);
}
}

}

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

Check if word exists in a string using Matcher

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Using Patterns
*
* @author asharda
*
*/
public class PatternTest {

/**
* @param args
*/
public static void main(String[] args) {

String text = “This is the time of elections”;
String search = “iso”;
Pattern pattern = Pattern.compile(search);
Matcher match = pattern.matcher(text);
if (match.find()) {
System.out.println(“Text Found”);
} else {
System.out.println(“Text not Found”);
}
}

}

Posted in 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]);
max=Math.max(max, sum[i]);
}
}
return max;
}
/**
* @param args
*/
public static void main(String[] args) {

Knapsack k=new Knapsack();
int value[]= {60,100,120};
int weight[]= {10,20,30};
int maxi=k.maxValue(weight, value, 50);
System.out.println(“Max value seen is “+maxi);
}

}

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

Fix Max number in an array

 

import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.stream.Collectors;

/**
* Find Max number in an array
* @author asharda
*
*/
public class MaxArray {
/**
* @param args
*/
public static void main(String[] args) {

int arr[]= {3,5,2,8};
List<Integer> list=Arrays.stream(arr).boxed().collect(Collectors.toList());
IntSummaryStatistics stats= list.stream().mapToInt((i)->i).summaryStatistics();
System.out.println(“Max element in the array is “+stats.getMax());
System.out.println(“Min element in the array is “+stats.getMin());
}

}

Posted in Uncategorized | Leave a comment

Reverse LinkedList using Stack Java

import java.util.Stack;

/**
* @author asharda
*
*/

class TreeNodees {
int data;
TreeNodees next;

TreeNodees(int data) {
this.data = data;
}
}

 

public class ReverseLL {

public TreeNodees insert(TreeNodees node, int data) {
TreeNodees temp;
if (node == null) {
node = new TreeNodees(data);
return node;
} else {
temp = node;
while (temp.next != null) {
temp = temp.next;
}
temp.next = new TreeNodees(data);
return node;
}
}

public void display(TreeNodees node) {
while (node != null) {
System.out.println(node.data);
node = node.next;
}
}

public void display(Stack<Integer> stack) {
if (stack.isEmpty()) {
return;
}

else {
while (!stack.isEmpty()) {
System.out.println(stack.pop());
}
}
}

public Stack<Integer> reverse(TreeNodees root) {
Stack<Integer> st = new Stack<Integer>();
while (root != null) {
st.push(root.data);
root = root.next;
}
return st;
}

/**
* @param args
*/
public static void main(String[] args) {

ReverseLL list = new ReverseLL();
TreeNodees head = new TreeNodees(10);
head = list.insert(head, 20);
head = list.insert(head, 30);
list.display(head);
Stack<Integer> st = list.reverse(head);
list.display(st);
}

}

Posted in Uncategorized | Leave a comment