Finding pair arrangement in Go - algorithm

Here I'm trying to form an arrangement that contains pairs of numbers that each pair of m's are separated by m elements. for example:
for [0,2], the pair arrangement is [2,0, 0,2] such that m=2, hence the number 2 is separated by 2 elements.
for [0,1] = there is no valid arrangement
I still can't figure out the pattern or algorithm for the arrangement as I need to find the arrangement up to [0,1,2,3,4,5,6,7,8]. however the valid arrangement for this list is [3,7,8,2,3,1,2,1,6,7,5,8,4,0,0,6,5,4] by doing it manually.
In the codes below, I could only rearrange the numbers in the list by getting the largest number in the list first. I want to know how to separate the pair according to the number of the pair (e.g if the pair is 2, hence separation number is 2)
how can i do the separation and pattern for the list of numbers?
package main
import "fmt"
func MagicPairs(list []int) {
//length := len(list) * 2
magicPair := []int{}
magicPair = append(list, list...)
for i := 0; i <len(magicPair); i++{
if len(magicPair) == 0 {
//do nothing
}else{
m := max(list)
for p, x := range magicPair{
if magicPair[len(magicPair)-1] == m {
magicPair = append([]int{m}, (magicPair)[:len(magicPair)-1]...)
fmt.Println(magicPair)
return
}
if x == m{
magicPair = append([]int{m}, append((magicPair)[:p], (magicPair)[p+1:]...)...)
}
previousPair := magicPair[x]
if x == previousPair{
}
}
}
}
fmt.Println("1", magicPair)
}
func max(list[] int) int{
max := list[0]
for _, value := range list{
if value > max {
max = value
}
}
return max
}
func main(){
list := [] int {0,1,2,3,4,5,6,7,8}
MagicPairs(list)
}

You seem to try to find the optimal solution by doubling up the source list and then shuffling the numbers around by repeatedly slicing and concatenating the array.
I think this problem lends itself to a recursive approach. Create thze target array with 2 * len(list) empty slots. (Whether a slot is empty or not must be marked with a special value, for example -1.) Then recursively try to fit the elements of the original array into the target array.
Let's look at your example {0, 1, 3}. Create the target array:
. . . . . .
Try all possible poitions for the 0. The first is
0 0 . . . .
Now try to fit the 1. There are two possibilities
0 0 . . . .
0 0 1 . 1 .
but that won't accomodate the next element, 3. Go back one step:
0 0 . . . .
0 0 . 1 . 1
The 3 won't fit in here, either. We've exhausted our search for that position of zeros, so let's take the next viable position of zeros:
. 0 0 . . .
There's only one way to place the one:
. 0 0 . . .
. 0 0 1 . 1
Now let's try to fit the 3 and, bingo!, it fits:
. 0 0 . . .
. 0 0 1 . 1
3 0 0 1 3 1
Now you can stop the search or try to find other solutions. In this case, there's only one other solution, namely the reflection of this one, but there are 300 ways to place the numbers from 1 to 8, foe example.
That approach is pretty much brute force, but in practice, there aren't many valid ways to fill the array, so that wrong paths are detected early. Perhaps placing the big numbers first gives better performance. You can play with that and measure it.
Here's a program that does that. (It probably looks more like C than Go. never mind.)
package main
import "fmt"
func fit_r(res[] int, a[] int, i int) int {
n := len(a);
if i == n {
fmt.Printf("%v\n", res);
return 1;
} else {
count := 0;
m := a[i];
for j := 0; j < 2*n - 1 - m; j++ {
if res[j] == -1 && res[j + 1 + m] == -1 {
// place values
res[j] = m;
res[j + 1 + m] = m;
// test further values
count += fit_r(res, a, i + 1);
// clean up and remove values again
res[j] = -1;
res[j + 1 + m] = -1;
}
}
return count;
}
}
func fit(a[] int) int {
res := make([] int, 2 * len(a));
for i := range res {
res[i] = -1;
}
return fit_r(res, a, 0);
}
func main() {
list := [] int {0, 1, 2, 3};
n := fit(list);
fmt.Println(n, "solutions");
}

Related

last iteration gets skipped and not printed in a for loop

