## Samsung Interview Question

Java Developers**Country:**India

**Interview Type:**Written Test

This is how I tried to solve this in C.

```
#include <stdio.h>
#include <stdlib.h>
int MinimumDifference(int n, int* pOilmines, int nNoOfMines)
{
if (nNoOfMines < n)
{
return -1;
}
if (pOilmines != nullptr && nNoOfMines > 0 && n > 0)
{
double nMeanVal = 0.0;
for (int i = 0; i < nNoOfMines; i++)
{
nMeanVal += pOilmines[i];
}
nMeanVal /= n;
int *pCurrentAllocation = (int*)malloc(n * sizeof(int));
int j = 0;
int nMaxAllocation = 0, nMinAllocation = 0;
for (int i = 0; i < n; i++)
{
pCurrentAllocation[i] = 0;
if (n == nNoOfMines)
{
pCurrentAllocation[i] = pOilmines[i];
}
else
{
while (j < nNoOfMines && pCurrentAllocation[i] < nMeanVal)
{
pCurrentAllocation[i] += pOilmines[j];
j++;
}
if (j < nNoOfMines)
{
if ((pCurrentAllocation[i] - nMeanVal) > (nMeanVal - (pCurrentAllocation[i] - pOilmines[j - 1])))
{
pCurrentAllocation[i] = pCurrentAllocation[i] - pOilmines[j - 1];
j--;
}
}
}
if (pCurrentAllocation[i] > nMaxAllocation)
{
nMaxAllocation = pCurrentAllocation[i];
}
if (0 == i)
{
nMinAllocation = pCurrentAllocation[i];
}
if (pCurrentAllocation[i] < nMinAllocation)
{
nMinAllocation = pCurrentAllocation[i];
}
}
free(pCurrentAllocation);
return nMaxAllocation - nMinAllocation;
}
return -1;
}
int main()
{
int nNoOfCompanies = -1;
int nNoOfMines = -1;
printf("\n No. Of companies : ");
scanf("%d", &nNoOfCompanies);
if (nNoOfCompanies < 1)
{
printf("-1"); // Error
return -1;
}
printf("\n No. Of oil mines : ");
scanf("%d", &nNoOfMines);
if (nNoOfMines < nNoOfCompanies)
{
printf("-1"); // Error
return -1;
}
int *pArr = (int*)malloc(nNoOfMines * sizeof(int));
if (!pArr)
{
printf("Failed to allocate memory");
return -1;
}
for (int i = 0; i < nNoOfMines; i++)
{
scanf("%d", &pArr[i]);
if (pArr[i] < 1)
{
printf("-1"); // Error
return -1;
}
}
/**************************************************************************
Run MinimumDifference Algo
**************************************************************************/
printf("\n Min Diff = %d\n", MinimumDifference(nNoOfCompanies, pArr, nNoOfMines));
free(pArr);
return 0;
}
```

There is an island surrounded by oil mines. You will be given n companies and m oil mines having values. You have to distribute the mines to "n" companies in fair manner. Remember the companies can have oil mines adjacent to each other and not in between of each others.After distributing them compute the differnce of oil mines from the company getting highest and company getting lowest. This number should be minimum.(then only the distribution can be termed as fair).

I think instead of --

while(j < oilmines.Length && companyMineAllocation[i] < mediumMineValue)

you should try to make group of such mines sum of which is as close as possible to the mediumMineValue i.e sum_of_mines/total_companies.

if(abs(sumOfCurrentMines - mediumMineValue) > (sumOfCurrentMines+nextMine - mediumMineValue))

sumOfCurrentMine += nextMine

and since the oil mines are arranged in a circular fashion. you need to try with different starting points.

for that u can choose all the values that are subset of closest sum of mines to the mediumMineValue containing max value from the mines.

I think instead of --

while(j < oilmines.Length && companyMineAllocation[i] < mediumMineValue)

you should try to make group of such mines sum of which is as close as possible to the mediumMineValue i.e sum_of_mines/total_companies.

if(abs(sumOfCurrentMines - mediumMineValue) > (sumOfCurrentMines+nextMine - mediumMineValue))

sumOfCurrentMine += nextMine

and since the oil mines are arranged in a circular fashion. you need to try with different starting points.

for that u can choose all the values that are subset of closest sum of mines to the mediumMineValue containing max value from the mines.

I think instead of --

while(j < oilmines.Length && companyMineAllocation[i] < mediumMineValue)

you should try to make group of such mines sum of which is as close as possible to the mediumMineValue i.e sum_of_mines/total_companies.

if(abs(sumOfCurrentMines - mediumMineValue) > (sumOfCurrentMines+nextMine - mediumMineValue))

sumOfCurrentMine += nextMine

and since the oil mines are arranged in a circular fashion. you need to try with different starting points.

