Divide first n square numbers 1^2, 2^2, ……. n^2 into two groups such that absolute difference of the sum of the two groups is minimum [closed]

lets say Given input is n = 6 (n is as large as 100000) My task is to divide {1, 4, 9, 16, 25, 36} into two groups and PRINT these two groups

Possible Solution 1: dividing groups as {1, 9, 36} and {4, 16, 25} which gives abs diff as abs(46 – 45) = 1. So the minimum difference is 1 and the two groups are {1, 9, 36} and {4, 16, 25}

Possible Solution 2: Another Possible Solution is dividing groups as {9, 36} and {1, 4, 16, 25} which gives abs diff as abs(45 – 46) = 1. So the minimum difference is 1 and the two groups are {9, 36} and {1, 4, 16, 25}.

If there are multiple solutions we can print any one. Iam trying to solve it using https://www.geeksforgeeks.org/divide-1-n-two-groups-minimum-sum-difference/ but its not working.

I know that min difference is always 0 or 1 for n >= 6 but how to divide them into two groups.

And can we extend this problem to cubes, fourth powers, so on. if so what is the strategy used

Is there a minimum requirement for a character to have line of effect with Shadow Jaunt/Stride/Blink?

I’m pretty sure this has been asked already but I can’t find the question in question, so I hope I don’t break any rules by asking again.

The Shadow X maneuvers from Shadow Hand have a line of effect and line of sight requirement to work. Let’s say I want to teleport through a hole in a wall. According to both line rules, as long as I can see though the hole and there is nothing obstructing the path though the hole, I should be able to teleport though it.

Is there a minimum size the hole has to be for my character to be able to traverse the hole?

Oriented spanning tree of a directed multi-graph with minimum weight

I have problem of finding the minimum spanning tree of a simple graph, but the result is restricted by additional two types of condition:

  • There is a known root, which is s in the following example.
  • We know directions of some edges if they are chosen. These edges are chosen yet, or the problem becomes a Steiner tree problem.

Note that numbers on edges are their weights. So we will get s -> b -> c -> a if a normal min spanning tree is applied, but the direction of edge ac is wrong. On the other hand, we cannot use Chu–Liu/Edmonds’ algorithm for spanning arborescence of directed graphs, because we don’t know and cannot infer the direction of edge bc.

We can infer some edges’ directions according to the position of the root. For example, in the example, we know s -> b and s -> a.

Oriented Spanning Tree

In the final section of spanning tree, Wikipedia, oriented spanning tree is mentioned and a paper [levine2011sandpile] is referred. The problem fits the setting. It says:

Given a vertex v on a directed multigraph G, an oriented spanning tree T rooted at v is an acyclic subgraph of G in which every vertex other than v has outdegree 1.

Note that the term "outdegree" is a bit confusing, which I think should be "indegree". But it doesn’t matter, because it just restricts the simple subgraph to be a directed tree with root being source or sink.

For edges (in the original simple graph) whose directions are unknown, we add two directed edges between two vertices with inverse directions. Then we find the oriented spanning tree of this multi-graph. But it is not clear to me how an algorithm can be implemented according to that paper.

  • Levine, L. (2011). Sandpile groups and spanning trees of directed line graphs. Journal of Combinatorial Theory, Series A, 118(2), 350-364.
  • https://en.wikipedia.org/wiki/Spanning_tree

minimum travel from point to point with incremental steps

It’s my first time to make a question here. I have a curious problem about algorithm, in the center of Cartesian plane (0,0) I need to go to another point (x,y) but I only can use horizontaly and verticaly steps and this steps increases one by one.

For example, I need to go to (1,1) point and steps are:

  • Go to (1,0), a step of 1 unit.
  • Go to (1,-2) a step of 2 units.
  • Finally, go to (1,1) a step of 3 units.

Obviously, there are several ways to go to a point from center but the problem needs the minimal.

Are there a formula or an algorithm to answer this question? Thanks for read this and for your questions.

Multi-dimensional Knapsack with Minimum Value constraints for Dimensions

