📜  K次操作后打印数组

📅  最后修改于: 2021-09-06 05:31:21             🧑  作者: Mango

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

方法:思路是在每一步之后都清楚地观察数组。

  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 是数组中的最大元素。

下面是上述方法的实现:

CPP
// 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


Python3
# 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


Javascript


输出:
0 4 8 12

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live