Skip to content
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@

## Contents

- [Arithmetic Analysis]()
- [File Transfer Protocol]()
- [Graphs]()
- [Math]()
- [Neutral Network]()
- [Ciphers]()
- [Data Structures]()
- [Dynamic Programming]()
- [Hashes]()
- [Searches]()
- [Sorting]()
- [Strings]()
- [Traversals]()
- [Arithmetic Analysis](arithmetic-analysis)
- [File Transfer Protocol](file-transfer-protocol)
- [Greedy Algorithms](greedy-algorithms)
- [Graphs](graphs)
- [Math](math)
- [Neutral Network](neutral-network)
- [Ciphers](ciphers)
- [Data Structures](data-structures)
- [Dynamic Programming](dynamic-programming)
- [Hashes](hashes)
- [Searches](searches)
- [Sorting](sorting)
- [Strings](strings)
- [Traversals](traversals)


## License

Expand Down
101 changes: 101 additions & 0 deletions data-structures/SegmentTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import java.util.*;
import java.lang.*;
import java.io.*;
class SegmentTree
{
int st[];
Segment(int []arr, int n)
{
int x = (int) Math.ceil((Math.log(n)/Math.log(2)));
int max_size = 2 *(int) (Math.pow(2,x) - 1);
st = new int[max_size];

constSegmentUtil(arr,0,n-1,0);
}

int constSegmentUtil(int []arr, int ss,int se,int si)
{
if(ss == se)
{
st[si] = arr[ss];
return arr[ss];
}
int mid = getMid(ss,se);
st[si] = max(constSegmentUtil(arr,ss,mid,2*si+1),constSegmentUtil(arr,mid+1,se,2*si+2));
return st[si];
}

int getMid(int s,int e)
{
return s + (e - s)/2;
}

int max(int a,int b)
{
if(a > b)
{
return a;
}
else{
return b;
}
}

void print(){
for (int i=0;i<st.length;i++ )
{
System.out.print(st[i]);
}
}

int getMaxUtil(int ss,int se,int qs,int qe,int si)
{
if(qs<=ss && qe>=se)
{
return st[si];
}

if(se<qs || ss>qe)
{
return 0;
}

int mid = getMid(ss,se);

return max(getMaxUtil(ss,mid,qs,qe,2*si+1),getMaxUtil(mid+1,se,qs,qe,2*si+2));
}


int getMax(int n,int qs,int qe){

if(qs > qe || qs < 0 || qe > n-1){
return -1;
}

return getMaxUtil(0,n-1,qs,qe,0);
}

public static void main (String[] args) throws java.lang.Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());//for testcase
String []temp = br.readLine().split(" ");//taking array as string
int []arr = new int[t];
for(int i=0;i<t;i++) //to convert array of string to array of integers
{
arr[i] = Integer.parseInt(temp[i]);
}
Segment tree = new Segment(arr,arr.length);
//tree.print();
int q = Integer.parseInt(br.readLine()); // no of queries
while (q--!=0)
{
String []temp1 = br.readLine().split(" ");
int s = Integer.parseInt(temp1[0]); //q1
int e = Integer.parseInt(temp1[1]); //q2

System.out.println(tree.getMax(arr.length,s-1,e-1)); // maximum in range [q1,q2]
//System.out.println(getMax(arr,n,s,e));
}
}
}
46 changes: 46 additions & 0 deletions dynamic-programming/EDIST.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

// A Naive recursive Java program to find minimum number
// operations to convert str1 to str2
class EDIST
{
static int min(int x,int y,int z)
{
if (x<=y && x<=z) return x;
if (y<=x && y<=z) return y;
else return z;
}

static int editDist(String str1 , String str2 , int m ,int n)
{
// If first string is empty, the only option is to
// insert all characters of second string into first
if (m == 0) return n;

// If second string is empty, the only option is to
// remove all characters of first string
if (n == 0) return m;

// If last characters of two strings are same, nothing
// much to do. Ignore last characters and get count for
// remaining strings.
if (str1.charAt(m-1) == str2.charAt(n-1))
return editDist(str1, str2, m-1, n-1);

// If last characters are not same, consider all three
// operations on last character of first string, recursively
// compute minimum cost for all three operations and take
// minimum of three values.
return 1 + min ( editDist(str1, str2, m, n-1), // Insert
editDist(str1, str2, m-1, n), // Remove
editDist(str1, str2, m-1, n-1) // Replace
);
}

public static void main(String args[])
{
String str1 = "sunday";
String str2 = "saturday";

System.out.println( editDist( str1 , str2 , str1.length(), str2.length()) );
}
}
42 changes: 42 additions & 0 deletions dynamic-programming/Knapsack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

// A Dynamic Programming based solution for 0-1 Knapsack problem
class Knapsack
{

// A utility function that returns maximum of two integers
static int max(int a, int b) { return (a > b)? a : b; }

// Returns the maximum value that can be put in a knapsack of capacity W
static int knapSack(int W, int wt[], int val[], int n)
{
int i, w;
int K[][] = new int[n+1][W+1];

// Build table K[][] in bottom up manner
for (i = 0; i <= n; i++)
{
for (w = 0; w <= W; w++)
{
if (i==0 || w==0)
K[i][w] = 0;
else if (wt[i-1] <= w)
K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]);
else
K[i][w] = K[i-1][w];
}
}

return K[n][W];
}


// Driver program to test above function
public static void main(String args[])
{
int val[] = new int[]{60, 100, 120};
int wt[] = new int[]{10, 20, 30};
int W = 50;
int n = val.length;
System.out.println(knapSack(W, wt, val, n));
}
}