HashMaps - Unclear on the hash function and double hashing - java

I have been asked to implement a hash map using an array. I need to insert the following keys:
15, 7, 26, 39, 11, 9, 27, 5, 18, 2, 54, 22, 4
into an array of size 19 using the hash function:
(3x + 7) % 19
Using linear probing, I would expect to get the following array (correct me if I'm wrong):
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Key: 4 11 5 18 7 26 39 27 2 15 9 22 54
where 26 had a collision with 7 at index 9, and so was inserted at index 10, and 39 then had a collision with 26 at index 10 and so was inserted at index 11.
I am now attempting to insert the same elements in an array implementation of a HashMap, using double hashing instead of linear probing. The 2nd hash function I am given is:
11 - (x % 11)
I have two questions:
Does this mean that my array will be of size 11 or still 19?
Do I initially use the original hash function and if the given index is free, insert the element there, otherwise if there is a collision, use the 2nd hash function and insert the element there?

According to Wikipedia the secondary hash function is used indirectly in the probing function:
h(0, x) = (3x + 7) % 19
h(j, x) = ((3x + 7) + j(11 - (x % 11))) % 19
Where j is the collision counter.

Related

smallest possible sum of the values of subarrays

I'm trying to understand question and solving it using java.
But first I'm not able to understand properly.
Here is the question:
You are given an array a of length n and an integer c.
The value of some array b of length k is the sum of its elements except for the smallest. For example, the value of the array [3, 1, 6, 5, 2] with c = 2 is 3 + 6 + 5 = 14.
Among all possible partitions of a into contiguous subarrays output the smallest possible sum of the values of these subarrays.
Input
The first line contains integers n and c (1 ≤ n, c ≤ 100 000).
The second line contains n integers ai (1 ≤ ai ≤ 109) — elements of a.
Output
Output a single integer — the smallest possible sum of values of these subarrays of some partition of a.
Examples
inputCopy
3 5
1 2 3
output
6
inputCopy
12 10
1 1 10 10 10 10 10 10 9 10 10 10
output
92
inputCopy
7 2
2 3 6 4 5 7 1
output
17
inputCopy
8 4
1 3 4 5 5 3 4 1
output
23
In the third example one of the optimal partitions is [2, 3], [6, 4, 5, 7], [1] with the values 3, 13 and 1 respectively.
My Understanding:
1) Partition is being being done within continuous numbers. Correct ?
2) What is the significance of Integer c in input ?
3) How is being done in third example ? I mean after having subarrays, How 13 came out from second subarray ?
Can anyone help me to understand the question ? I can write code myself.

I want to render data column wise in table in itext 7.0.1

Generally in table cells go row wise but I want to render it column wise.
Data is stored in an array list.
----------- means page break
Expectation is:
1 6
2 7
3 8
4 9
5 10
-------------
11 16
12 17
13 18
But generally it comes like this
1 2
3 4
5 6
7 8
9 10
-------------
11 12
13 14
15 16
17 18

Print triangle of numbers in java

I've been having some trouble with a homework assignment for my Java class. In it, we're supposed to take in an integer between 1 and 13 and display three different triangles consisting of numbers. For example, if I were to enter 5, the result would be:
Triangle 1
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Triangle 2
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
Triangle 3
5
4 9
3 8 12
2 7 11 14
1 6 10 13 15
I've already got the first Triangle going fine, but my big concern is the second triangle. I haven't attempted the third one yet. The other thing is that my Professor is picky about what method we use in creating the project. In other words, we can only use what he has taught us. He told us to use the System.out.printf("%3d", n) statement to space out the characters and we have to create them within a separate class.
The code for the first triangle is as follows:
void triangle1(int n)
{
int k = 1;
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < 1; j++)
{
System.out.printf("%3d", n);
k += 1;
}
System.out.println();
}
}
So, pretty much, I need to follow that standard to create the other two triangles, but I'm really stuck on the second one and I don't know where to start. Any help will be much appreciated!
Here is the way I would approach it.
Programs print one line at a time, you cannot print half a line then start to print another line.
With that being said, you should recognize the pattern in the triangles.
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
You have the first number n, then you see the next row starts with n + 1. The next number starts in the row is (n + 1) + t where t = 4. There is a pattern there.
The third row follows the same pattern.
The first number is (n + 1) then you can calculate the others by + (t - 1)
This can be done with a for loop, like you did in the first time.
For the last triangle you can use the same process, just change the signs and t would equal something different.
Algorithm writing is all about identifying patterns.
If you look closely, you'll see there's a repeating pattern between each number and the one that follows on a given line.
3 7 10 => [3 & 7 differ by 4][7 & 10 differ by 3]
4 8 11 13 => [4 & 8 differ by 4][8 & 11 differ by 3][11 & 13 differ by 2]
5 9 12 14 15 => [... differ by 4][... by 3][... by 2][... by 1]
You can use that information to make the second triangle. I'll leave the rest to you. I hope that helps!
Seems you are a CS-Student, thus I will not present a finished solution. I'll give you some hints how I would solve this.
This is what the print statement has to do:
for i=1 j=0 print 1
for i=2 j=0 print 2
for i=2 j=1 print 6
for i=3 j=0 print 3
for i=3 j=1 print 7
for i=3 j=2 print 10
Find a formula that calculates the correct output from i and j, its a simple linear combination.

