I have created hashtable to solve one of the problem on hackerearth using java.
link:https://www.hackerearth.com/practice/data-structures/hash-tables/basics-of-hash-tables/practice-problems/algorithm/mind-palaces-3/
My java solution is able to pass all the test cases.
Now with same logic ,I am creating solution in C.
But by solution in C does not pass all test cases.
I am learning C.
Please help me to find the problem in C code.
Thanks.
My Java Code:
package hashtable;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class MindPalces {
public static void main(String[] args) throws NumberFormatException,
IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String string = br.readLine();
String strArr[] = string.split(" ");
int n = Integer.parseInt(strArr[0]);
int m = Integer.parseInt(strArr[1]);
SinglyLinkLists hashTable[] = new SinglyLinkLists[337];
long array[][] = new long[n][m];
for (int i = 0; i < n; i++) {
String string1 = br.readLine();
String[] innerArr = string1.split(" ");
for (int j = 0; j < m; j++) {
array[i][j] = Long.parseLong((innerArr[j]));
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
long data = array[i][j];
if (data < 0) {
data = data * -1;
}
int index = Hashs.hashFunction(data);
if (hashTable[index] == null) {
SinglyLinkLists linkList = new SinglyLinkLists();
linkList.insertAtEnd(i, j, array[i][j]);
hashTable[index] = linkList;
} else {
SinglyLinkLists linkList = hashTable[index];
linkList.insertAtEnd(i, j, array[i][j]);
}
}
}
int q = Integer.parseInt(br.readLine());
for (int i = 0; i < q; i++) {
long val = Long.parseLong(br.readLine());
long val1 = val;
if (val < 0) {
val = val * -1;
}
int index = Hashs.hashFunction(val);
if (hashTable[index] == null) {
System.out.println("-1" + " " + "-1");
} else {
SinglyLinkLists linkList = hashTable[index];
NOde list = linkList.getNode(val1);
if (null != list) {
System.out.println(list.getI1() + " " + list.getJ1());
} else {
System.out.println("-1" + " " + "-1");
}
}
}
}
}
class Hashs {
public static int hashFunction(long key) {
return (int) (key % 337);
}
}
class NOde {
int i1 = 0;
int j1 = 0;
NOde link = null;
long number = 0;
public NOde() {
link = null;
i1 = 0;
j1 = 0;
number = 0;
}
public NOde(NOde node, int i1, int j1, long number) {
this.i1 = i1;
this.j1 = j1;
link = node;
this.number = number;
}
public NOde getLink() {
return link;
}
public void setLink(NOde link) {
this.link = link;
}
public int getI1() {
return i1;
}
public void setI1(int i1) {
this.i1 = i1;
}
public int getJ1() {
return j1;
}
public void setJ1(int j1) {
this.j1 = j1;
}
public long getNumber() {
return number;
}
public void setNumber(long number) {
this.number = number;
}
}
class SinglyLinkLists {
NOde start = null;
NOde end = null;
int size = 0;
public SinglyLinkLists() {
start = null;
end = null;
size = 0;
}
public void insertAtEnd(int i1, int j1, long number) {
NOde nptr = new NOde(null, i1, j1, number);
size++;
if (start == null) {
start = nptr;
end = start;
} else {
end.setLink(nptr);
end = nptr;
}
}
// Function to display elements
public NOde getNode(long val) {
if (start.getNumber() == val) {
return start;
}
NOde ptr = start;
ptr = start.getLink();
while (null != ptr && ptr.getLink() != null) {
if (ptr.getNumber() == val) {
return ptr;
}
ptr = ptr.getLink();
}
if (null != ptr && ptr.getNumber() == val) {
return ptr;
}
return null;
}
}
My C Code:
#include<stdio.h>
#include <stdlib.h>
struct Node
{
int i1;
int j1;
long inputVal;
struct Node * next;
};
struct hash
{
struct Node * head;
};
struct hash *hashTable=NULL;
struct Node *createNode(int i,int j,long number)
{
struct Node *list;
list = (struct Node *) malloc(sizeof(struct Node));
list->i1 = i;
list->j1 = j;
list->inputVal=number;
list->next = NULL;
return list;
};
int main()
{
int m=0,n=0;
scanf("%d%d",&m,&n);
hashTable=(struct hash *)calloc(337,sizeof(struct Node));
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
long data=0;
long copy=0;
scanf("%ld",&data);
copy=data;
if(data<0)
{
data=data*-1;
}
int index=hashFunction(data);
struct Node *newnode = createNode(i,j,copy);
struct Node *myNode = hashTable[index].head;
if (!myNode)
{
hashTable[index].head=newnode;
}
else
{
newnode->next=hashTable[index].head;
hashTable[index].head=newnode;
}
}
}
int q=0;
scanf("%d",&q);
for(int i=0; i<q; i++)
{
long val=0;
long copy=0;
int boolean=0;
scanf("%ld",&val);
copy=val;
if(val<0)
{
val=val*-1;
}
int index=hashFunction(val);
struct Node *myNode = hashTable[index].head;
if(!myNode)
{
printf("%d %d\n",-1,-1);
}
else
{
while (myNode->next!= NULL)
{
if (myNode->inputVal==copy)
{
boolean=1;
printf("%d %d\n",myNode->i1,myNode->j1);
break;
}
myNode = myNode->next;
}
if(myNode->inputVal==copy &&!boolean)
{
boolean=1;
printf("%d %d\n",myNode->i1,myNode->j1);
}
if(!boolean)
{
printf("%d %d\n",-1,-1);
}
}
}
return 0;
}
int hashFunction(long data)
{
return (int)(data%337);
}
Sample Input:
5 5
-993655555 -758584352 -725954642 -696391700 -649643547
-591473088 -568010221 -432112275 -421496588 -351507172
-323741602 -232192004 -30134637 -369573 100246476
156824549 174266331 392354039 601294716 763826005
768378344 802829330 818988557 992012759 999272829
10
156824549
-758584352
-993655555
601294716
-696391700
802829330
-993655555
-232192004
392354039
-568010221
Related
I have a homework that the teacher test if it's corrects by checking it's output using this website moodle.caseine.org, so to test my code the program execute these lines and compare the output with the expected one, this is the test :
Tas t = new Tas();
Random r = new Random(123);
for(int i =0; i<10000;i++)t.inser(r.nextInt());
for(int i =0;i<10000;i++)System.out.println(t.supprMax());
System.out.println(t);
And my Heap (Tas) class:
package td1;
import java.util.ArrayList;
import java.util.List;
public class Tas {
private List<Integer> t;
public Tas() {
t = new ArrayList<>();
}
public Tas(ArrayList<Integer> tab) {
t = new ArrayList<Integer>(tab);
}
public static int getFilsGauche(int i) {
return 2 * i + 1;
}
public static int getFilsDroit(int i) {
return 2 * i + 2;
}
public static int getParent(int i) {
return (i - 1) / 2;
}
public boolean estVide() {
return t.isEmpty();
}
#Override
public String toString() {
String str = "";
int size = t.size();
if (size > 0) {
str += "[" + t.get(0);
str += toString(0);
str += "]";
}
return str;
}
public boolean testTas() {
int size = t.size();
int check = 0;
if (size > 0) {
for (int i = 0; i < t.size(); i++) {
if (getFilsGauche(i) < size) {
if (t.get(i) < t.get(getFilsGauche(i))) {
check++;
}
}
if (getFilsDroit(i) < size) {
if (t.get(i) < t.get(getFilsDroit(i))) {
check++;
}
}
}
}
return check == 0;
}
public String toString(int i) {
String str = "";
int size = t.size();
if (getFilsGauche(i) < size) {
str += "[";
str += t.get(getFilsGauche(i));
str += toString(getFilsGauche(i));
str += "]";
}
if (getFilsDroit(i) < size) {
str += "[";
str += t.get(getFilsDroit(i));
str += toString(getFilsDroit(i));
str += "]";
}
return str;
}
//insert value and sort
public void inser(int value) {
t.add(value);
int index = t.size() - 1;
if (index > 0) {
inserCheck(index); // O(log n)
}
}
public void inserCheck(int i) {
int temp = 0;
int parent = getParent(i);
if (parent >= 0 && t.get(i) > t.get(parent)) {
temp = t.get(parent);
t.set(parent, t.get(i));
t.set(i, temp);
inserCheck(parent);
}
}
//switch position of last element is list with first (deletes first and return it)
public int supprMax() {
int size = t.size();
int max = 0;
if (size > 0) {
max = t.get(0);
t.set(0, t.get(size - 1));
t.remove(size - 1);
supprMax(0);
}
else {
throw new IllegalStateException();
}
return max;
}
public void supprMax(int i) {
int size = t.size();
int temp = 0;
int index = i;
if (getFilsGauche(i) < size && t.get(getFilsGauche(i)) > t.get(index)) {
index = getFilsGauche(i);
}
if (getFilsDroit(i) < size && t.get(getFilsDroit(i)) > t.get(index)) {
index = getFilsDroit(i);
}
if (index != i) {
temp = t.get(index);
t.set(index, t.get(i));
t.set(i, temp);
supprMax(index);
}
}
public static void tri(int[] tab) {
Tas tas = new Tas();
for (int i = 0; i < tab.length; i++) {
tas.inser(tab[i]);
}
for (int i = 0; i < tab.length; i++) {
tab[i] = tas.supprMax();
}
}
}
The last 3 lines of the test are :
-2145024521
-2147061786
-2145666206
But the last 3 of my code are :
-2145024521
-2145666206
-2147061786
The problem are probably with the inser and supprMax methods.
I hate to get a bad grade just because of 3 lines placement, because it is a program that verify the code, it dosn't care the the solution was close, it's still says it's wrong.
I am solving this challenge: https://open.kattis.com/problems/virtualfriends
My solution seems to be working but kattis's test cases are running too slowly so I was wondering how I can improve code efficiency. I am using a custom made union-find structure to do this, storing "friends" into a treemap to reference.
import java.util.*;
public class virtualfriends {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int testcases = scan.nextInt();
scan.nextLine();
for (int i= 0; i < testcases; i++) {
int numFriendships = scan.nextInt();
scan.nextLine();
TreeMap<String , Integer> map = new TreeMap<String , Integer>();
int cnt = 0;
UF unionFind = new UF(50000);
for (int j = 0; j < numFriendships; j++)
{
String p1 = scan.next();
String p2 = scan.next();
if (!map.containsKey(p1)) map.put(p1, cnt++);
if (!map.containsKey(p2)) map.put(p2, cnt++);
unionFind.unify(map.get(p1), map.get(p2));
System.out.printf("%d\n", unionFind.getSetSize(map.get(p2)));
}
}
}
static class UF{
private int[] id, setSize;
private int numSets;
public UF(int size) {
id = new int[size] ;
setSize = new int[size];
numSets = size;
for(int i = 0 ; i < size ; ++i) {
id[i] = i;
setSize[i] = 1;
}
}
int find(int i )
{
int root = i;
while (root != id[root]) {
root = id[root];
}
while (i != root) {
int newp = id[i];
id[i] = root;
i = newp;
}
return root;
}
boolean isConnected(int i , int j) {
return find(i) == find(j);
}
int getNumSets() {
return numSets;
}
int getSetSize(int i) {
return setSize[find(i)];
}
boolean isSameSet(int i, int j) {
return find(i) == find(j);
}
void unify(int i, int j)
{
int root1 = find(i);
int root2 = find(j);
if (root1 == root2) return;
if (setSize[root1] < setSize[root2])
{
setSize[root2] += setSize[root1];
id[root1] = root2;
} else {
setSize[root1] += setSize[root2];
id[root2] = root1;
}
numSets--;
}
}
}
I have a hard time to figure out one error after assigning
int evaluationNode = getMinDistances();
settled.add(evaluationNode);
checkNeighbours(evaluationNode);
The error show type mismatch: connot convert from Node to int. I am appreciated if anyone can help me this. Below is a complete code.
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Set;
import java.util.Comparator;
public class DijkstraPriorityQueue
{
private int distances[];
private Set<Integer> settled;
private PriorityQueue<Node> priorityQueue;
private int number_of_nodes;
private int adjacencyMatrix[][];
public DijkstraPriorityQueue(int number_of_nodes)
{
this.number_of_nodes = number_of_nodes;
distances = new int[number_of_nodes + 1];
settled = new HashSet<Integer>();
priorityQueue = new PriorityQueue<Node>(number_of_nodes,new Node());
adjacencyMatrix = new int[number_of_nodes + 1][number_of_nodes + 1];
}
public void dijkstra_algorithm(int adjacency_matrix[][], int source)
{
int evaluationNode;
for (int i = 1; i <= number_of_nodes; i++)
for (int j = 1; j <= number_of_nodes; j++)
adjacencyMatrix[i][j] = adjacency_matrix[i][j];
for (int i = 1; i <= number_of_nodes; i++)
{
distances[i] = Integer.MAX_VALUE;
}
priorityQueue.add(new Node(source, 0));
distances[source] = 0;
while (!priorityQueue.isEmpty())
{
evaluationNode = getMinDistances();
settled.add(evaluationNode);
evaluateNeighbours(evaluationNode);
}
}
private int getMinDistances()
{
int node = priorityQueue.remove();
return node;
}
private void checkNeighbours(int evaluationNode)
{
int edgeDistance = -1;
int newDistance = -1;
for (int destinationNode = 1; destinationNode <= number_of_nodes; destinationNode++)
{
if (!settled.contains(destinationNode))
{
if (adjacencyMatrix[evaluationNode][destinationNode] != Integer.MAX_VALUE)
{
edgeDistance = adjacencyMatrix[evaluationNode][destinationNode];
newDistance = distances[evaluationNode] + edgeDistance;
if (newDistance < distances[destinationNode])
{
distances[destinationNode] = newDistance;
}
priorityQueue.add(new Node(destinationNode,distances[destinationNode]));
}
}
}
}
public static void main(String[] args)
{
int adjacency_matrix[][];
int number_of_vertices;
int source = 0;
Scanner scan = new Scanner(System.in);
try
{
System.out.println("Enter the number of vertices");
number_of_vertices = scan.nextInt();
adjacency_matrix = new int[number_of_vertices + 1][number_of_vertices + 1];
System.out.println("Enter the Weighted Matrix for the graph");
for (int i = 1; i <= number_of_vertices; i++)
{
for (int j = 1; j <= number_of_vertices; j++)
{
adjacency_matrix[i][j] = scan.nextInt();
if (i == j)
{
adjacency_matrix[i][j] = 0;
continue;
}
if (adjacency_matrix[i][j] == 0)
{
adjacency_matrix[i][j] = Integer.MAX_VALUE;
}
}
}
System.out.println("Enter the source ");
source = scan.nextInt();
DijkstraPriorityQueue dijkstrasPriorityQueue = new DijkstraPriorityQueue(number_of_vertices);
dijkstrasPriorityQueue.dijkstra_algorithm(adjacency_matrix, source);
System.out.println("The Shorted Path to all nodes are ");
for (int i = 1; i <= dijkstrasPriorityQueue.distances.length - 1; i++)
{
System.out.println(source + " to " + i + " is " + dijkstrasPriorityQueue.distances[i]);
}
} catch (InputMismatchException inputMismatch)
{
System.out.println("Wrong Input Format");
}
scan.close();
}
}
class Node implements Comparator<Node>
{
public int node;
public int cost;
public Node()
{
}
public Node(int node, int cost)
{
this.node = node;
this.cost = cost;
}
#Override
public int compare(Node node1, Node node2)
{
if (node1.cost < node2.cost)
return -1;
if (node1.cost > node2.cost)
return 1;
return 0;
}
}
In the getMinDistances method you are calling
int node = priorityQueue.remove();
But the priority queue contains Node objects, and not int values.
Maybe you wanted something like
private int getMinDistances()
{
Node node = priorityQueue.remove();
return node.getDistance();
}
but this is something that can not be answered by the debugging cloud (aka stackoverflow).
In order to use the PriorityQueue, you have to implement MinPQ extending the PriorityQueue and you'll also need a Comparator between nodes that returns the Node with a minimum distance in the MinPQ.
Take a look here for more details.
I am trying to implement Karger's min cut algorithm but I am unable to get the correct answer. Can someone please have a look at my code and help me figure out what I am doing wrong? Would really appreciate the help.
package a3;
import java.util.*;
import java.io.*;
public class Graph {
private ArrayList<Integer>[] adjList;
private int numOfVertices = 0;
private int numOfEdges = 0;
public Graph(String file) throws IOException {
FileInputStream wordsFile = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(wordsFile));
adjList = (ArrayList<Integer>[]) new ArrayList[201];
for (int i = 1; i < 201; i++) {
adjList[i] = new ArrayList<Integer>();
}
while (true) {
String s = br.readLine();
if (s == null)
break;
String[] tokens = s.split("\t");
int vertex = Integer.parseInt(tokens[0]);
this.numOfVertices++;
for (int i = 1; i < tokens.length; i++) {
Integer edge = Integer.parseInt(tokens[i]);
addEdge(vertex, edge);
this.numOfEdges++;
}
}
}
public void addEdge(int v, int w) {
adjList[v].add(w);
//this.numOfEdges++;
}
public ArrayList<Integer> getNeighbors(Integer v) {
return adjList[v];
}
public boolean hasEdge(Integer i, Integer j) {
return adjList[i].contains(j);
}
public boolean removeEdge(Integer i, Integer j) {
if (hasEdge(i, j)) {
adjList[i].remove(j);
adjList[j].remove(i);
this.numOfEdges -= 2;
return true;
}
return false;
}
public int getRandomVertex(){
Random rand = new Random();
return (rand.nextInt(this.getNumOfVertices()) + 1);
}
//Returns an array which consists of vertices connected by chosen edge
public int[] getRandomEdge(){
int arr[] = new int[2];
arr[0] = this.getRandomVertex();
while (adjList[arr[0]].size() == 0) {
arr[0] = this.getRandomVertex();
}
Random rand = new Random();
arr[1] = adjList[arr[0]].get(rand.nextInt(adjList[arr[0]].size()));
return arr;
}
//Algorithm for min cut
public int minCut() {
while (this.getNumOfVertices() > 2) {
int[] edge = this.getRandomEdge();
this.removeEdge(edge[0], edge[1]);
//Adding edges of second vertex to first vertex
for (Integer v: adjList[edge[1]]) {
if (!adjList[edge[0]].contains(v)) {
addEdge(edge[0], v);
}
}
//Removing edges of second vertex
for (Iterator<Integer> it = adjList[edge[1]].iterator(); it.hasNext();) {
Integer v = it.next();
it.remove();
this.numOfEdges--;
}
//Removing self-loops
for (Iterator<Integer> it = adjList[edge[0]].iterator(); it.hasNext();) {
Integer v = it.next();
if (v == edge[0])
it.remove();
//this.numOfEdges--;
}
this.numOfVertices--;
}
return this.numOfEdges;
}
public int getNumOfVertices() {
return this.numOfVertices;
}
public int getNumOfEdges() {
return (this.numOfEdges) / 2;
}
public String toString() {
String s = "";
for (int v = 1; v < 201; v++) {
s += v + ": ";
for (int e : adjList[v]) {
s += e + "-> ";
}
s += null + "\n";
//s += "\n";
}
return s;
}
/**
* #param args
* #throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int min = 1000;
//Graph test = new Graph("C:\\Users\\UE\\Desktop\\kargerMinCut.txt");
for (int i = 0; i < 10; i++) {
Graph test = new Graph("C:\\Users\\UE\\Desktop\\kargerMinCut.txt");
int currMin = test.minCut();
min = Math.min( min, currMin );
}
System.out.println(min);
}
}
There seems to be a problem in add method of the class I have written.. I want to make a SortedList using an array, but I can't figure out what the problem is. This is my code:
public class SortedList {
private Integer[] elements;
private int size;
private int capacity;
public SortedList(int cap) {
elements = new Integer[cap];
if (cap > 0)
{
cap = capacity;
}
else
capacity = 10;
}
public boolean isEmpty()
{
return size == 0;
}
public boolean isFull()
{
return size == capacity;
}
public int size()
{
return size;
}
public void doubleCapacity()
{
capacity = capacity * 2;
}
public void add(Integer el)
{
if(this.isEmpty())
{
elements[0] = el;
size++;
}
else if(this.isFull())
{
this.doubleCapacity();
for(int i = 0; i<this.size(); i++)
{
if(el >= elements[i])
{
elements[i+2] = elements[i+1];
elements[i+1] = el;
}
else
{
elements[i+1] = elements[i];
elements[i] = el;
}
}
size++;
}
else
{
for(int i = 0; i<this.size(); i++)
{
if(el >= elements[i])
{
elements[i+2] = elements[i+1];
elements[i+1] = el;
}
else
{
elements[i+1] = elements[i];
elements[i] = el;
}
}
size++;
}
}
public String toString()
{
String s = "";
s = s + "<SortedList[";
for(int i = 0; i < this.size(); i++)
{
s = s + elements[i];
if(i < this.size()-1)
s = s + ",";
}
s = s + "]>";
return s;
}
public static void main(String[] args)
{
SortedList sl = new SortedList(5);
sl.add(3);
//sl.add(2);
sl.add(4);
sl.add(5);
// sl.add(6);
System.out.println(sl.toString());
}
}
My code works if I only add 2 Integers to my list, but when I try to add the numbers 3,4,5 then I get 3,5,5...
What can be the problem? Thanks..
public class SortedList {
private Integer[] elements;
private int size=0;
private int capacity;
public SortedList(int cap) {
elements = new Integer[cap];
if (cap > 0)
{
capacity = cap;
}
else
capacity = 10;
}
public boolean isEmpty()
{
return size == 0;
}
public boolean isFull()
{
return size == capacity;
}
public int size()
{
return size;
}
public void doubleCapacity()
{
capacity = capacity * 2;
}
public void add(Integer el) throws Exception{
elements[size] = el;
size++;
if(size>capacity){
throw new Exception("Size Exceeded");
}
}
public String toString()
{
sort();
String s = "";
s = s + "<SortedList[";
for(int i = 0; i < this.size(); i++)
{
s = s + elements[i];
if(i < this.size()-1)
s = s + ",";
}
s = s + "]>";
return s;
}
public void sort(){
for (int i=0; i <size()-1; i++) {
if (elements[i] > elements[i+1]) {
// exchange elements
int temp = elements[i];
elements[i] = elements[i+1];
elements[i+1] = temp;
}
}
}
public static void main(String[] args)
{
try {
SortedList sl = new SortedList(5);
sl.add(3);
//sl.add(2);
sl.add(6);
sl.add(5);
// sl.add(6);
System.out.println(sl.toString());
} catch (Exception ex) {
Logger.getLogger(SortedList.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Your insertion code doesn't work.
elements[i+1] = elements[i];
elements[i] = el;
What happens to the old value of elements[i+1]?
I'd recommend the following changes to the previous solution. If you're only calling sort in toString(), your list is going to get out of order quickly in cases where you have multiple unsorted elements in a row (Now you could remove sort() from toString()). It's essentially a quick insertion sort that dies as soon as it can't make any more swaps down the list. Again, as dty suggested, a faster choice would be a binary search to find the insertion point.
public void doubleCapacity(){
capacity = capacity * 2;
Integer temp[] = new Integer[capacity];
for (int i = 0; i < size; i++){
temp[i] = elements[i];
}
elements = temp;
}
public void add(Integer el){
if(size+1>capacity){
doubleCapacity();
}
elements[size] = el;
size++;
sort();
}
public void sort(){
//Iterates down the list until it's sorted.
for (int i=size()-2; i >= 0 && (elements[i] < elements[i+1]); i--) {
// exchange elements
int temp = elements[i];
elements[i] = elements[i+1];
elements[i+1] = temp;
}
}