In MDK, we have a vector $ W = \{W_1, W_2, …, W_d\}$ where each element corresponds to the maximum weight for the respective dimension in the knapsack.

I want to add a conditional constraint: $ V = {V_1, V_2, …, V_d}$ , where each $ i$ -th dimension in the knapsack must have a value sum greater than threshold $ V_i$ . I am not so much concerned with the total value sum.

I would like to show this problem is NP-hard. My intuition is that the additional constraint makes this problem harder than MKD and therefore is NP-hard. But clearly this doesn’t constitute a formal proof.

Minimum number of nodes to select such that every node is at most k nodes away

I received this problem on an exam a few months ago, and have kept thinking about how to solve it with no luck.

Given a binary tree where each node in the tree can either be selected or unselected, implement a function k_away() which returns the minimum number of nodes that need to be selected so that every node in the tree is at most k nodes away from a selected node.

So, the nodes simply contain a pointer to the left child, a pointer to the right child, and a boolean marking it as selected or not:

struct Node {     Node *left;     Node *right;     bool selected = false; // starts out false }; 

The problem specifies a constraint of having a time complexity of O(n) and an auxiliary space complexity of O(n).

What I’ve thought of so far:

  • It seems like there are 2^n potential solutions (if I can choose to either select or not select every node and there are 2 of them), so brute force is a bad idea
  • I’ve searched around for similar problems and the closest thing I can find is the Dominating Set problem which seems… impossible to solve at this moment in polynomial time. I doubt this problem was given as an impossible problem.
  • Running DFS to get to the leaf nodes, then counting height as recursion unrolls. Every k away, marking the node as selected. This seems to work on small test cases, but does not return the minimum number away.
  • Running BFS on the root node to find all nodes k away while using another data structure to mark all visited nodes as ‘covered’, and then recursively running the BFS on each k-away node. This also seems to work on small test cases, but again doesn’t return the minimum number away.

Minimum vertex cover and odd cycles

Suppose we have a graph $ G$ . Consider the minimum vertex cover problem of $ G$ formulated as a linear programming problem, that is for each vertex $ v_{i}$ we have the variable $ x_{i}$ , for each edge $ v_{i}v_{j}$ we have the constraint $ x_{i}+x_{j}\geq 1$ , for each variable we have $ 0\leq x_{i}\leq 1$ and we have the objective function $ \min \sum\limits_{1}^{n}{x_{i}}$ . We say such a linear programming problem LP. Note that it is NOT an integer linear programming problem.

We find a half integral optimal solution of LP that we say $ S_{hi}$ . For each variable $ x_{i}$ that takes value 0 in $ S_{hi}$ , we add the constraint $ x_{i}=0$ to LP.

For each odd cycle of $ G$ , add to LP the constraint $ x_{a}+x_{b}+x_{c}+…+x_{i}\geq \frac{1}{2}(k+1)$ where $ x_{a},x_{b},x_{c},…,x_{i}$ are the vertices of the cycle and $ k$ is the number of vertices of the cycle. We find a new optimal solution of LP that we say $ S$ .

If $ x_{i}$ is a variable that takes value $ 0.5$ in $ S_{hi}$ and value $ \gt 0.5$ in $ S$ , can we say that there is at least a minimum vertex cover of $ G$ that contains the vertex associated to $ x_{i}$ ?

The idea behind the question: in an odd cycle $ c$ with $ k$ vertices, the number of vertices needed to cover the cycle is $ \frac{1}{2}(k+1)$ , therefore for each odd cycle we add to LP the constraint $ x_{a}+x_{b}+x_{c}+…+x_{i}\geq \frac{1}{2}(k+1)$ . If in $ S_{hi}$ the sum of the variables of $ c$ is $ \frac{k}{2}$ (that is all the variables of $ c$ take value $ \frac{1}{2}$ ), then in $ S$ at least a variable $ x_{i}$ of $ c$ takes vale $ \gt \frac{1}{2}$ and the vertex associated to $ x_{i}$ belongs to at least a minimum vertex cover of the given graph.

Minimum number of given operations in order to group letters in a string


Suppose we have a string containing letters ‘A’,’B’,’C’,’D’, and the characters are placed in a stack.We also have an empty stack.Ultimately,we want all letters grouped in the 2nd stack,using only 3 operations:

