Pyramid pattern using java with number - java

How to write a java program to print below the pattern?
1
2 2
3 3
2 2
1

I think this is what are you looking for:
System.out.println("\t 1\n\t 2 2\n\t3 3\n\t 2 2\n\t 1 ");

Related

Can one turn all the light bulbs with m switches or not

Given n bulbs, initially, all bulbs are off and m switches, each switch controls a range of light bulbs l to r (both inclusive)
We neeed to determine if it is possible to switch on all the bulbs using any of m switches any no of time.
The answer can be yes if one can or can be no if one cant.
E.g first given n then m and then next m lines give the range which the switch controls. In the following example n = 5 and m = 2.
5 2
1 2
3 5
here answer is yes because 2 switches can control all the bulbs. and can be turned on.
Second example
5 2
1 2
3 4
here the answer is no because one of the bulbs is not controlled by any switch.
What I did is declare an array count with size m and for each switch I counted how many bulbs it control by (r-l +1) if the sum of count is equal to n then yes else no.
But only sample test cases are passed rest all fails.
Note: Ranges might intersect
If ranges intersect for example
1 3
3 5
The answer is no because when one turn the second switch on the 3 rd bulb flip from on to off
So the answer will be no.
We can see this problem as an instance XOR-SAT problem, though it is more general than the problem posed here, that's one way to go.
Just to gain some intuition I provide a very simple example. Suppose you have systems of three switches and three bulbs like this:
S B
1 1, 3 // toggles bulbs 1 and 3
2 1, 2
3 1, 2, 3
It is equivalent to have the following formula, which we want to satisfy:
(x1^x2^x3)&(x1^x2)&(x1^x3).
And now we want to satisfy this formula. We start with writing it as system of boolean equations modulo 2:
|1 1 1| |x_1| |1|
|0 1 1| * |x_2| = |1| mod 2
|1 0 1| |x_3| |1|
Now solve it with Gaussian elimination.
First, add the first and the second rows to the third:
1 1 1 1 1 1 1 1
0 1 1 1 -> 0 1 1 1
1 0 1 1 0 0 1 1 // for RHS, 1+1+1 = 1 mod 2
Second, back-substitute: x1 = 0, x2 = 0, x3 = 1, which is obviously the answer.
So, the main complexity here is to program Gaussian elimination process.

How to print all possible sequences with intermixed sequence of push and pop operation

Suppose that an intermixed sequence of push and pop operations are performed on a LIFO stack.How to print all possible sequences? I can just judge it's about recursion. For example, if order 1 2 3 is given, output is
1 2 3
1 3 2
2 1 3
2 3 1
3 2 1
Use Google GUAVA's method https://google.github.io/guava/releases/19.0/api/docs/com/google/common/collect/Collections2.html#orderedPermutations(java.lang.Iterable) to get all possible permutations and then for each permutation reverse the order using https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#reverse(java.util.List)

Why does an index in Quick-Union Weighted remain size 1 when merged with a bigger tree?

I've been looking into algorithms using a class on coursera. In one of the first lectures, Quick Union Weighted is being discussed. I get what it does and I've tested it out using their code and written a small test for it.
Everything is clear but one point: when you create a union of two objects, it will add the object with the smallest tree to the bigger one. At the same time, the size of the larger tree will be incremented with the size of the smaller tree in a separate array which is used to determine what tree is bigger. Since the array is initiated with value 1 for every index (every node on its own basically is a tree of 1 object), why isn't the value of this index set to 0 instead of remaining on 1?
In order to illustrate this:
// Quick Union Weighted
ID: 0 1 2 3 4 5 6 7 8 9
SZ: 1 1 1 1 1 1 1 1 1 1
quw.union(2, 4);
ID: 0 1 2 3 2 5 6 7 8 9
SZ: 1 1 2 1 1 1 1 1 1 1
quw.union(5, 4);
ID: 0 1 2 3 2 2 6 7 8 9
SZ: 1 1 3 1 1 1 1 1 1 1
quw.union(2, 7);
ID: 0 1 2 3 2 2 6 2 8 9
SZ: 1 1 4 1 1 1 1 1 1 1
// Whereas I would've expected to end up with this
// to point out that the index is empty.
SZ: 1 1 4 1 0 0 1 0 1 1
Why are the sizes of merged indices 1 instead of 0?
You can find the code to test it out here. Note that the implementation is the same as the example provided by the lecturers, which is why I'm assuming my code is correct.
I think this is because the node itself is also size 1 and does not have any children. It can however have children. I'm actually not familiar with Quick-Union Weighted but if it's bit like the other union find algoritmes I've seen you can for example do
quw.union(0, 1);
ID: 0 0 2 3 2 2 6 2 8 9
SZ: 1 1 4 1 1 1 1 1 1 1
quw.union(0, 2);
ID: 2 2 2 3 2 2 6 2 8 9
SZ: 2 1 6 1 1 1 1 1 1 1
So now 0 en 1 have merged and the entire tree starting from 0 is merged with 2 again, still making the subtree starting at 0 size 2.
Like I said, I'm not sure it that's possible in Quick-Union Weighted but the reason for the '1' is still because it's also size 1 on its own.