Im trying to build a code that when the user inputs a sequence of numbers it will go through the sequence comparing each numbers and for every new biggest number in the sequence it will sum all the previous ones
func main() {
var numeri []int
numeri = GetInputSlice()
fmt.Println(numeri)
var sum int
num := len(numeri)
for i := 0; i < num - 1 ; i++ {
sum += numeri[i]
if numeri[i] > numeri[i+1] || numeri[i] == num - 1 {
fmt.Println(sum)
sum = 0
}
}
}
full code over here: https://go.dev/play/p/13ljQPmKaRA
if I input this sequence of numbers [1 2 13 0 7 8 9 -1 0 2] I would like to get 16, 24 and 1.
But in my code I only get 16 and 24 without getting the last 1 and I can't figure out a way to fix this.
Only numeri[i] is ever added to sum, and your loop never visits the last item (i < num - 1), so how could the last item be ever added?
Range through the whole slice, perform the addition, but only compare to the next element if you're not at the last one. If we're at the last one, we also want to print, so we may use a single condition
i == max || numeri[i] > numeri[i+1]
Where the comparison to the next element will not be executed if i == max (short circuit evaluation).
For example:
max := len(numeri) - 1
for i, v := range numeri {
sum += v
if i == max || v > numeri[i+1] {
fmt.Println(sum)
sum = 0
}
}
This will output (try it on the Go Playground):
[1 2 13 0 7 8 9 -1 0 2]
16
24
1

Recursion in Go for coding question: Product Sum

I've been practicing coding interview questions with this current example of Product Sum with Go. Basically, you need to take a nested array and return it's product sum.
Example: [1,3,[2,[5],-3],7] = 1 + 3 + 2*(2-3) + 3*(5) + 7 = 24
Which should also be equal to: 1 + 3 + 2*(2) + 2*(-3) + 3*5 + 7 = 24
However, when I try implementing this in code I can only get the first example.
func ProductSum(array []interface{}) int {
sum := productSum(array, 1)
fmt.Println(sum)
return sum
}
func productSum(array SpecialArray, multiplier int) int {
sum := 0
for _, el := range array {
if cast, ok := el.(SpecialArray); ok {
sum += productSum(cast, multiplier+1)
} else if cast, ok := el.(int); ok {
sum += cast
}
}
return sum * multiplier
}
If I change sum += cast to sum += cast * multiplier, and change return sum * multiplier to return sum - then the function doesn't work as expected. I've tried working through the recursive stack on this but am still confused.
According to your code the "array notation" should be transformed to following equation:
[1,3,[2,[5],-3],7] = 1*(1 + 3 + 2*(2 + 3 * (5) - 3) + 7) = 1 + 3 + 2*(2 - 3 + 15) + 7 = 11 + 14 * 2 = 39
or
[1,3,[2,[5],-3],7] = 1*(1 + 3 + 2*(2 + 3 * (5) - 3) + 7) = 1 + 3 + 2*(-1) + 6 * 5 + 7 = 11 - 2 + 30 = 39
In your interpretation each level of nesting increases the multiplier by one.
Could you paste the exact content of this exercise?
Edit:
To get 24 in result your code should look like this:
func ProductSum(array []interface{}) int {
sum := productSum(array, 1)
fmt.Println(sum)
return sum
}
func productSum(array []interface{}, multiplier int) int {
sum := 0
for _, el := range array {
if cast, ok := el.([]interface{}); ok {
sum += productSum(cast, multiplier+1)
} else if cast, ok := el.(int); ok {
// Multiplier is applied only to integers in current slice.
sum += multiplier * cast
}
}
return sum
}
The sum of an array is always multiplied by the its level of nesting.

Convert a hexadecimal number to binary in Go and be able to access each bit

