Skip to content

Added Important Algos #1322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
May 25, 2020
Merged
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
487 changes: 487 additions & 0 deletions DataStructures/Graphs/GraphAlgos

Large diffs are not rendered by default.

64 changes: 64 additions & 0 deletions DataStructures/HashMap/Hashing/Intersection
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package DataStructures.HashMap.Hashing;
/*
* this is algo which implies common mathematical set theory concept
* called intersection in which result is common values of both the sets
* here metaphor of sets is HashMap


Test Case:
Scanner scn=new Scanner(System.in);
int len =scn.nextInt();
int arr[]=new int[len];
int arr2[]=new int[len];

for(int i=0;i<2*len;i++) {

if(i<len)
arr[i]=scn.nextInt();
if(i>=len) {
arr2[i-len]=scn.nextInt();
}
}
System.out.println(Main(arr,arr2));



*/

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class Intersection {

public static ArrayList Main(int arr[],int arr2[]) {
HashMap<Integer,Integer> hmap=new HashMap<>();
HashMap<Integer,Integer> hmap2=new HashMap<>();
for(int i=0;i<arr.length;i++) {
if(hmap.containsKey(arr[i])) {
int val=hmap.get(arr[i]);
hmap.put(arr[i],val+1);
}else
hmap.put(arr[i],1);

}
ArrayList<Integer> res=new ArrayList<>();
for(int i=0;i<arr2.length;i++) {
if(hmap.containsKey(arr2[i])&&hmap.get(arr2[i])>0) {
int val=hmap.get(arr2[i]);
hmap.put(arr2[i],val-1);
res.add(arr2[i]);
}

}
return res;
}
public Intersection() {

}



}
63 changes: 0 additions & 63 deletions DataStructures/HashMap/Hashing/LinkedList.java

This file was deleted.

11 changes: 0 additions & 11 deletions DataStructures/HashMap/Hashing/Node.java

This file was deleted.

141 changes: 141 additions & 0 deletions DataStructures/Lists/ListAddnFun
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package DataStructures.Lists;

/*
* This class implements a SinglyLinked List.
* A linked list is similar to an array, it hold values.
* However, links in a linked list do not have indexes. With
* a linked list you do not need to predetermine it's size as
* it grows and shrinks as it is edited.
*it has functions called mid that gives node at mid
* in addn to linked list there is algo that
* construct a linked list with alternate sums of linked list
* and added to new one and add mid value
* i.e sum of first and last value of inital list

Test Case:


LinkedList LL1 = new LinkedList();
Scanner scn=new Scanner(System.in);
int numNodes=scn.nextInt();
for(int i=0;i<2*numNodes;i++) {
LL1.addLast(scn.nextInt());
}
LL1.display();
LinkedList LL2=new LinkedList();
LL2.formLL2(LL1);
LL2.display();
LinkedList LL3=new LinkedList();
LL3.formLL3(LL1);
LL3.display();
Node MID=LL1.midValue();
System.out.println(MID.data);
LinkedList updLL1=new LinkedList();
updLL1.formRes(LL1,LL2,LL3,MID);
updLL1.display();
updLL1.Size();

*/



import java.util.*;
import java.lang.*;
import java.io.*;
class LinkedList {
private class Node{
int data;
Node next;

Node(int data) {
this.data = data;
this.next = null;
}
}
public Node head = null;
public Node tail = null;
private int size=0;

public void addLast(int data) {
Node newNode = new Node(data);

if(this.head == null) {
this.head = newNode;
this.tail = newNode;
this.size++;
}
else {
this.tail.next = newNode;
this.tail = newNode;
this.size++;
}
}


public void display() {
Node current = this.head;
if(this.head == null) {
return;
}
while(current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}

public void formLL2(LinkedList LL1) {
Node current=LL1.head;
while(current.next!=null&&current.next.next!=null) {
int sum=current.data+current.next.next.data;
this.addLast(sum);
current=current.next.next;
}
}
public void formLL3(LinkedList LL1) {
Node current=LL1.head.next;
while(current.next!=null&&current.next.next!=null) {
int sum=current.data+current.next.next.data;
this.addLast(sum);
current=current.next.next;
}
}
public Node mid() {
Node slow=this.head;
Node fast=this.head;
while(fast.next!=null && fast.next.next!=null) {
slow=slow.next;
fast=fast.next.next;
}
return slow;
}
public Node midValue() {
int sum=this.head.data+this.tail.data;
Node mid=new Node(sum);
return mid;
}
public void formRes(LinkedList LL1,LinkedList LL2,LinkedList LL3,Node MID) {
Node LL1mid=LL1.mid();
Node currentLL1=LL1.head;
Node currentLL2=LL2.head;
Node currentLL3=LL3.head;
while(currentLL1!=null) {
this.addLast(currentLL1.data);

if(currentLL2!=null) {
this.addLast(currentLL2.data);
currentLL2=currentLL2.next;
}else if(currentLL1.equals(LL1mid)) {
this.addLast(MID.data);
}
else if(currentLL2==null&&currentLL3!=null) {
this.addLast(currentLL3.data);
currentLL3=currentLL3.next;
}
currentLL1=currentLL1.next;
}
}
public void Size() {
System.out.println(this.size);
}
}
Loading