原文:

给定一个大小为 n 的数组 arr[] 和一个数字 k 。任务是在 k 操作后打印数组 arr[] ,以便在每次操作时,将数组中的每个元素 arr[i] 替换为max–arr[i],其中 max 是数组中的最大元素。 举例:

输入: arr[] = {4,8,12,16},k = 4 输出: 0 4 8 12 解释: 对于给定的数组 arr[] = {4,8,12,16 }。对于每个操作 k,数组的变化如下: { 12,8,4,0 }–k = 1 { 0,4,8,12 }–k = 2 { 12,8,4,0 }–k = 3 { 0,4,8,12 }–k = 4 输入: arr[] = {8,0,3,5},k = 3 输出: 0 每个操作 k 的数组变化如下: { 0,8,5,3 }–k = 1 { 8,0,3,5 }–k = 2 { 0,8,5,3 }–k = 3

进场:思路是每走一步后清晰观察阵面。

  1. 最初,因为我们从数组中减去最大元素,所以我们可以确定数组中至少有一个元素的值为零。这发生在第一步 k = 1 之后。让这一步后的阵为 a[] 带最大元素 m
  2. 在此第一步之后,阵列变得停滞,并且值交替地从其值 a[i] 变为m–a[i]
  3. 例如,让我们取数组 arr[] = {4,8,12,16}。在这个数组中,最大值是 16,让我们假设 k = 4。
    • 第一步,(即)对于 k = 1 ,数组简化为 {12,8,4,0} 。也就是说,每个元素 arr[i]都被替换为 16–arr[i]。设这个数组是 a[],这个数组 m 的最大元素是 12。
    • 第一步后,该数组中的每个元素 a[i]a[i]12–a[i]之间交替变化。也就是说,对于 k = 2 ,数组现在变成 {0,4,8,12}
    • 同样,对于第三步,(即) k = 3 ,数组再次变为 {12,8,4,0} ,与 k = 1 相同。
    • 而对于第四步,(即) k = 4 ,数组变为 {0,4,8,12} ,与 k = 2 等相同。

所以可以得出结论,我们只需要检查 k 是奇数还是偶数:

  • 如果 k 是奇数:将每个元素arr【i】替换为arr【i】–min,其中 min 是数组中的最小元素。
  • 如果 k 是偶数:将每个元素arr【i】替换为max–arr【i】,其中 max 是数组中最大的元素。

以下是上述方法的实现:

卡片打印处理机(card print processor 的缩写)

// c   program to print the array
// after k operations
#include 
using namespace std;
// function to print the array
// after k operations
void printarray(int a[], int n, int k)
{
    // variables to store the minimum and
    // the maximum elements of the array
    int minele = int_max,
        maxele = int_min;
    // loop to find the minimum and the
    // maximum elements of the array
    for (int i = 0; i < n; i  ) {
        minele = min(minele, a[i]);
        maxele = max(maxele, a[i]);
    }
    // if k is not equal to 0
    if (k != 0) {
        // if k is odd
        if (k % 2 == 1) {
            // replace every element with
            // max - arr[i]
            for (int i = 0; i < n; i  )
                a[i] = maxele - a[i];
        }
        // if k is even
        else {
            // replace every element with
            // a[i] - min
            for (int i = 0; i < n; i  )
                a[i] = a[i] - minele;
        }
    }
    // printing the array after k operations
    for (int i = 0; i < n; i  )
        cout << a[i] << " ";
}
// driver code
int main()
{
    int arr[] = { 4, 8, 12, 16 };
    int k = 4;
    int n = sizeof(arr) / sizeof(arr[0]);
    printarray(arr, n, k);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to print the array
// after k operations
import java.io.*;
class gfg {
    // function to print the array
    // after k operations
    static void printarray(int[] a, int n, int k)
    {
        // variables to store the minimum and
        // the maximum elements of the array
        int minele = integer.max_value,
            maxele = integer.max_value;
        // loop to find the minimum and the
        // maximum elements of the array
        for (int i = 0; i < n; i  ) {
            minele = math.min(minele, a[i]);
            maxele = math.max(maxele, a[i]);
        }
        // if k is not equal to 0
        if (k != 0) {
            // if k is odd
            if (k % 2 == 1) {
                // replace every element with
                // max - arr[i]
                for (int i = 0; i < n; i  )
                    a[i] = maxele - a[i];
            }
            // if k is even
            else {
                // replace every element with
                // a[i] - min
                for (int i = 0; i < n; i  )
                    a[i] = a[i] - minele;
            }
        }
        // printing the array after k operations
        for (int i = 0; i < n; i  )
            system.out.print(a[i]   " ");
    }
    // driver code
    public static void main (string[] args)
    {
        int[] arr = { 4, 8, 12, 16 };
        int k = 4;
        int n = arr.length;
        printarray(arr, n, k);
    }
}
// this code is contributed by shivanisinghss2110

python 3

# python3 program to print the array
# after k operations
# function to print the array
# after k operations
def printarray(a, n, k):
    # variables to store the minimum and
    # the maximum elements of the array
    minele = 10**9
    maxele = -10**9
    # loop to find the minimum and the
    # maximum elements of the array
    for i in range(n):
        minele = min(minele, a[i])
        maxele = max(maxele, a[i])
    # if k is not equal to 0
    if (k != 0):
        # if k is odd
        if (k % 2 == 1):
            # replace every element with
            # max - arr[i]
            for i in range(n):
                a[i] = maxele - a[i]
        # if k is even
        else:
            # replace every element with
            # a[i] - min
            for i in range(n):
                a[i] = a[i] - minele
    # printing the array after k operations
    for i in a:
        print(i, end=" ")
# driver code
if __name__ == '__main__':
    arr=[4, 8, 12, 16]
    k = 4
    n = len(arr)
    printarray(arr, n, k)
# this code is contributed by mohit kumar 29

c

// c# program to print the array
// after k operations
using system;
class gfg{
    // function to print the array
    // after k operations
    static void printarray(int[] a, int n, int k)
    {
        // variables to store the minimum and
        // the maximum elements of the array
        int minele = int32.maxvalue,
            maxele = int32.minvalue;
        // loop to find the minimum and the
        // maximum elements of the array
        for (int i = 0; i < n; i  ) {
            minele = math.min(minele, a[i]);
            maxele = math.max(maxele, a[i]);
        }
        // if k is not equal to 0
        if (k != 0) {
            // if k is odd
            if (k % 2 == 1) {
                // replace every element with
                // max - arr[i]
                for (int i = 0; i < n; i  )
                    a[i] = maxele - a[i];
            }
            // if k is even
            else {
                // replace every element with
                // a[i] - min
                for (int i = 0; i < n; i  )
                    a[i] = a[i] - minele;
            }
        }
        // printing the array after k operations
        for (int i = 0; i < n; i  )
            console.write(a[i]   " ");
    }
    // driver code
    static public void main ()
    {
        int[] arr = { 4, 8, 12, 16 };
        int k = 4;
        int n = arr.length;
        printarray(arr, n, k);
    }
}
// this code is contributed by shubhamsingh10

java 描述语言


output: 

0 4 8 12

时间复杂度: o(n),其中 n 是数组的大小。