for that u can choose all the values that are subset of closest sum of mines to the mediumMineValue containing max value from the mines.

while(j < oilmines.Length && companyMineAllocation[i] < mediumMineValue)

you should try to make group of such mines sum of which is as close as possible to the mediumMineValue i.e sum_of_mines/total_companies.

if(abs(sumOfCurrentMines - mediumMineValue) > (sumOfCurrentMines+nextMine - mediumMineValue))

sumOfCurrentMine += nextMine

and since the oil mines are arranged in a circular fashion. you need to try with different starting points.

for that u can choose all the values that are subset of closest sum of mines to the mediumMineValue containing max value from the mines.

{

package spf;

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class Mines {

public static int nComp;

public static int mMines;

public static int x[] = new int[20];

public static int CompAlloc[] = new int[20];

public static int findFairness (int x[]) {

int nAverage = 0;

for (int i = 0; i < mMines; i++)

{

nAverage += x[i];

}

nAverage = nAverage/nComp;

int iStart = findStartPoint (nAverage);

findMinimum (iStart, nAverage);

System.out.println("Allocation");

for (int k =0; k < nComp; k ++)

{

System.out.print(CompAlloc[k] + ",");

}

System.out.println("");

return 0;

}

public static int findStartPoint (int nAverage)

{

int nMin = 100000000;

int iStart = 0;

for (int k =0; k < mMines; k++)

{

int nfinMin = findMinimum (k, nAverage);

if (nMin >= nfinMin)

{

nMin = nfinMin;

iStart = k;

}

}

System.out.println("Start Point: " + iStart);

return iStart;

}

public static int findMinimum (int iStart, int nAverage)

{

int nthComp = 0;

int Sum = 0;

int nMinimum = 100000000;

for (int j = iStart, k= 0; k < mMines; k++)

{

Sum += x[j];

if ((nComp -1 - nthComp) == (mMines-1-k))

{

int sum = Math.abs(Sum - nAverage);

if (sum < nMinimum)

{

nMinimum = sum;

}

CompAlloc[nthComp++] = Sum;

Sum = 0;

}

else if (nthComp == nComp - 1)

{

if (k == mMines -1)

{

int sum = Math.abs(Sum - nAverage);

if (sum < nMinimum)

{

nMinimum = sum;

}

CompAlloc[nthComp++] = Sum;

}

}

else if (Sum >= nAverage)

{

int sum1 = 0;

int sum2 = 0;

sum1 = Math.abs(Sum - nAverage);

sum2 = Math.abs(Sum -x[j] - nAverage);

if (sum1 < sum2)

{

CompAlloc[nthComp++] = Sum;

if (sum1 < nMinimum)

{

nMinimum = sum1;

}

}

else

{

CompAlloc[nthComp++] = (Sum - x[j]);

j--;

k--;

if (sum2 < nMinimum)

{

nMinimum = sum2;

}

}

Sum =0;

}

j++;

if (j >= mMines)

{

j =0;

}

}

return nMinimum;

}

public static void main(String args[]) throws FileNotFoundException {

Scanner sc = new Scanner(new File ("sample3.txt"));

int testcasecount = sc.nextInt();

for (int i = 0; i < testcasecount; ++i) {

nComp = sc.nextInt();

mMines = sc.nextInt();

for (int j = 0; j < mMines; ++j) {

x[j]= sc.nextInt();

}

findFairness (x);

}

sc.close();

return ;

}

}

}

```
#include<iostream>
using namespace std;
int an;
void calculateDiff(int comp[],int n)
{
int maxa=0,mina=1000;
for(int i=0;i<n;i++)
{
maxa = max(maxa,comp[i]);
mina = min(mina,comp[i]);
}
an = min(an,maxa-mina);
}
void calculateTotal(int end,int curr,int oil[],int comp[],int n,int m,int compNo)
{
if((curr+1)%m==end)
{
for(int j = compNo;j<n;j++)
{
comp[j]+=oil[curr];
calculateDiff(comp,n);
comp[j]-=oil[curr];
}
return;
}
if(compNo==n)
return;
comp[compNo]+=oil[curr];
calculateTotal(end,(curr+1)%m,oil,comp,n,m,compNo);
calculateTotal(end,(curr+1)%m,oil,comp,n,m,compNo+1);
comp[compNo]-=oil[curr];
calculateTotal(end,curr,oil,comp,n,m,compNo+1);
}
int main()
{
int n,m;
an = 10000;
scanf("%d%d",&n,&m);
int oil[m];
for(int i=0;i<m;i++)
scanf("%d",&oil[i]);
int comp[n];
for(int i=0;i<n;i++)
comp[i]=0;
for(int i=0;i<m;i++)
{
calculateTotal(i,i,oil,comp,n,m,0);
}
printf("%d",an);
return 0;
}
```

Classical example of how a medium of something can be utilized to solve many complex problems