  • push("p"): Removes an items from the bottom of the 1st stack and place it to the top of the 2nd
  • complement("c"): Replace every all letters of the 1st stack with they "complements".The pairs are A – B and C – D
  • reverse("r"): Reverse the content of the 2nd stack.The top becomes bottom and bottom->top.

Example of moves

| Move | First Stack | Second Stack | +------+-------------+--------------+ |      | DBACA       |              | +------+-------------+--------------+ | p    | DBAC        | A            | +------+-------------+--------------+ | p    | DBA         | CA           | +------+-------------+--------------+ | r    | DBA         | AC           | +------+-------------+--------------+ | p    | DB          | AAC          | +------+-------------+--------------+ | c    | CA          | AAC          | +------+-------------+--------------+ | p    | C           | AAAC         | +------+-------------+--------------+ | r    | C           | CAAA         | +------+-------------+--------------+ | p    |             | CCAAA        | +------+-------------+--------------+ 

Note that the example above finds a solution,but not the minimum solution.The correct answer would be "ppr ppp"

Correct examples

Spaces in the sequence have no meaning and are added for readability purposes.

+------------------------+-------------------------------------+ | First Stack (input)    | Moves (output)                      | +------------------------+-------------------------------------+ | DD                     | pp                                  | +------------------------+-------------------------------------+ | BADA                   | ppr pp                              | +------------------------+-------------------------------------+ | DADA                   | ppc pp                              | +------------------------+-------------------------------------+ | DBACA                  | pprppp                              | +------------------------+-------------------------------------+ | BDA CACA               | ppr prp rppp                        | +------------------------+-------------------------------------+ | CAC DCDC               | pcp cpc pcp cpp                     | +------------------------+-------------------------------------+ | ADA DBD BCB DBCB       | ppr pcr pcr prp rpr prp rpr prp rp  | +------------------------+-------------------------------------+ | DAB BCC DCC BDC ACD CC | ppc pcp cpp rpp rpp cpc ppr ppc prp | +------------------------+-------------------------------------+ 

Brute force approach

We could just use brute force approach,calculating all possible moves until the first stack is empty.This could be done using BFS or A* algorithms.

For example,we could initialize an empty queue,start from a parent node and create 3 new nodes for every possible move.Then add these nodes to the queue.Every time remove a node from the queue and apply the operations.Save the sequence of moves while nodes are created.If the last move was a "c",then skip "c" operation for this node.The same is true about "r" operation (no repetitive c’s or r’s).If stack1 = empty for a node,then finish the program and return the sequence of moves.


Is there a better way to solve this problem? Can we apply some heuristics as improvement in the brute force approach? Thank you in advance.

Why decision tree method for lower bound on finding a minimum doesn’t work

(Motivated by this question. Also I suspect that my question is a bit too broad)

We know $ \Omega(n \log n)$ lower bound for sorting: we can build a decision tree where each inner node is a comparison and each leaf is a permutation. Since there are $ n!$ leaves, the minimum tree height is $ \Omega(\log (n!)) = \Omega (n \log n)$ .

However, it doesn’t work for the following problem: find a minimum in the array. For this problem, the results (the leaves) are just indices of the minimum element. There are $ n$ of them, and therefore the reasoning above gives $ \Omega(\log n)$ lower bound, which is obviously an understatement.

My question: why does this method works for sorting and doesn’t work for minimum? Is there some greater intuition or simply "it just happens" and we were "lucky" that sorting has so many possible answers?

I guess the lower bound from decision tree makes perfect sense: we do can ask yes/no questions so that we need $ O(\log n)$ answers: namely, we can use binary search for the desired index. My question still remains.