INTA-007
# interview assessment questions
Min Abs Difference of Server Loads
// Schedule jobs so that
// each server gets equal load.
#include <bits/stdc++.h>
using namespace std;
// Function to find new array a
int solve(int a[], int b[], int n)
{
int i;
long long int s = 0;
// find sum S of both arrays a and b.
for (i = 0; i < n; i++)
s += (a[i] + b[i]);
// Single element case.
if (n == 1)
return a[0] + b[0];
// This checks whether sum s can be divided
// equally between all array elements. i.e.
// whether all elements can take equal value
// or not.
if (s % n != 0)
return -1;
// Compute possible value of new array
// elements.
int x = s / n;
for (i = 0; i < n; i++) {
// Possibility 1
if (a[i] > x)
return -1;
// ensuring that all elements of
// array b are used.
if (i > 0) {
a[i] += b[i - 1];
b[i - 1] = 0;
}
// If a(i) already updated to x
// move to next element in array a.
if (a[i] == x)
continue;
// Possibility 2
int y = a[i] + b[i];
if (i + 1 < n)
y += b[i + 1];
if (y == x) {
a[i] = y;
b[i] = b[i + 1] = 0;
continue;
}
// Possibility 3
if (a[i] + b[i] == x) {
a[i] += b[i];
b[i] = 0;
continue;
}
// Possibility 4
if (i + 1 < n &&
a[i] + b[i + 1] == x) {
a[i] += b[i + 1];
b[i + 1] = 0;
continue;
}
// If a(i) can not be made equal
// to x even after adding all
// possible elements from b(i)
// then print -1.
return -1;
}
// check whether all elements of b
// are used.
for (i = 0; i < n; i++)
if (b[i] != 0)
return -1;
// Return the new array element value.
return x;
}
int main()
{
int a[] = { 6, 14, 21, 1 };
int b[] = { 15, 7, 10, 10 };
int n = sizeof(a) / sizeof(a[0]);
cout << solve(a, b, n);
return 0;
}
=====================================================
Min Abs Difference of Server Loads
#include <bits/stdc++.h>
using namespace std;
int MaxSumDifference(int a[], int n)
{
// final sequence stored in the vector
vector<int> finalSequence;
// sort the original array
// so that we can retrieve
// the large elements from
// the end of array elements
sort(a, a + n);
// In this loop first we will insert
// one smallest element not entered
// till that time in final sequence
// and then enter a highest element
// (not entered till that time) in
// final sequence so that we
// have large difference value. This
// process is repeated till all array
// has completely entered in sequence.
// Here, we have loop till n/2 because
// we are inserting two elements at a
// time in loop.
for (int i = 0; i < n / 2; ++i) {
finalSequence.push_back(a[i]);
finalSequence.push_back(a[n - i - 1]);
}
// variable to store the
// maximum sum of absolute
// difference
int MaximumSum = 0;
// In this loop absolute difference
// of elements for the final sequence
// is calculated.
for (int i = 0; i < n - 1; ++i) {
MaximumSum = MaximumSum + abs(finalSequence[i] -
finalSequence[i + 1]);
}
// absolute difference of last element
// and 1st element
MaximumSum = MaximumSum + abs(finalSequence[n - 1] -
finalSequence[0]);
// return the value
return MaximumSum;
}
// Driver function
int main()
{
int a[] = { 1, 2, 4, 8 };
int n = sizeof(a) / sizeof(a[0]);
cout << MaxSumDifference(a, n) << endl;
}