Given an array A[], determine the minimum value of expression: min( abs( a[ i ] - x ) , abs( a[ i ] - y ) ) - sum

Given an array A of N integers. Find two integers x and y such that the sum of the absolute difference between each array element to one of the two chosen integers is minimal.
Determine the minimum value of the expression
Σ(i=0 to n) min( abs(a[i] - x), abs(a[i] - y) )
Example 1:
N = 4
A = [2,3,6,7]
Approach
You can choose the two integers, 3 and 7
The required sum = |2-3| + |3-3| + |6-7| + |7-7| = 1+0+0+1 = 2
Example 2:
N = 8
A = [ 2, 3, 5, 8, 11, 14, 17, 996 ]
Approach
You can choose the two integers, 8 and 996
The required sum = |2-8| + |3-8| + |5-8| + |8-8| + |11-8| + |14-8| + |17-8| + |996-996| = 6+5+3+0+3+6+9+0 = 32
Constraints
1<=T<=100
2<=N<=5*10^3
1<=A[i]<=10^5
The sum of N over all test cases does not exceed 5*10^3.
**My code : **
Please help me with the optimal solution O(N) or O(NlogN)
int minAbsDiff(vector<int> Arr, int N)
{
// Approach: O(N^3)
sort(Arr.begin(), Arr.end());
int sum = INT_MAX;
for (int i = 0; i < N; i++)
for (int j = i + 1; j < N; j++)
{
int tmp_sum = 0;
for (int k = 0; k < N; k++)
{
tmp_sum += min(abs(Arr[k] - Arr[i]), abs(Arr[k] - Arr[j]));
}
sum = min(sum, tmp_sum);
}
std::cout << "Sum is :" << sum << std::endl;
return sum;
}

Related

Is the Time Complexity of this function O(n * (n * log n² ))

What is the Time Complexity of the function below? n > 0
Function fun(n){
Let count = 0;
For( I = 0; I < n; I++){
For(j = 0; j < n; j /= 2) {
For(h = 0; h < n; h /= 2) {
Count = count + 1;
}
}
}
Return count;
}
I have O(n * (n * log n² )) , but something tells me i might be wrong.
The above loop is an infinite loop. time complexity for this cannot be determined, unless the problem statement is updated properly!
Function fun(n){
Let count = 0;
For( I = 0; I < n; I++){
// will run infinitely even if you change j /= 2 to j *= 2, because initial value is 0
For(j = 0; j < n; j /= 2) {
// will run infinitely even if you change h /= 2 to h *= 2, because initial value is 0
For(h = 0; h < n; h /= 2) {
Count = count + 1;
}
}
}
Return count;
}

Time complexity of two nested loops depending on n and k

I have a string of n chars and a k length of unique substrings
I'm trying to understand the time complexity of this code:
for (int i = 0; i <= inputStr.length() - k; i++) {
String substr = inputStr.substring(i, i + k);
Set<Character> setChars = new HashSet<Character>();
for (int j = 0; j < k; j++) {
setChars.add(substr.charAt(j));
}
if (setChars.size() == num) {
set.add(substr);
}
}
If I correctly understood the time complexity might be expressed by the formula:
f((n-k+1)*k)
I believe that the worse case I can have is when k = n/2, so:
f((n-k+1)k) = nn/2 - n/2*n/2 + n/2 = 1/2*n*n - 1/4*n*n + 1/2*n =>
O(n)
You are correct until the implication.
f((n-k+1)k) = nn/2 - n/2*n/2 + n/2 = 1/2*n*n - 1/4*n*n + 1/2*n
= 1/4n*n + 1/2*n => O(n*n)

Algorithm to group consecutive words minimizing length per group

