1
$\begingroup$

Given a list made of integers either positive or negative: I want to make $N$ groups with consecutive $M$ integers in it such that in every group, majority are positive. Is there a way to group them so if possible.

Eg- $N=2,M=3$ and list is $(-2 ,-2,2,2,-2,-2 )$ then 1 of the possible combination of groups can be $(-2(2),2(3),2(4)), (-2(5),-2(6),-2(1))$. The inner parenthesis represents its position in list.

I can only think of trying every combination i.e. brute force.

EDIT : To simplify: No $0$ and $M$ is odd and only majority of positive is needed.

  • 0
    I suppose by consecutive you mean in regards to the cyclic ordering of the list? The value of the integers on the list doesn't matter, only their sign?2017-02-10
  • 0
    Also, is there any relation between $N, M$ and the length $L$ of the list? In your example, $L=NM$.2017-02-10
  • 0
    @Fimpellizieri Yes by consecutive I meant that and only the sign matters. Also,the relation is $L=NM$.2017-02-10

1 Answers 1

1

You could make a parallel list of the running total of $M$ sign values, each starting at element $i$ and running to element $i+M-1$ with wraparound:

List: $(-2 ,-2,2,2,-2,-2 )$
Signs only: $( \color{red}{-1}, \color{orange}{-1}, \color{brown}{1}, \color{green}{1}, \color{blue}{-1}, \color{purple}{-1} )$
Length-$3$ totals:
$( \color{red}{-1}+ \color{orange}{-1}+ \color{brown}{1}=-1,$
$\color{orange}{-1}+ \color{brown}{1}+ \color{green}{1}=1,$
$\color{brown}{1}+ \color{green}{1}+ \color{blue}{-1}=1,$
$\color{green}{1}+ \color{blue}{-1}+ \color{purple}{-1}=-1,$
$\color{blue}{-1}+ \color{purple}{-1}+ \color{red}{-1}=-3,$
$\color{purple}{-1}+ \color{red}{-1}+ \color{orange}{-1}=-3)$

As you can see the sum for the last few elements wraps around and the sum is completed by adding on elements from the start.

Then anything that isn't zero (which, incidentally, with $M$ odd and no zeroes is inevitably everything) indicates a valid starting position for a list.

So a more interesting case would be with an even $M=4$ and here $N=3$:

List:: $1, 4, 4, 2, 1, -4, 2, 3, -4, 0, -3, -1$
Signs: $1, 1, 1, 1, 1, -1, 1, 1, -1, 0, -1, -1$
$M$-length Totals: $4, 4, 2, 2, 2, 0, 1, -1, -3, -1, 0, 2$

So we would avoid the cases where we include a zero; so we could start with the first element or the fourth element but not with the second or third, because that would include a zero in a later $M$-length list.


So let's try to get the most positive-majority groups possible, for $M$ odd and with no zeroes in the source list.

Once again we can focus onto just sign-value and then make the $M$-length sum lists ($M=3$ this time):

List:: $1, 4, 4, 2, 1, -4, 2, 3, -4, -2, -3, -1$
Signs: $1, \color{red}{1, 1, 1}, 1, -1, 1, \color{blue}{1, -1, -1}, -1, -1$
$M$-length Totals: $3,\color{red}{3},3,1,1,1,1,\color{blue}{-1},-3,-3, -1,1$

Higlighted here a couple of sets of summing elements to build the $M$-length list.

Then we can abstract these $M$-length-totals down to sign and fold the list down into just one list of M elements representing different splitting positions by summing every $M^{th}$ element:

$M$-length signs: $\color{red}{1},\color{green}{1},\color{blue}{1},\color{red}{1},\color{green}{1},\color{blue}{1},\color{red}{1},\color{green}{-1},\color{blue}{-1},\color{red}{-1}, \color{green}{-1},\color{blue}{1}$
Folded list: $\color{red}{2},\color{green}{0},\color{blue}{2}$

So splitting the list before the first or third element will give us more positive-majority lists than splitting before the second element.

  • 0
    I am not getting the $M$ totals part. Can you explain it ?And for simplicity let's remove $0$ and denote positive with $1$ and negative with $-1$2017-02-10
  • 0
    Sorry,I'm still not getting it. Can you expand the answer more? And for majority to exist, let's take $M$ to be an odd integer.2017-02-10
  • 0
    If we have no zeroes in the list and odd $M$, there is nothing to do. Any division of the list into blocks of $M$ will satisfy the condition of having a majority positive or a majority negative in each block. I begin to think I may have misunderstood the question.2017-02-10
  • 0
    I will update the question to make it more clear2017-02-10
  • 0
    Take a look at the question now. I will work on the negative if you can help with the majority of positive.2017-02-10
  • 0
    @sammy updated answer with some thoughts, hopefully in the right direction.2017-02-10
  • 0
    I am sorry, but I'm not getting the part from $M$ totals list. What is $M$ totals ?See what I want is after that sign list (from list) to group them in $N$ groups with each having $M$ consecutive numbers. In this case, I only want them to have majority of positives if possible. If it's not then that means majority is of negatives.2017-02-11
  • 0
    For the eg you mentioned I can club majority into many ways ,one being: $M=3${$(1,4,4),(2,1,−4),(2,3,−4),(−2,−3,−1)$} in this $3$ are majority positives and $1$ group is negative group. So I can say I have majority of positive group. Second way can be {$(4,2,1),(−4,2,3),(−4,−2,−3),(−1,1,4)$} Here also $3$ groups are positives and $1$ negative, So I can make set of positive groups.2017-02-11
  • 0
    Agreed, and those options correspond to the positive values in the final list of splitting options. Answer clarified again, hopefully.2017-02-11
  • 0
    Sorry I'm still not getting how did you make $M$-length sum lists. What exactly does this list mean ? What is the meaning of length sum list ?How did you get $3$ and $-3$ in that list ?2017-02-11
  • 0
    Here take a small example of sign list : {$1, -1, -1, 1, 1$}. Now can you please explain how to get answer from this step by step?2017-02-11
  • 0
    Sorry, why not take $M=1$2017-02-11
  • 0
    I just want to know how you get the $M$ length total list. Sorry,but I am not able to understand it. What is the meaning of $M$ length list ?2017-02-12