I am fiddling around with Go at the moment and have stumpled upon a problem where I want to get some feedback and help :)
My problem is that I have a string containing a hexadecimal value as input, such as this:
"60A100"
Now, I want to convert this to the binary representation of the number and be able to look at specific bits within.
My solution to this right now is:
i, err := strconv.ParseUint(rawHex, 16, 32)
if err != nil {
fmt.Printf("%s", err)
}
// Convert int to binary representation
// %024b indicates base 2, padding with 0, with 24 characters.
bin := fmt.Sprintf("%024b", i)
The variable bin now holds exactly what I want, except it is a string which I don't think is optimal. I would rather that I could have an array of the individual bits such that I could just choose index i to get bit number i :)
Because as far as I know right now, if I lookup index 8 like so; bin[8], I will get a decimal that corresponds to the binary number, in the ASCII table.
I have searched quite a bit, but I can't find a solution that fits perfectly, but maybe I am looking in the wrong spot.
I hope you guys can guide me to the correct / optimal solution in this case :)
Thanks in advance!
You could turn it into a slice representing bits
// This could also return []bool
func asBits(val uint64) []uint64 {
bits := []uint64{}
for i := 0; i < 24; i++ {
bits = append([]uint64{val & 0x1}, bits...)
// or
// bits = append(bits, val & 0x1)
// depending on the order you want
val = val >> 1
}
return bits
}
func main() {
rawHex := "60A100"
i, err := strconv.ParseUint(rawHex, 16, 32)
if err != nil {
fmt.Printf("%s", err)
}
fmt.Printf("%024b\n", i)
fmt.Println(asBits(i))
}
OUTPUT
011000001010000100000000
[0 1 1 0 0 0 0 0 1 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0]
https://play.golang.org/p/KK_AUPgbZu
As #jimb points out, you can also just check an individual bit
fmt.Printf("9th bit is set? %t\n", (i >> 8) & 1 == 1)
which is what #n-carter's answer does.
After parsing the value you can directly access each bit. You can use something like this:
func getNthBit(val, n uint32) int {
n = 32 - n
if 1 << n & val > 0 {
return 1
}
return 0
}
Following #n-carter answer, you can access each bit individually
There are two approaches:
Option 1: Shifting the value:
Shift the bin number to the right n possitions to get the n-th bit the first one. then mask it with 1
func getNthBit(val, n uint32) int {
// 1. reverse the golang endian
nthBit := 32-n
// 2. move the nth bit to the first position
movedVal := val >> nthBit
// 3. mask the value, selecting only this first bit
maskedValue := movedVal & 1
return maskedValue
// can be shortened like so
// return (val >> (32-n)) & 1
}
Explanation:
1. Get the right bit index according to the endian
01100000101000010000000001000101
^
(32-3)=29nth bit
2. Shift the bits to get n-th in the first possition
01100000101000010000000001000101 >> 29
^^^
00000000000000000000000000000011
^^^
3. Mask first bit. This picks(extracts) the value from this bit
00000000000000000000000000000011
& ^
00000000000000000000000000000001
1
Option 2: shifting 1 and masking with it
This can be done the way #n-carter does. Shift a 1 to the left
func getNthBit(val, n uint32) int {
// 1. reverse the golang endian
nthBit := 32-n
// 2. move the mask 1 bit to the nth position
mask := 1 << nthBit
// 3. mask the value, selecting only this nth bit
maskedValue := val & mask
if maskedValue == 0 {
return 0
}
return 1
// can be written shorter like:
//if val & (1 << (32-n)) == 0 {
// return 0
//}
//return 1
}
Explanation:
1. Get the right bit index according to the endian
01100000101000010000000001000101
^
(32-3)=29nth bit
2. Shift the 1 to the n-th position (1 << 29 == 2^(29-1))
00000000000000000000000000000001 << 29
00100000000000000000000000000000
3. Mask n-th bit. This picks(extracts) the value from this bit
01100000101000010000000001000101
&
00100000000000000000000000000000
1
Hope this helps. It takes some time to visualise bit operations in your head.

Golang code too slow for Hackerrank

I've been trying to solve this Hackerrank challenge: Link
This is what you have to do:
You have one large matrix:
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 0 1 1
and one small matrix:
1 1 1
1 1 1
1 1 0
You have to find out if the small matrix is present in the large matrix.
There are up to 5 testcases and each matrix can be of max 1000x1000 size and I need to solve this in under 4 seconds.
My code timeouts for the largest possible input, I thought that maybe how I'm scanning the matrix is too slow.
This is my code:
package main
import (
"fmt"
"strconv"
"strings"
)
func main() {
var t, rL, cL, rS, cS, temp int
var s string
var sl []string
var mxL, mxS [][]int
var found bool
fmt.Scanf("%d", &t)
for ; t > 0; t-- {
// Start scanning input
// Scanning large matrix
fmt.Scanf("%d%d", &rL, &cL)
mxL = make([][]int, rL)
for i := range mxL {
mxL[i] = make([]int, cL)
}
for i := 0; i < rL; i++ {
fmt.Scanf("%s", &s)
sl = strings.Split(s, "")
for j, v := range sl {
temp, _ = strconv.Atoi(v)
mxL[i][j] = temp
}
}
// Scanning small matrix
fmt.Scanf("%d%d", &rS, &cS)
mxS = make([][]int, rS)
for i := range mxS {
mxS[i] = make([]int, cS)
}
for i := 0; i < rS; i++ {
fmt.Scanf("%s", &s)
sl = strings.Split(s, "")
for j, v := range sl {
temp, _ = strconv.Atoi(v)
mxS[i][j] = temp
}
}
// Stop scanning input
// Start searching for small matrix in large matrix
found = true
for iL := 0; iL <= rL-rS; iL++ {
for jL := 0; jL <= cL-cS; jL++ {
found = true
if mxL[iL][jL] == mxS[0][0] {
for iS := 0; iS < rS; iS++ {
for jS := 1; jS < cS; jS++ {
if mxS[iS][jS] != mxL[iS+iL][jS+jL] {
found = false
break
}
}
if !found {
break
}
}
if found {
break
}
} else {
found = false
}
}
if found {
fmt.Println("YES")
break
}
}
if !found {
fmt.Println("NO")
}
// Stop searching for small matrix in large matrix
}
}
I'm using a slice of slices of ints to store the input.
mxL is the large matrix and mxS is the small matrix.
rL and cL stand for row and column of the large matrix.
rS and cS stand for row and column of the small matrix.
Well I am gonna point out an idea to you and then you can try to implement it. So create a new 2d array as large as your large array. Call it sumArray. Now let each cell in this sumArray represent the sum where the current cell is the most bottom-left cell. Now what you do is check only the cells that has the same sum as your small array instead of checking every element in the array.
So if those are your inputs
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 0 1 1
1 1 1
1 1 1
1 1 0
First sum your small array --> 8
Now let me show you how your sum array would look like
-1 -1 -1 -1 -1 -1 -1 means that we can't sum at this point because
-1 -1 -1 -1 -1 -1 the dimensions are just smaller than your small array
-1 -1 9 9 9 9 each other cell represent the sum of your original
9 9 9 9 9 9 matrix values.
9 9 9 8 9 9
Now if you scan trough this array only you can see that you will reduce your search space from every possible position to only the position where your sum is equal. This doesn't guarantee that the array are in this position you still have to add a verification step but it reduce your search space.