Multithreaded search of single collection for duplicates

use I can't divide into segads. As for my above example if 5 threads are set, then first segment would take 2 first object, and second 3th and 4th, so they dont find dups, but there are dups if we merge them, its 2th and 3th.
There could be more complex strate take from first threads .. ah nevermind, to hard to explain.
And ofcourse, problelection itself in my plans.
Tha
EDIT:
InChunk, and then continue analyzing that chunk till the end. ;/
I think the process of dividing up the items to be de-duped is going to have to look at the end of the section and move forward to encompass dups past it. For example, if you had:
1 1 2 . 2 4 4 . 5 5 6
And you dividing up into blocks of 3, then the dividing process would take 1 1 2 but see that there was another 2 so it would generate 1 1 2 2 as the first block. It would move forward 3 again and generate 4 4 5 but see that there were dups forward and generate 4 4 5 5. The 3rd thread would just have 6. It would become:
1 1 2 2 . 4 4 5 5 . 6
The size of the blocks are going to be inconsistent but as the number of items in the entire list gets large, these small changes are going to be insignificant. The last thread may have very little to do or be short changed altogether but again, as the number of elements gets large, this should not impact the performance of the algorithm.
I think this method would be better than somehow having one thread handle the overlapping blocks. With that method, if you had a lot of dups, you could see it having to handle a lot more than 2 contiguous blocks if you were unlucky in the positing of the dups. For example:
1 1 2 . 2 4 5 . 5 5 6
One thread would have to handle that entire list because of the 2s and the 5s.
I would use a chunk-based division, a task queue (e.g. ExecutorService) and private hash tables to collect duplicates.
Each thread in the pool will take chunks on demand from the queue and add 1 to the value corresponding to the key of the item in the private hash table. At the end they will merge with the global hash table.
At the end just parse the hash table and see which keys have a value greater than 1.
For example with a chunk size of 3 and the items:
1 2 2 2 3 4 5 5 6 6
Assume to have 2 threads in the pool. Thread 1 will take 1 2 2 and thread 2 will take 2 3 4. The private hash tables will look like:
1 1
2 2
3 0
4 0
5 0
6 0
and
1 0
2 1
3 1
4 1
5 0
6 0
Next, thread 1 will process 5 5 6 and thread 2 will process 6:
1 1
2 2
3 0
4 0
5 2
6 1
and
1 0
2 1
3 1
4 1
5 0
6 1
At the end, the duplicates are 2, 5 and 6:
1 1
2 3
3 1
4 1
5 2
6 2
This may take up some amount of space due to the private tables of each thread, but will allow the threads to operate in parallel until the merge phase at the end.

Path finding in java / prolog using A*Star

I am supposed to start for example from point 1B to 5D. how am i supposed to reach? anyone can gv me a hint to get start on this?thanks. not asking for codes but the tips/ hints. thanks.
A B C D E
1 5 1 4 4 1
2 3 4 3 3 4
3 4 3 1 1 3
4 4 3 4 2 5
5 3 4 1 1 3
Start by defining data structures , and apply algorithm with it
You may refer to pseudo-code on wiki: Wiki A Star Search Algorithm

Categories