How to solve this efficiently? [closed] - arrays

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have two arrays:
score[n]
pos[n], where n <= 10^5; pos[i],score[i] <= 10^4
Define:
f(i,j) = abs(pos[i]-pos[j])*max(score[i],score[j])
I need to find sum of f(i,j) for all i,js.
I have an algorithm that can solve it in O(n^2) but i want to optimize.
I have spent much time but could not.
Any help is appreciated.
Worst case code
http://ideone.com/q4qSNh

There is a similar question asked earlier. See here.
#Gribouillis has provided a nice algorithm which has O(nlogn) complexity, consisting of sorting and using balanced binary search trees. Look here for the complete answer
Implementation has been left as an exercise.

Related

graph traversal in C [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have to implement a backtracing algorithm in C which will emulate a hardware in purely software, and trace a single path from output of a system to the input pin, registering all the gates, their outputs and inputs(taken) on the path in a reverse order. I figured out that it can be done in graph traversal algorithm but not able to implement in C. Any useful suggestions shall be helpful indeed!
I've done a few maze solving algorithms, both breadth and depth first search.
I'd say you should first build a graph and make sure its perfectly built and without any incoherence, and something i found to be very useful was to find a way to print my graph to check for errors :).
Other than that, good luck !
Depends on what kind of path tracing, it can follow both breadth first search or else Depth first search. I have tried both of them and it works.

C - Values under the same array index are different [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am analyzing a C code that I have been given and I came across this block:
for (k=0; k<m; k++)
{
//Perform some calculation and assign result to
//A[k].
if (A[k]!=A[k])
{
exception=1;
}
}
I have performed runs of the code where exception does turn out to be one, but I can't seem to understand how two array indices can contain different numbers! Is that something to do with machine precision? Thank you!
You might want to check whether the A[] array is allocated sufficient amount of memory: it should have 'm' elements allocated at least. If everything is OK, check the sizes of other arrays allocated in your program. The phenomenon that you've encountered looks like some memory allocation error.

Converting a character into a space in C arrays [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
So lets say I have received a message that resembles the following
"L2N5*8R10!11T0A1K3Y14#4W7O6O9C12R13"
and I am expected to sort out the characters in accordance to the numbers succeeding them and change the characters that are not letters into a space. I have no problem doing the sorting out part, I am only having a trouble while trying to write a function that will change those characters into space.
The out put should be something like this
TALK NOW OR CRY
but I am getting
TALK#NOW*OR!CRY
Can anyone help me figure out what my function should look like so that I can be able to change the characters into space??
Unless you show your code, we'll only be able to guess!!
However, as a general suggestion, I would recommended, you should check each entry against isalpha(). In case you got a return value of 0, replace the entry with a .

Warshall algorithm idea and improvement [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
The Warshall-Floyd algorithm is based on essentially the idea: exploit a relationship between a problem and its simpler rather than smaller version. Warshall and Floyd published their algorithms without mentioning dynamic programming. Nevertheless, the algorithms certainly have a dynamic programming flavor and have come to be considered applications of this technique.
ALGORITHM Warshall(A[1..n, 1..n])
//ImplementsWarshall’s algorithm for computing the transitive closure
//Input: The adjacency matrix A of a digraph with n vertices
//Output: The transitive closure of the digraph
R(0) ←A
for k←1 to n do
for i ←1 to n do
for j ←1 to n do
R(k)[i, j ]←R(k−1)[i, j ] or (R(k−1)[i, k] and R(k−1)[k, j])
return R(n)
We can speed up the above implementation of Warshall’s algorithm for some inputs by restructuring its innermost loop
My question on above text are following
What does author mean by idea is " exploit a relationship between a problem and its simpler rather than smaller version" Please elobaorate.
How can we improve speed as author mentioned in above implemenation.
The formulation from 1. means that the shortest path problem (which can be seen as a generalization of the transitive closure problem) has the optimal substructure property; however for this property does not exist a formal description (in the sense of a mathematical definition). The optimal substructure property is necessary for a problem to be amenable to dynamic programming.

Two to the power of some large number [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Can anyone tell me how to find (2^101100111000)%1000000007 in C?
There is a problem in which we have to convert a number into binary(1<=N<=600000) and find
2^(binary representation of N)modulo1000000007.
The values you are talking about will not fit into a standard long on any architecture, so you will have to use an arbitrary precision maths library such as GMP.
Hmm, just read Zong's answer... he's pointing to a more efficient method... haven't finished reading through the article but it looks like the better way to go...

Resources