Array Transformation

Array transformation refers to creating a new array from an existing array by changing, mapping or transforming elements based on some logic or rules.

Some examples of array transformations are:

  • Square each element
  • Increment each element by 1
  • Append/prepend fixed values
  • Subtract a set value from each element
  • Merge two arrays by summation/multiplication

Array transformations allow creating variations of arrays for different computational purposes while reusing the original data.

Solution

Here is how array transformation can be implemented:

Java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
int[] transform(int[] arr, int x) {
  
  int[] transformed = new int[arr.length];

  for(int i=0; i<arr.length; i++) {
    transformed[i] = arr[i] + x; 
  }

  return transformed;
}

C++

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
vector<int> transform(vector<int> arr, int x) {
  
  vector<int> transformed(arr.size());

  for(int i=0; i<arr.size(); i++) {
    transformed[i] = arr[i] + x;
  }

  return transformed; 
}

Python

1
2
3
4
5
6
7
def transform(arr, x):
  
  transformed = []
  for i in arr: 
    transformed.append(i + x)  

  return transformed

This takes an array and returns a new array with each element incremented by x.

We can customize this for any array transformation logic needed. Array transformations are useful for data manipulation.

Description

Array Transformation is a type of operation where elements of an array are transformed based on some rules or conditions. This concept is used in various types of problems where the goal is to transform an array into a specific form or pattern. The rules might involve adding, multiplying, or replacing elements, often based on the values of neighboring elements. Array transformation problems often require careful attention to boundary conditions.

Solution

Here, we’ll consider a simple example where we transform an array by replacing each element with the sum of its neighbors. The first and last elements are special cases as they have only one neighbor.

Java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class ArrayTransformation {
    public static int[] transform(int[] arr) {
        int n = arr.length;
        int[] result = new int[n];
        result[0] = arr[1];
        result[n-1] = arr[n-2];

        for (int i = 1; i < n-1; i++) {
            result[i] = arr[i-1] + arr[i+1];
        }

        return result;
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        int[] transformed = transform(arr);
        for (int num : transformed) {
            System.out.print(num + " ");
        }
    }
}

C++

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <vector>
using namespace std;

vector<int> transform(vector<int>& arr) {
    int n = arr.size();
    vector<int> result(n);
    result[0] = arr[1];
    result[n-1] = arr[n-2];
    
    for (int i = 1; i < n-1; i++) {
        result[i] = arr[i-1] + arr[i+1];
    }

    return result;
}

int main() {
    vector<int> arr = {1, 2, 3, 4, 5};
    vector<int> transformed = transform(arr);
    for (int num : transformed) {
        cout << num << " ";
    }
    return 0;
}

Python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
def transform(arr):
    n = len(arr)
    result = [0] * n
    result[0], result[-1] = arr[1], arr[-2]

    for i in range(1, n-1):
        result[i] = arr[i-1] + arr[i+1]

    return result

if __name__ == "__main__":
    arr = [1, 2, 3, 4, 5]
    transformed = transform(arr)
    print(" ".join(map(str, transformed)))

Key Takeaways

  • The boundary elements are handled separately because they have only one neighbor.
  • A new array is used to store the results so that the original array remains unmodified, making it easier to calculate the transformations.
  • The core logic remains the same across Java, C++, and Python, varying only in syntax and data structures.