Partitioning a no. N into M partitions

I'm trying a problem in which I have to partition a no. N into M partitions as many as possible.
Example:
N=1 M=3 , break 1 into 3 parts
0 0 1
0 1 0
1 0 0
N=3 M=2 , break 3 into 2 parts
2 1
1 2
3 0
0 3
N=4 M=4 , break 4 into 4 parts
0 0 0 4
0 0 4 0
0 4 0 0
4 0 0 0
0 0 1 3
0 1 0 3
0 1 3 0
.
.
.
and so on.
I did code a backtrack algo. which produce all the possible compositions step by step, but it chokes for some larger input.Because many compositions are same differing only in ordering of parts.I want to reduce that.Can anybody help in providing a more efficient method.
My method:
void backt(int* part,int pos,int n) //break N into M parts
{
if(pos==M-1)
{
part[pos]=n;
ppart(part); //print part array
return;
}
if(n==0)
{
part[pos]=0;
backt(part,pos+1,0);
return;
}
for(int i=0;i<=n;i++)
{
part[pos]=i;
backt(part,pos+1,n-i);
}
}
In my algo. n is N and it fill the array part[] for every possible partition of N.
What I want to know is once generating a composition I want to calculate how many times that composition will occur with different ordering.For ex: for N=1 ,M=3 ::: composition is only one : <0,0,1> ,but it occurs 3 times. Thats what I want to know for every possible unique composition.
for another example: N=4 M=4
composition <0 0 0 4> is being repeated 4 times. Similarly, for every unique composition I wanna know exactly how many times it will occur .
Looks like I'm also getting it by explaining here.Thinking.
Thanks.
You can convert an int to a partitioning as follows:
vector<int> part(int i, int n, int m)
{
int r = n; // r is num items remaining to be allocated
vector<int> result(m, 0); // m entries inited to 0
for (int j = 0; j < m-1; j++)
{
if (r == 0) // if none left stop
break;
int k = i % r; // mod out next bucket
i /= r; // divide out bucket
result[j] = k; // assign bucket
r -= k; // remove assigned items from remaining
}
result[m-1] = r; // put remainder in last bucket
return result;
}
So you can use this as follows:
for (int i = 0; true; i++)
{
vector<int> p = part(i, 3, 4);
if (i != 0 && p.back() == 3) // last part
break;
... // use p
};
It should be clear from this how to make an incremental version of part too.
A much simpler and mathematical approach:
This problem is equivalent to finding the co-efficient of x^N in the expression f(x) = (1+x+x^2+x^3+....+x^N)^M
f(x) = ((x^(N-1) - 1)/(x-1))^M
differentiate it M times(d^Nf(x)/dx^N) and the co-efficient will be (1/n!)*(d^Nf(x)/dx^N) at x = 0;
differentiation can be done using any numerical differentiation technique. So the complexity of the algorithm is O(N*complexity_of_differentiation)..

Resources