Replace Middle

Description:

We define the middle of the array arr as follows:

  • if arr contains an odd number of elements, its middle is the element whose index number is the same when counting from the beginning of the array and from its end;
  • if arr contains an even number of elements, its middle is the sum of the two elements whose index numbers when counting from the beginning and from the end of the array differ by one.

Given array arr, your task is to find its middle, and, if it consists of two elements, replace those elements with the value of middle. Return the resulting array as the answer.

Example

  • For arr = [7, 2, 2, 5, 10, 7], the output should be
    replaceMiddle(arr) = [7, 2, 7, 10, 7].

    The middle consists of two elements, 2 and 5. These two elements should be replaced with their sum, i.e. 7.

  • For arr = [-5, -5, 10], the output should be
    replaceMiddle(arr) = [-5, -5, 10].

    The middle is defined as a single element -5, so the initial array with no changes should be returned.

Input/Output

  • [time limit] 3000ms (cs)
  • [input] array.integer arr

    The given array.

    Constraints:
    2 ≤ arr.length ≤ 104,
    -103 ≤ arr[i] ≤ 103.

  • [output] array.integer

    arr with its middle replaced by a single element, or the initial array if themiddle consisted of a single element to begin with.

Tests:
Solution:

int[] replaceMiddle(int[] arr) {    
    if(arr.Length%2==0) {
        List<int>result=new List<int>();
        for(int index=0;index<arr.Length;index++) {
            if(index==arr.Length/2-1) {
                result.Add(arr[index]+arr[index+1]);
            }
            else if(index!=arr.Length/2) {
                result.Add(arr[index]);
            }
        }
        return result.ToArray();
    }
    else {
        return arr;
    }    
}

Make Array Consecutive 2

Description:

Given an array of integers, we need to find the number of “holes” that need to be filled such that it contains all the integers from some range.

Example

For sequence = [6, 2, 3, 8], the output should be
makeArrayConsecutive2(sequence) = 3.

We need to add in 4, 5 and 7.

Input/Output

  • [time limit] 3000ms (cs)
  • [input] array.integer sequence

    An array of distinct integers.

    Constraints:
    1 ≤ sequence.length ≤ 10,
    -10 ≤ sequence[i] ≤ 10.

  • [output] integer

    The minimal number of integers that need to be added to sequence such that it contains every integer from an interval [L, R] (for some L, R) and no other integers.

Tests:
Solution:

int makeArrayConsecutive2(int[] sequence) {
    Array.Sort(sequence);
    int holes=0;
    for (int i = sequence[0]; i <= sequence[sequence.Length - 1]; i++)
        if(!sequence.Contains(i))
            holes++;
    return holes;
}

Is Smooth?

Description:

We define the middle of the array arr as follows:

  • if arr contains an odd number of elements, its middle is the element whose index number is the same when counting from the beginning of the array and from its end;
  • if arr contains an even number of elements, its middle is the sum of the two elements whose index numbers when counting from the beginning and from the end of the array differ by one.

An array is called smooth if its first and its last elements are equal to one another and to the middle. Given an array arr, determine if it is smooth or not.

Example

  • For arr = [7, 2, 2, 5, 10, 7], the output should be
    isSmooth(arr) = true.

    The first and the last elements of arr are equal to 7, and its middle also equals 2 + 5 = 7. Thus, the array is smooth and the output is true.

  • For arr = [-5, -5, 10], the output should be
    isSmooth(arr) = false.

    The first and middle elements are equal to -5, but the last element equals10. Thus, arr is not smooth and the output is false.

Input/Output

  • [time limit] 3000ms (cs)
  • [input] array.integer arr

    The given array.

    Constraints:
    2 ≤ arr.length ≤ 105,
    -109 ≤ arr[i] ≤ 109.

  • [output] boolean

    true if arr is smooth, false otherwise.

Tests:
Solution:

bool isSmooth(int[] arr) {
    if(arr.Length%2==0) {
        return arr[0]==arr[arr.Length-1] && arr[0]== (arr[arr.Length/2-1]+arr[arr.Length/2]);
    }
    else {
        return arr[0]==arr[arr.Length-1] && arr[0]== arr[(arr.Length-1)/2];
    }
}

Remove Array Part

Description:

Remove a part of a given array between given 0-based indexes l and r(inclusive).

Example

For inputArray = [2, 3, 2, 3, 4, 5], l = 2 and r = 4, the output should be
removeArrayPart(inputArray, l, r) = [2, 3, 5].

Input/Output

  • [time limit] 3000ms (cs)
  • [input] array.integer inputArray

    Constraints:
    2 ≤ inputArray.length ≤ 10,
    -10 ≤ inputArray[i] ≤ 10.

  • [input] integer l

    Left index of the part to be removed (0-based).

    Constraints:
    0 ≤ l ≤ r.

  • [input] integer r

    Right index of the part to be removed (0-based).

    Constraints:
    l ≤ r < inputArray.length.

  • [output] array.integer

Tests:
Solution:

int[] removeArrayPart(int[] inputArray, int l, int r) {
    List<int>result = new List<int>();
    for(int index=0;index<Math.Min(l,r);index++) {
        result.Add(inputArray[index]);
    }
    for(int index=Math.Max(l,r)+1;index<inputArray.Length;index++) {
        result.Add(inputArray[index]);
    }
    return result.ToArray();
}

Concatenate Arrays

Description:

Given two arrays of integers a and b, obtain the array formed by the elements of a followed by the elements of b.

Example

For a = [2, 2, 1] and b = [10, 11], the output should be
concatenateArrays(a, b) = [2, 2, 1, 10, 11].

Input/Output

  • [time limit] 3000ms (cs)
  • [input] array.integer a

    Constraints:
    1 ≤ a.length ≤ 10,
    1 ≤ a[i] ≤ 15.

  • [input] array.integer b

    Constraints:
    0 ≤ b.length ≤ 10,
    1 ≤ b[i] ≤ 15.

  • [output] array.integer

Tests:
Solution:

int[] concatenateArrays(int[] a, int[] b) {
    List<int>result=new List<int>();
    foreach(int item in a)
        result.Add(item);
    foreach(int item in b)
        result.Add(item);
    return result.ToArray();
}

First Reverse Try

Description:

Reversing an array can be a tough task, especially for a novice programmer. Mary just started coding, so she would like to start with something basic at first. Instead of reversing the array entirely, she wants to swap just its first and last elements.

Given an array arr, swap its first and last elements and return the resulting array.

Example

For arr = [1, 2, 3, 4, 5], the output should be
firstReverseTry(arr) = [5, 2, 3, 4, 1].

Input/Output

  • [time limit] 3000ms (cs)
  • [input] array.integer arr

    Constraints:
    0 ≤ arr.length ≤ 50,
    -104 ≤ arr[i] ≤ 104.

  • [output] array.integer

    Array arr with its first and its last elements swapped.

Tests:
Solution:

int[] firstReverseTry(int[] arr) {
    if(arr.Length>1) {
        int temp = arr[0];
        arr[0] = arr[arr.Length-1];
        arr[arr.Length-1] = temp;
    }
    return arr;
}

Array Replace

Description:

Given an array of integers, replace all the occurrences of elemToReplace withsubstitutionElem.

Example

For inputArray = [1, 2, 1], elemToReplace = 1 and substitutionElem = 3, the output should be
arrayReplace(inputArray, elemToReplace, substitutionElem) = [3, 2, 3].

Input/Output

  • [time limit] 3000ms (cs)
  • [input] array.integer inputArray

    Constraints:
    2 ≤ inputArray.length ≤ 10,
    0 ≤ inputArray[i] ≤ 10.

  • [input] integer elemToReplace

    Constraints:
    0 ≤ elemToReplace ≤ 10.

  • [input] integer substitutionElem

    Constraints:
    0 ≤ substitutionElem ≤ 10.

  • [output] array.integer

Tests:
Solution:

int[] arrayReplace(int[] inputArray, int elemToReplace, int substitutionElem) {
    for(int index=0;index<inputArray.Length;index++) {
        if(inputArray[index]==elemToReplace)
            inputArray[index]=substitutionElem;
    }
    return inputArray;
}