What was wrong with my logic on Java insertion sorts?

I had a question on a test - I got it wrong, but I don't know why?
Question: An array of integers is to be sorted from biggest to smallest using an insertion sort. Assume the array originally contains the following elements:
5 9 17 12 2 14
What will it look like after the third pass through the for loop?
17 9 5 12 2 14
17 12 9 5 2 14 - Correct answer
17 12 9 5 14 2
17 14 12 9 5 2
9 5 17 12 2 14
In an insertion sort, I thought the source array is untouched; I felt, at the third pass, the destination array would be incomplete.
How did the test arrive at this answer?
Basically, after n passes of insertion sort, the first n+1 elements are sorted in the correct order and the rest are untouched. As you can see in the alternatives, the correct answer is the only one that fulfills that. Every step is only inserting relative to the already sorted numbers, so each step one extra number is correctly sorted.
Step 0 (Original, 5 assumed sorted)
5 9 17 12 2 14
Step 1, takes the 9 and puts it in the correct place before 5 (result, 9 5 sorted)
9 5 17 12 2 14
Step 2, takes the 17 and puts it in the correct place before 9 (result 17 9 5 sorted)
17 9 5 12 2 14
Step 3, takes the 12 and puts it in the correct place after 17 and before 9 (result 17 12 9 5sorted)
17 12 9 5 2 14
Insertion sort is typically done in-place. After k iterations the first k + 1 elements are sorted, hence the answer you've shown above.

Java sort array based on three column

On my quest learning Java I come across one doubt.
For sorting a unidimensional array we can use Arrays.sort() but if I want to sort a bidimensional array based on three columns? Is there any option to d that or do I have to code it for my self (something like three nested loops)?
Here is an example input:
13 2 28 36
1 4 56 17
4 2 5 40
2 4 41 55
9 5 48 12
19 2 25 12
20 5 13 8
15 3 51 30
12 5 39 59
17 3 56 40
3 1 56 46
7 3 28 51
8 5 14 58
5 3 34 15
14 4 53 2
18 4 38 57
6 2 16 25
16 3 17 13
10 5 41 33
11 1 13 57
Columns are int and this is stored in an array of ints.
I wanna sort by column 2, if equal numbers are found, then sort by column 3 and finally, if equals found, sort by column 3.
The output should be this:
11 1 13 57
3 1 56 46
4 2 5 40
6 2 16 25
19 2 25 12
13 2 28 36
16 3 17 13
7 3 28 51
5 3 34 15
15 3 51 30
17 3 56 40
18 4 38 57
2 4 41 55
14 4 53 2
1 4 56 17
20 5 13 8
8 5 14 58
12 5 39 59
10 5 41 33
9 5 48 12
Is there a simple way for doing this?
Remember that I'm new at Java.
regards,
Favolas
Simple use Comparator and use Arrays.sort(arr, comparator);
It depends on how you get the data.
In general, you could give your own Comparator to Collections.sort.
Use the comparator below
class MyArrayComparator implements Comparator<Integer[]> {
#Override
public int compare(Integer[] o1, Integer[] o2) {
if (o1[1] == o2[1]) {
if (o1[2] == o2[2]) {
return o1[3].compareTo(o2[3]);
}
else {
return o1[2].compareTo(o2[2]);
}
}
return o1[1].compareTo(o2[1]);
}
}
Use the following sort method
Collections.sort(yourListOfArry, new MyArrayComparator());
Implementing an Comparator on arrays of arrays seems kind of dangerous to me because you have to ensure manually, that all arrays in the second dimension are of the same length and the semantics of the components of the array must be the same at all times - for these kind of constraints objects should be used.
If the second dimension in the array has some meaning (i.e. it is always four becouse three are coordinates and the fourth is the value), you should consider not to model it as an array of arrays but an array of objects with four member variables (e.g. xCoord, yCoord, zCoord and value). You can then make this class implement the Comparable interface and implement the compareTo method. Then the Arrays.sort() method will sort according to the given compareTo method.

Categories