divide and conquer – Clarification on the algorithm for finding a majority element

When the size of your problem reduced to 2, it’s the base case of your
recursion, it’s false that we pair up two remaining elements. so it’s
sufficiently, first count number of occurrence of two remaining
elements in $A$.

for your problem we act as follow:

we check the whole array to count number of occurrence $A(i)=3$, and
number of occurrence of $A(i)=1$ and then if number of occurrence
$3geq frac{n}{2}+1$ or if number of occurrence $1geq frac{n}{2}+1$, we print it.

but the main problem is we must prove the correctness and running time of algorithm.

Suppose $mathcal{X}$ is majority element, because of, $mathcal{X}$ is majority element, then the number of occurrence of it, is grater than
$frac{n}{2}$, so after pair up, at least there is one pair that elements are $mathcal{X}$, otherwise it contradict with this fact that $mathcal{X}$ is majority element.

On the other hand, at most all elements of array $A$ be $mathcal{X}$, so both elements of a pair be equal, as a result at most half of the elements remain in each recursion.

Therefore one of the at most $frac{n}{2}$ elements be $mathcal{X}$. And we do our divide an conquer the problem until remain only two element.

Our statement holds only if $A$ contains $mathcal{X}$. So after we get base that $n=2$, it’s sufficient to for each of two remaining elements we check number of occurrence it.

Suppose the remaining elements is a,b:

  for i = 1 to n
     if (a==A(i))
     if (b==A(i))

 if(a> n/2)
   print a
 else if (b> n/2)
   print b
   print "there is no majority in the array"

Because at each iteration at most half of the elements are remain

Note that at each iteration pairing up of $n$ elements need $O(n)$ time.

floating point – Unit conversion – Better to divide by an integer or multiply by a double?

I currently have a long timestamp measured in units of 100ns elapsed since January 1st, 1900. I need to convert it to milliseconds.

I have the choice of either multiplying by 0.0001 or dividing by 10_000. Although at first glance they sound the same, the former would actually cause an implicit cast to a double – the latter would of course result in another long with the remainder truncated.

Which would yield a better result? Obviously double is an imprecise type that introduces errors due to the use of a mantissa, floating radix, and exponent, but would that error be less or more than the error from performing the simple integer division? Or would the error be demonstrably negligible?

To give an example of one of my values, here is one of the timestamps: 38348440316924872 .

I’m specifically referring to C#, but this question should be general to computer science.

How does one divide massive distances into a floating-point cooperative value system?

I am making a space game, and the large problem I am having is distances, positions, and floating point errors.

Imagine I currently have an galaxy of 100,000 X 2,000 X 100,000 light-years. I need to place objects in this space, and because of floating point limitation, I cannot simply put something at, say, 247014781, 181487104710, 17218383000 (actual distances even larger).

My current idea was to make a coordinate system of incrementally smaller cubes.

For this idea, we divide the galaxy into 4 quadrants of 50k ly. We also say that negative means lower half, positive means upper half. We now essentially have 8 octants of 50k x 50k x 50k light-year length.

using this idea, we could theoretically divide this large area into something reasonable, but the resultant coordinates are a bit odd. Take a 2, 4, 5, 10, 10 division (so divide into quadrants, then those quads into 4, etc) and we get a coordinate that is represented as:

-+1-4, 1-64, 1-125, 1-1000, 1-1000, meters (ei -1, 31, 23, 420, 69, any value between 0 and 2.365e+17)

this DOES make the possible x, y, z position way smaller (a change from 9.46073e+20 to 2.365e+17 meters), but it doesn’t make it small enough, it looks a bit odd, and it doesn’t seem practical from a programmers standpoint.

Let’s look at this on a smaller scale, say a cube of 1000 meters. Let’s use a half, quadrant, sector, meters system, in which each sector is 1/2 of a quadrant (8 sectors a quadrant))
This changes the highest possible value from 1000 to 250, as 1000 / 2 / 2 = 250. A position of 230, 272, -71 would be represented in our new system as (index from topleftback corner) (+3.1.230, +3.1.22, +3.1.71)

We have also confined all value to positive, as we know their direction of each value due to the quadrant and sector, and we add in that direction. That is why z=-71 is now +3.1.71, as 3 tells me I am in a negative quadrant.

Let’s say that we clamp the player position at 0,0,0, and handle movement by procedurally generating using our new coordinate value. The player is, in unity, always at 0, but in the class we designate a value coords as that stated above. We also have a “star” who’s coordinate is (+3.1.70, +3.1.245, +3.1.11). We draw the star (in unity coordinates now) at a position relative to the player (in this case -160, 223, 60)

How can I do something like this on a reasonable scale? Is there a better way to do this?

combinatorics – For which $n$ we can divide set $M= {1,2,3,…,3n}$ in to $n$ subsets each with $3$ elements such that in each subset ${x,y,z}$ we have $x+y=3z$?

For which $n$ we can divide set $M= {1,2,3,…,3n}$ in to $n$ $3$ element subsets such that in each subset ${x,y,z}$ we have $x+y=3z$?

