## Amazon Interview Question

SDE-2s**Country:**India

**Interview Type:**In-Person

```
/**
*
* @param input Array of positive numbers in the sorted order.
* @param x
*/
private static void editArray(int[] input, int x) {
int lCount = 0;
for (int i = 0, j = -1, k = 0; i < input.length; i++) {
if (input[i] == x) {
lCount++;
if (lCount > 2) {
if (j == -1) {
j = i;
}
input[i] = 0;
}
} else if (lCount > 0) {
if (j != -1) {
k = j;
j = -1;
}
input[k++] = input[i];
input[i] = 0;
}
}
}
```

```
/**
*
* @param input Array of positive numbers in the sorted order.
* @param x
*/
private static void editArray(int[] input, int x) {
int lCount = 0;
for (int i = 0, j = -1, k = 0; i < input.length; i++) {
if (input[i] == x) {
lCount++;
if (lCount > 2) {
if (j == -1) {
j = i;
}
input[i] = 0;
}
} else if (lCount > 0) {
if (j != -1) {
k = j;
j = -1;
}
input[k++] = input[i];
input[i] = 0;
}
}
}
```

```
/**
*
* @param input Array of positive numbers in the sorted order.
* @param x
*/
private static void editArray(int[] input, int x) {
int lCount = 0;
for (int i = 0, j = -1, k = 0; i < input.length; i++) {
if (input[i] == x) {
lCount++;
if (lCount > 2) {
if (j == -1) {
j = i;
}
input[i] = 0;
}
} else if (lCount > 0) {
if (j != -1) {
k = j;
j = -1;
}
input[k++] = input[i];
input[i] = 0;
}
}
}
```

```
/**
*
* @param input Array of positive numbers in the sorted order.
* @param x
*/
private static void editArray(int[] input, int x) {
int lCount = 0;
for (int i = 0, j = -1, k = 0; i < input.length; i++) {
if (input[i] == x) {
lCount++;
if (lCount > 2) {
if (j == -1) {
j = i;
}
input[i] = 0;
}
} else if (lCount > 0) {
if (j != -1) {
k = j;
j = -1;
}
input[k++] = input[i];
input[i] = 0;
}
}
}
```

```
/**
*
* @param input Array of positive numbers in the sorted order.
* @param x
*/
private static void editArray(int[] input, int x) {
int lCount = 0;
for (int i = 0, j = -1, k = 0; i < input.length; i++) {
if (input[i] == x) {
lCount++;
if (lCount > 2) {
if (j == -1) {
j = i;
}
input[i] = 0;
}
} else if (lCount > 0) {
if (j != -1) {
k = j;
j = -1;
}
input[k++] = input[i];
input[i] = 0;
}
}
}
```

Javascript solution

```
var arr = [1,2,2,2,2,3,4];
var m = 4;
var cur = 0, i, currElement = -1;
var count = 0;
console.log(arr)
for(var i = 0;i < arr.length;i++)
{
// set the current element
if (currElement != arr[i])
{
currElement = arr[i];
count = 0;
}
// the moment count is > than min, it wil stop incrementing the "curr" pointer
// and wait for next condition to match
// so that subsequent filling will resume and start removing the duplicates.
if (currElement == arr[i] && count < Math.min(m, 2))
{
count++;
// start filling the array with matching value
arr[cur] = arr[i];
cur++;
}
}
for(var i = cur;i < arr.length;i++) {
arr[i] = 0;
}
console.log(arr)
```

Java Solution

```
public static void editArray(int[] arr, int m) {
int curElm = 0, curIndx = 0, count = 0;
for(int i = 0; i < arr.length; i++) {
if(curElm == arr[i]) {
count++;
} else count = 1;
if(count <= Math.min(2, m)) {
arr[curIndx] = arr[i];
curElm = arr[i];
curIndx++;
}
}
while(curIndx < arr.length) {
arr[curIndx] = 0;
curIndx++;
}
}
```

```
void editArray(vector<int>& arr, int m)
{
auto p0 = arr.begin();
auto p1 = arr.begin();
while (p1 != arr.end())
{
auto er = equal_range(p1, arr.end(), *p1);
auto p2 = er.second;
int sz = distance(p1, p2);
if (sz == m)
sz = min(m, 2);
p0 = copy(p1, p1 + sz, p0);
p1 = p2;
}
arr.erase(p0, arr.end());
}
```

```
private static void EditArray(int[] arr, int x)
{
int lCount = 0;
for (int i = 0, j = -1, k = 0; i < arr.Length; i++)
{
if (arr[i] == x)
{
lCount++;
if (lCount > 2)
{
if (j == -1)//Set the position of i starting where lCount>2, i.e. we are going to assign the 0 to arr[i]
{
j = i;
}
arr[i] = 0;
}
}
else if (lCount > 0)//if x is found more than x
{
if (j != -1)//if j has been assigned the value of i, i.e. position where and on we have set arr[i] to 0
{
k = j;
j = -1;
}
if (k > 0)
{
arr[k++] = arr[i];
arr[i] = 0;
}
}
}
```

}

- B_K_J April 18, 2017