Idea is to stay as close ass possible to medium.

Here is how we will solve it

```
public static int MinimumDifference(int n, int[] oilmines)
{
if(oilmines != null && oilmines.Length > 0 && n > 0)
{
int mediumMineValue = oilmines.Sum()/n;
int[] companyMineAllocation = new int[n];
int j = 0;
int maxallocation = 0, minallocation=0;
for(int i = 0 ; i < n;i++)
{
companyMineAllocation[i] = 0
while(j < oilmines.Length && companyMineAllocation[i] < mediumMineValue)
{
companyMineAllocation[i]+ = oilmines[j];
j++;
}
if(j < oilmines.Length)
{
if((companyMineAllocation[i] - mediumMineValue) > (mediumMineValue - (companyMineAllocation[i] - oilmines[j-1]))) // making sure we stay as close as possible to medium
{
companyMineAllocation[i] = companyMineAllocation[i]- oilmines[j-1];
j--;
}
}
else if((companyMineAllocation[i] - mediumMineValue) > (mediumMineValue - (companyMineAllocation[i] - oilmines[j-1]))) // making sure we stay as close as possible to medium
{
companyMineAllocation[i] = companyMineAllocation[i]- oilmines[j-1];
j--;
}
else
{
break;
}
if(companyMineAllocation[i] > maxallocation)
{
maxallocation = companyMineAllocation[i];
}
if(companyMineAllocation[i] < minallocation)
{
minallocation = companyMineAllocation[i];
}
}
return maxallocation-minallocation;
}
throw new Exception("invalid input")
}
```

Complexity : O(N) in time and O(1) in space. It is of type Greedy Algorithms

Can you please check if this code work on given constraints or not ?

Thanks!!

```
#include <bits/stdc++.h>
using namespace std;
bool vis[10000];
int main()
{
int t; cin>>t;
while(t--)
{
int n,m,lim; cin>>n>>m>>lim;
vector<int> v,o;
queue<pair<int,int> > q;
for(int i=0;i<n;++i)
{
int x; cin>>x;
v.push_back(x);
q.push(make_pair(x,1));
}
for(int i=0;i<m;++i)
{
int x; cin>>x;
o.push_back(x);
}
int y; cin>>y;
pair<int,int> re;
while(!q.empty())
{
int x = q.front().first;
int l = q.front().second;
if(x==y)
{
re.first = y;
re.second = l;
break;
}
q.pop();
if(!vis[x])
{
for(int i=0;i<n;++i)
{
q.push(make_pair(x*10 + v[i],l+1));
}
for(int i=0;i<m;++i)
{
for(int j=0;j<n;++j)
{
if(o[i]==1)
{
q.push(make_pair(x+v[j],l+3));
}
else if(o[i]==2)
{
q.push(make_pair(x-v[j],l+3));
}
else if(o[i]==3)
{
q.push(make_pair(x*v[j],l+3));
}
else if(o[i]==4)
{
q.push(make_pair(x/v[j],l+3));
}
}
}
vis[x] = true;
}
}
cout<<re.second<<endl;
}
}
```

Can you please check if this code work on given constraints or not ?

Thanks!!

```
#include <bits/stdc++.h>
using namespace std;
bool vis[10000];
int main()
{
int t; cin>>t;
while(t--)
{
int n,m,lim; cin>>n>>m>>lim;
vector<int> v,o;
queue<pair<int,int> > q;
for(int i=0;i<n;++i)
{
int x; cin>>x;
v.push_back(x);
q.push(make_pair(x,1));
}
for(int i=0;i<m;++i)
{
int x; cin>>x;
o.push_back(x);
}
int y; cin>>y;
pair<int,int> re;
while(!q.empty())
{
int x = q.front().first;
int l = q.front().second;
if(x==y)
{
re.first = y;
re.second = l;
break;
}
q.pop();
if(!vis[x])
{
for(int i=0;i<n;++i)
{
q.push(make_pair(x*10 + v[i],l+1));
}
for(int i=0;i<m;++i)
{
for(int j=0;j<n;++j)
{
if(o[i]==1)
{
q.push(make_pair(x+v[j],l+3));
}
else if(o[i]==2)
{
q.push(make_pair(x-v[j],l+3));
}
else if(o[i]==3)
{
q.push(make_pair(x*v[j],l+3));
}
else if(o[i]==4)
{
q.push(make_pair(x/v[j],l+3));
}
}
}
vis[x] = true;
}
}
cout<<re.second<<endl;
}
}
```

It can be solved easily using dynamic programming in O(n^4) time and O(2*n^3) space.

- Namit September 08, 2018The idea is first double the size of array and append a copy of it in back..

now the idea is

dividing array from i to any index j in k groups and store its min and max value....

If you still need help mail me at namit.pasrija@gmail.com