From 52c37efecabe8de2b1f5c418269020e612062f9e Mon Sep 17 00:00:00 2001 From: Vishal Bindal Date: Sun, 2 Jul 2023 17:10:33 +0530 Subject: [PATCH] added few more algorithms in Java --- public/templates/ModifiedMva.java | 56 ++++++++++++++++++++ public/templates/Mva.java | 39 ++++++++++++++ public/templates/dijkstra.java | 84 ++++++++++++++++++++++++++++++ public/templates/dnf.java | 46 ++++++++++++++++ public/templates/fenwick_tree.cpp | 4 +- public/templates/fenwick_tree.java | 38 ++++++++++++++ public/templates/templates.json | 42 +++++++++++++++ 7 files changed, 308 insertions(+), 1 deletion(-) create mode 100644 public/templates/ModifiedMva.java create mode 100644 public/templates/Mva.java create mode 100644 public/templates/dijkstra.java create mode 100644 public/templates/dnf.java create mode 100644 public/templates/fenwick_tree.java diff --git a/public/templates/ModifiedMva.java b/public/templates/ModifiedMva.java new file mode 100644 index 0000000..0b8588d --- /dev/null +++ b/public/templates/ModifiedMva.java @@ -0,0 +1,56 @@ +import java.util.ArrayList; +import java.util.List; + +public class Modified_Mva { + + public static List findMajorityElements(int[] nums) { + List result = new ArrayList<>(); + int count1 = 0, count2 = 0, candidate1 = 0, candidate2 = 1; + + for (int num : nums) { + if (num == candidate1) { + count1++; + } else if (num == candidate2) { + count2++; + } else if (count1 == 0) { + candidate1 = num; + count1 = 1; + } else if (count2 == 0) { + candidate2 = num; + count2 = 1; + } else { + count1--; + count2--; + } + } + + count1 = 0; + count2 = 0; + + for (int num : nums) { + if (num == candidate1) { + count1++; + } else if (num == candidate2) { + count2++; + } + } + + int n = nums.length; + if (count1 > n / 3) { + result.add(candidate1); + } + if (count2 > n / 3) { + result.add(candidate2); + } + System.out.println(result); + return result; + } + + public static void main(String [] args){ + int [] arr = new int[]{1,4,5,2,2,1,1,1,3,2,3,2,4,1,2,2,1,1,1,2}; + + findMajorityElements(arr); + } + } + + diff --git a/public/templates/Mva.java b/public/templates/Mva.java new file mode 100644 index 0000000..71ee6b4 --- /dev/null +++ b/public/templates/Mva.java @@ -0,0 +1,39 @@ +public class Mva { + + // moore's voting algorithm for finding the elements that occur more than + // n/2 times in an array n is the size of the array + public static int findMajorityElement(int[] nums) { + int count = 0, candidate = -1; + + // Finding majority candidate + for (int index = 0; index < nums.length; index++) { + if (count == 0) { + candidate = nums[index]; + count = 1; + } else { + if (nums[index] == candidate) + count++; + else + count--; + } + } + + // Checking if majority candidate occurs more than n/2 times + count = 0; + for (int index = 0; index < nums.length; index++) { + if (nums[index] == candidate) + count++; + } + if (count > (nums.length / 2)) + return candidate; + return -1; + } + + public static void main(String[] args) { + int[] arr = {1, 1, 1, 1, 2, 3, 4}; + int majority = findMajorityElement(arr); + System.out.println("The majority element is: " + majority); + } + } + + diff --git a/public/templates/dijkstra.java b/public/templates/dijkstra.java new file mode 100644 index 0000000..4b99095 --- /dev/null +++ b/public/templates/dijkstra.java @@ -0,0 +1,84 @@ +import java.util.*; + +class Pair implements Comparable { + long first; + int second; + + public Pair(long first, int second) { + this.first = first; + this.second = second; + } + + @Override + public int compareTo(Pair other) { + return Long.compare(this.first, other.first); + } +} + +public class dijkstra { + + static final long INF = (long) 1e15; + + public static int[] dijsktra(int S, ArrayList[] adj) { + int n = adj.length; + long[] dist = new long[n]; + Arrays.fill(dist, INF); + int[] par = new int[n]; + Arrays.fill(par, -1); + dist[S] = 0; + par[S] = S; + PriorityQueue pq = new PriorityQueue<>(); + pq.add(new Pair(0, S)); + + while (!pq.isEmpty()) { + Pair u = pq.poll(); + if (u.first > dist[u.second]) continue; + for (Pair v : adj[u.second]) { + if (dist[v.second] > dist[u.second] + v.first) { + dist[v.second] = dist[u.second] + v.first; + par[v.second] = u.second; + pq.add(new Pair(dist[v.second], v.second)); + } + } + } + return par; // or dist according to needs + } + + + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int m = scanner.nextInt(); + int D = n - 1; + int S = 0; + ArrayList[] adj = new ArrayList[n]; + for (int i = 0; i < n; i++) { + adj[i] = new ArrayList<>(); + } + while (m-- > 0) { + int u = scanner.nextInt() - 1; + int v = scanner.nextInt() - 1; + long w = scanner.nextLong(); + adj[u].add(new Pair(w, v)); + adj[v].add(new Pair(w, u)); + } + int[] par = dijsktra(S, adj); + if (par[D] == -1) { + System.out.println(-1); + } else { + int u = par[D]; + ArrayList path = new ArrayList<>(); + path.add(D); + while (path.get(path.size() - 1) != S) { + path.add(u); + u = par[u]; + } + Collections.reverse(path); + for (int node : path) { + System.out.print((node + 1) + " "); + } + } + } + +} diff --git a/public/templates/dnf.java b/public/templates/dnf.java new file mode 100644 index 0000000..dab101f --- /dev/null +++ b/public/templates/dnf.java @@ -0,0 +1,46 @@ +import java.util.Arrays; + +public class dnf { + //sort the array of 0 ,1 and 2 using dutch national flag algo + + static void sort(int a[], int arr_size) + { + int lo = 0; + int hi = arr_size - 1; + int mid = 0, temp = 0; + + while (mid <= hi) { + + + switch (a[mid]) { + // If the element is 0 + case 0 -> { + temp = a[lo]; + a[lo] = a[mid]; + a[mid] = temp; + lo++; + mid++; + break; + } + case 1 -> mid++; + case 2 -> { + temp = a[mid]; + a[mid] = a[hi]; + a[hi] = temp; + hi--; + break; + } + } + } + } + public static void main(String[] args){ + + + int [] arr = new int[]{0,1,1,0,2,1,1,1,2,2,2,0,2,1}; + int n = arr.length; + + sort(arr, n); + System.out.println(Arrays.toString(arr)); + + } +} diff --git a/public/templates/fenwick_tree.cpp b/public/templates/fenwick_tree.cpp index 4052491..13df3a6 100644 --- a/public/templates/fenwick_tree.cpp +++ b/public/templates/fenwick_tree.cpp @@ -1,4 +1,6 @@ const int MOD = 1e9+7; -void update(int BIT[],int x,int val,int N) { ++x; while(x<=N) { BIT[x]+=val;BIT[x] %=MOD; x+=(x&-x); } } +void update(int BIT[],int x,int val,int N) +{ ++x; while(x<=N) + { BIT[x]+=val;BIT[x] %=MOD; x+=(x&-x); } } long long query(int BIT[],int x) { ++x; long long res=0; while(x>0) { res+=BIT[x];res%=MOD; x-=(x&-x); } return res; } long long range(int bit[],int a,int b){ return (query(bit,b) - query(bit,a-1) + MOD)%MOD;} \ No newline at end of file diff --git a/public/templates/fenwick_tree.java b/public/templates/fenwick_tree.java new file mode 100644 index 0000000..ceb4d91 --- /dev/null +++ b/public/templates/fenwick_tree.java @@ -0,0 +1,38 @@ +class fenw{ + + + void update(long [] farr, int idx, long val, int n){ + while(idx0){ + res+=farr[idx]; + idx = idx - ((idx)&(-idx)); + } + return res; + } + + +} +public class fenwick_tree { + + public static void main(String [] args){ + long [] arr = new long[] {0,4,-3,2,6,7,8,14,-9}; + + fenw f = new fenw(); + int n = arr.length; + long [] farr = new long [n]; + for(int i=1;i