Since $x_i+y_i=3z_i$ for each subset $A_i={x_i,y_i,z_i}$, we have $$4sum _{i=1}^n z_i=sum _{i=1}^{3n}i = {3n(3n+1)over 2} implies 8mid n(3n+1) $$
so $n=8k$ or $n=8k-3$. Now it is not difficult to see that if $k=1$ we have such partition.

Say $n=5$ we have: $A_1= {9,12,15}, A_2= {4,6,14}, A_3= {2,5,13}, A_4= {10,7,11}$ and $A_5= {1,3,8}$

What about for $kgeq 2$? Some clever induction step? Or some ”well” known configuration?

java – Divide BigDecimal by int

I am trying to divide the change owed by 1 to figure out how many dollars are owned. So if 3.60 is owed I want to divide that by 1 but I used BigDecimal to collect the change variable and can’t figure out how to divide it by 1.

This is the code I have tried but it’s not working. I needed to use BigDecimal for round off errors but I also need to use that value with int values.

BigDecimal Dollars = (Change.divide(1));

plotting – Cannot divide a list of numbers by their total from uploaded data

I have a list of numbers in a CSV file which my code uploads to Mathematica. I upload the file, then flatten the list and sort it to arrange the numbers in ascending order.

After doing that, I would like to divide each number in the rearranged list by the total of the list and then plot it. I am using the code shown below, but it says it is ‘not a list of numbers or pair of numbers’. What am I doing wrong? What do I need to change?


FormPage[{"data" -> "CSV"},
 a = NumericalSort[Flatten[#data]] &;
 b = a/Total[a] &;

Divide string only at line breaks

I have a very simple problem but don’tk now how to solve it.

I have a string that looks like this:

string=”a b c

d e f”

I want to break it ONLY at the line breaks, i.e. I want to get a list of the following two strings in return:

“a b c” and “d e f”.

I.e. the answer should be {“a b c”, “d e f”}.

However, if I use StringSplit[string], I instead get {“a”,”b”,”c”,”d”,”e”,”f”}.

How can I fix this? Why does Mathematica treat line breaks and whitespaces the same way?

java – How do I divide Dao and Dao-Cache into different modules?

I want to improve performance for api. I design cache in dao layer. I use caffine cache. Basic code is:


public class CacheConfig {

    private AppDao appDao;

    @Bean(value = "appCache")
    public LoadingCache<String, App> appCache() {
        return Caffeine.newBuilder()
                .refreshAfterWrite(5, TimeUnit.MINUTES)
                .build(key -> appDao.getByKeyFromDB(key));


public class AppDao {

    @Qualifier(value = "appCache")
    private LoadingCache<String, App> appCache;

    private AppMapper appMapper;

    // service call getByAppKey
    public App getByKey(String appKey) {
        App app = appCache.get(appKey);
        return app;

    // appCache load data by getByKey
    public App getByKeyFromDB(String appKey) {
        LambdaQueryWrapper<App> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(App::getAppKey, appKey);
        return appMapper.selectOne(queryWrapper);

I maybe divide getByKey and getByKeyFromDB to two file, but I have no idea.
How do I divide getByKey and getByKeyFromDB to different modules?

google sheets – Divide data evenly in row, place in additional rows

Okay, so what I have presented is a random divisor for every number. If you want all the rows in a column to add up, it will not be a random number. for instance, if you want to have 4 rows, the number in which you will use as a divisor will be 4. This is unless you are referring to the average of the random divisors to equal 4… or however that really works. The point is, choosing a random number between 4 and 7 will not have the summation results in which you are looking for.

    function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var data = ss.getDataRange().getValues();
  var newData = ((,,,,,));
  var i = 0;
  var j = 0;
  var k = 1;
  var random = ();
  for (var i = 0, dataLength = data.length; i < dataLength; i++) {
    for (k = 1; k < 6; k++) {
    random(k) = Math.floor(Math.random() * 7) + 4;
    newData(j) = (data(i)(0) / random(1),data(i)(1) / random(2),data(i)(2) / random(3),data(i)(3) / random(4),data(i)(4) / random(5),data(i)(5));
    for (k = 1; k < 6; k++) {
      random(k) = Math.floor(Math.random() * 7) + 4;
    newData(j+1) = (data(i)(0) / random(1),data(i)(1) / random(2),data(i)(2) / random(3),data(i)(3) / random(4),data(i)(4) / random(5),data(i)(5));
    for (k = 1; k < 6; k++) {
      random(k) = Math.floor(Math.random() * 7) + 4;
    newData(j+2) = (data(i)(0) / random(1),data(i)(1) / random(2),data(i)(2) / random(3),data(i)(3) / random(4),data(i)(4) / random(5),data(i)(5));
    for (k = 1; k < 6; k++) {
      random(k) = Math.floor(Math.random() * 7) + 4;
    newData(j+3) = (data(i)(0) / random(1),data(i)(1) / random(2),data(i)(2) / random(3),data(i)(3) / random(4),data(i)(4) / random(5),data(i)(5));
    j = j + 4
  ss.getRange(1,1, newData.length,6).setValues(newData);