From an input of space-delimited words, how to concatenate consecutive words so that:
each group has a minimum length L (spaces don't count)
longest group length is minimal (spaces don't count)
Example input:
would a cat eat a mouse
Example minimum length:
L = 5
Naive algorithm that solves the first condition but not the second one:
while length of a group is less than L, concatenate next word to group
if last group is shorter than L, concatenate last two groups together
This naive algorithm produces:
group 1: would
group 2: acateat
group 3: amouse
longest group length: 7
Second condition is not solved because a better solution would be:
group 1: woulda
group 2: cateat
group 3: amouse
longest group length: 6
Which algorithm would solve the second condition (minimal longest group) with relatively fast execution as a program? (by fast, I'd like to avoid testing all possible combinations)
I know C, ObjC, Swift, Javascript, Python, but pseudocode is fine.
This can be done with dynamic programming approach. Let's count a function F(i) - the minimum length of the longest group among correct divisions of the first i words into groups.
F(0) = 0
F(i) = Min(Max(F(j), totalLen(j+1, i))), for j in [0..i-1]
Where
totalLen(i, j) = total length of words from i to j, if the length is at least L
totalLen(i, j) = MAX, if total length is less than L
The answer is the value of F(n). To get the groups themselves we can save the indices of the best j for every i.
There is a implementation from the scratch in c++:
const vector<string> words = {"would", "a", "cat", "eat", "a", "mouse"};
const int L = 5;
int n = words.size();
vector<int> prefixLen = countPrefixLen(words);
vector<int> f(n+1);
vector<int> best(n+1, -1);
int maxL = prefixLen[n];
f[0] = 0;
for (int i = 1; i <= n; ++i) {
f[i] = maxL;
for (int j = 0; j < i; ++j) {
int totalLen = prefixLen[i] - prefixLen[j];
if (totalLen >= L) {
int maxLen = max(f[j], totalLen);
if (f[i] > maxLen) {
f[i] = maxLen;
best[i] = j;
}
}
}
}
output(f[n], prev, words);
Preprocessing and output details:
vector<int> countPrefixLen(const vector<string>& words) {
int n = words.size();
vector<int> prefixLen(n+1);
for (int i = 1; i <= n; ++i) {
prefixLen[i] = prefixLen[i-1] + words[i-1].length();
}
return prefixLen;
}
void output(int answer, const vector<int>& best, const vector<string>& words) {
cout << answer << endl;
int j = best.size()-1;
vector<int> restoreIndex(1, j);
while (j > 0) {
int i = best[j];
restoreIndex.push_back(i);
j = i;
}
reverse(restoreIndex.begin(), restoreIndex.end());
for (int i = 0; i+1 < restoreIndex.size(); ++i) {
for (int j = restoreIndex[i]; j < restoreIndex[i+1]; ++j) {
cout << words[j] << ' ';
}
cout << endl;
}
}
Output:
6
would a
cat eat
a mouse
Runnable: https://ideone.com/AaV5C8
Further improvement
The complexity of this algorithm is O(N^2). If it is too slow for your data I can suggest a simple optimization:
Let's inverse the inner loop. First, this allows to get rid of the prefixLen array and it's preprocessing, because now we add words one by one to the group (actually, we could get rid of this preprocessing in the initial version, but at the expense of simplicity). What is more important we can break our loop when totalLen would be not less than already computed f[i] because further iterations will never lead to an improvement. The code of the inner loop could be changed to:
int totalLen = 0;
for (int j = i-1; j >= 0; --j) {
totalLen += words[j].length();
if (totalLen >= L) {
int maxLen = max(f[j], totalLen);
if (f[i] > maxLen) {
f[i] = maxLen;
best[i] = j;
}
}
if (totalLen >= f[i]) break;
}
This can drastically improve the performance for not very big values of L.

Total number of values - sum

I need to write a program "long int sum(int n)" which sum the total number of values like this:
1! − 2! + 3! − ... ± n!
I'm succesful with writing the sum for:
1-3 + 5 - ... ± (2n + 1)
float sum (int n) {
int max = 2*n +1, i = 1, sum = 0, ch = 2;
for (i = 1; i <= max; i+2; ){
if ((ch%2) == 0){
sum += i;
}
else{
sum = sum - i;
}
ch++;
return sum;
}
But I don't know/have an idea how to make it for a factorial sum.
it's useful to make another function that does the factorial and one that does the sum of the alternating series . . .
int factorial(int n)
{
int sum = 1;
if (n > 0)
for (int i = n; i > 1; --i)
sum *= i;
else if (n <= 0)
return 0;
return sum;
}
int alernatingSeriesSum(int nStart)
{
if(nStart < 1) return 0;
int sum = 0;
for(int i=1; i<nStart; ++i)
sum += (factorial(i) * ((i%2)==0 ? -1 : 1)); //multiply -1 if its an even #s
return sum;
}
the factorial is pretty straightforward, multiply by the value, decrement by one and iterate until it reaches 1.
the altnerating series sum is similiar, it calls factorial for reach iterating (except this time the index increases), and creates an alternating sign by mulitplying by -1 every time the index is even. this is how we produce 1! - 2! + 3! - 4! + . . . + (n+1)! - (n+2)!
i hope that helps . . .
if you cannot split it into functions, try writing this all in one main function . . . i tested this code in C and it works. feel free to play with the code and try to read what each line does. good luck.
Split it into two functions. Instead of
sum += i;
and
sum = sum - i;
try:
sum += factorial(i);
and
sum = sum - factorial(i)
where factorial is some method that computes factorial:
long int factorial(int n) {
long int fact = n;
while ( n > 1) {
n--;
fact *= n;
}
return fact;
}

C language. Logic error: The left operand of '-' is a garbage value

I have the following code, but in this line of code I have warning x[i] = (rhs[i] - x[i - 1]) / b;, compiler is telling me that rhs[i] is a garbage value. Why it's happend? And how to remove this warning?
double* getFirstControlPoints(double* rhs, const int n) {
double *x;
x = (double*)malloc(n * sizeof(double));
double *tmp; // Temp workspace.
tmp = (double*)malloc(n * sizeof(double));
double b = 2.0;
x[0] = rhs[0] / b;
for (int i = 1; i < n; i++) // Decomposition and forward substitution.
{
tmp[i] = 1 / b;
b = (i < n - 1 ? 4.0 : 3.5) - tmp[i];
x[i] = (rhs[i] - x[i - 1]) / b; //The left operand of '-' is a garbage value
}
for (int i = 1; i < n; i++) {
x[n - i - 1] -= tmp[n - i] * x[n - i]; // Backsubstitution.
}
free(tmp);
return x;
}
All compiler warnings and calling getFirstControlPoints you may see on screenshots.
You need a check to make sure you have at least 4 points in the points array because this loop (line 333):
for (NSInteger i = 1 ; i < n - 1 ; ++i) {
// initialisation stuff
}
will not execute at all for n = 0, 1, 2.
Assume that points has 3 objects in it, At line 311 you set n to the count - 1 i.e. n == 2
Then the loop condition is i < 2 - 1 i.e. i < 1.
I think you need the loop condition to be i < n
if points.count is 0 or 1 you are facing some problems, because then, n is -1 or 0, and you access rhs[n-1]; and you malloc n* bytes;
maybe that can be the problem. that you put some rangechecks int o the code?