absolute-value

Finding minimal absolute sum of a subarray

心不动则不痛 提交于 2019-11-29 06:27:12
There's an array A containing (positive and negative) integers. Find a (contiguous) subarray whose elements' absolute sum is minimal, e.g.: A = [2, -4, 6, -3, 9] |(−4) + 6 + (−3)| = 1 <- minimal absolute sum I've started by implementing a brute-force algorithm which was O(N^2) or O(N^3) , though it produced correct results. But the task specifies: complexity: - expected worst-case time complexity is O(N*log(N)) - expected worst-case space complexity is O(N) After some searching I thought that maybe Kadane's algorithm can be modified to fit this problem but I failed to do it. My question is -

Which is the fastest way to get the absolute value of a number

若如初见. 提交于 2019-11-28 03:56:19
Which is the fastest way to implement an operation that returns the absolute value of a number? x=root(x²) or if !isPositive(x): x=x*(-1) Actually this question can be translated as, how fast is an if (and why please). My college programing professors always told me to avoid if s for they are extremely slow, but I always forgot to ask how slow and why. Does anybody here know? kquinn Conditionals are slower than plain arithmetic operations, but much, much faster than something as silly as calculating the square root. Rules of thumb from my assembly days: Integer or bitwise op: 1 cycle Floating

How to convert a negative number to positive?

血红的双手。 提交于 2019-11-27 11:59:36
How can I convert a negative number to positive in Python? (And keep a positive one.) >>> n = -42 >>> -n # if you know n is negative 42 >>> abs(n) # for any n 42 Don't forget to check the docs . simply multiplying by -1 works in both ways ... >>> -10 * -1 10 >>> 10 * -1 -10 BoltClock If "keep a positive one" means you want a positive number to stay positive, but also convert a negative number to positive, use abs() : >>> abs(-1) 1 >>> abs(1) 1 The inbuilt function abs() would do the trick. positivenum = abs(negativenum) Tauquir In [6]: x = -2 In [7]: x Out[7]: -2 In [8]: abs(x) Out[8]: 2

Math.abs returns wrong value for Integer.Min_VALUE

偶尔善良 提交于 2019-11-27 10:25:28
This code: System.out.println(Math.abs(Integer.MIN_VALUE)); Returns -2147483648 Should it not return the absolute value as 2147483648 ? jonmorgan Integer.MIN_VALUE is -2147483648 , but the highest value a 32 bit integer can contain is +2147483647 . Attempting to represent +2147483648 in a 32 bit int will effectively "roll over" to -2147483648 . This is because, when using signed integers, the two's complement binary representations of +2147483648 and -2147483648 are identical. This is not a problem, however, as +2147483648 is considered out of range. For a little more reading on this matter,

jfreechart customize piechart to show absolute values and percentages

断了今生、忘了曾经 提交于 2019-11-27 05:25:40
How can this compilable minimal code snippet example, which uses JFreeChart as plotting API, adapted in order to show both absoulte values AND percentages ? I couldn't extract this information neither from any code snippet on the internet nor from the JFreechart manual itself. The code snippet produces a pie chart showing only percentages. The absolute values in my case also matter, so i need to display them right under the percentages. Here is the code: (Note it lacks the imports) public class MyMinimalPieChartExample { public static void main(String[] args) { DefaultPieDataset dataset = new

mongodb - Find document with closest integer value

老子叫甜甜 提交于 2019-11-27 05:04:11
Let's assume I have a collection with documents with a ratio attribute that is a floating point number. {'ratio':1.437} How do I write a query to find the single document with the closest value to a given integer without loading them all into memory using a driver and finding one with the smallest value of abs(x-ratio) ? Interesting problem. I don't know if you can do it in a single query, but you can do it in two: var x = 1; // given integer closestBelow = db.test.find({ratio: {$lte: x}}).sort({ratio: -1}).limit(1); closestAbove = db.test.find({ratio: {$gt: x}}).sort({ratio: 1}).limit(1);

Fastest way to compute absolute value using SSE

纵然是瞬间 提交于 2019-11-26 20:10:15
I am aware of 3 methods, but as far as I know, only the first 2 are generally used: Mask off the sign bit using andps or andnotps . Pros: One fast instruction if the mask is already in a register, which makes it perfect for doing this many times in a loop. Cons: The mask may not be in a register or worse, not even in a cache, causing a very long memory fetch. Subtract the value from zero to negate, and then get the max of the original and negated. Pros: Fixed cost because nothing is needed to fetch, like a mask. Cons: Will always be slower than the mask method if conditions are ideal, and we

How to convert a negative number to positive?

与世无争的帅哥 提交于 2019-11-26 15:44:27
问题 How can I convert a negative number to positive in Python? (And keep a positive one.) 回答1: >>> n = -42 >>> -n # if you know n is negative 42 >>> abs(n) # for any n 42 Don't forget to check the docs. 回答2: simply multiplying by -1 works in both ways ... >>> -10 * -1 10 >>> 10 * -1 -10 回答3: If "keep a positive one" means you want a positive number to stay positive, but also convert a negative number to positive, use abs() : >>> abs(-1) 1 >>> abs(1) 1 回答4: The inbuilt function abs() would do the

mongodb - Find document with closest integer value

为君一笑 提交于 2019-11-26 11:27:03
问题 Let\'s assume I have a collection with documents with a ratio attribute that is a floating point number. {\'ratio\':1.437} How do I write a query to find the single document with the closest value to a given integer without loading them all into memory using a driver and finding one with the smallest value of abs(x-ratio) ? 回答1: Interesting problem. I don't know if you can do it in a single query, but you can do it in two: var x = 1; // given integer closestBelow = db.test.find({ratio: {$lte: