📌  相关文章
📜  在执行给定类型的k次操作后找到修改后的数组

📅  最后修改于: 2021-04-23 20:26:53             🧑  作者: Mango

给定一个由n个整数和一个整数K组成的数组arr [] 。在一个操作中,将数组中的每个元素替换为该元素与数组最大值的差。任务是在执行K次操作后打印阵列。
例子:

方法:有两种可能的情况:

  1. 如果k为奇数,则对于范围[0,n)中的所有i ,新数组将为max(arr)– arr [i]
  2. 如果k为偶数,则对于范围[0,n)中的所有i ,新数组将为arr [i] – min(arr )

例如:

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Utility function to print
// the contents of an array
void printArray(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
 
// Function to remove the minimum value of
// the array from every element of the array
void removeMin(int arr[], int n)
{
    int i, minVal = arr[0];
 
    // Get the minimum value from the array
    for (i = 1; i < n; i++)
        minVal = min(minVal, arr[i]);
 
    // Remove the minimum value from
    // every element of the array
    for (i = 0; i < n; i++)
        arr[i] = arr[i] - minVal;
}
 
// Function to remove every element of the
// array from the maximum value of the array
void removeFromMax(int arr[], int n)
{
    int i, maxVal = arr[0];
 
    // Get the maximum value from the array
    for (i = 1; i < n; i++)
        maxVal = max(maxVal, arr[i]);
 
    // Remove every element of the array from
    // the maximum value of the array
    for (i = 0; i < n; i++)
        arr[i] = maxVal - arr[i];
}
 
// Function to print the modified array
// after k operations
void modifyArray(int arr[], int n, int k)
{
 
    // If k is odd then remove the minimum element
    // of the array from every element of the array
    if (k % 2 == 0)
        removeMin(arr, n);
 
    // Else remove every element of the array from
    // the maximum value from the array
    else
        removeFromMax(arr, n);
 
    // Print the modified array
    printArray(arr, n);
}
 
// Driver code
int main()
{
    int arr[] = { 4, 8, 12, 16 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 2;
    modifyArray(arr, n, k);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG {
 
    // Utility function to print
    // the contents of an array
    static void printArray(int arr[], int n)
    {
        for (int i = 0; i < n; i++)
            System.out.print(arr[i] + " ");
    }
 
    // Function to remove the minimum value of
    // the array from every element of the array
    static void removeMin(int arr[], int n)
    {
        int i, minVal = arr[0];
 
        // Get the minimum value from the array
        for (i = 1; i < n; i++)
            minVal = Math.min(minVal, arr[i]);
 
        // Remove the minimum value from
        // every element of the array
        for (i = 0; i < n; i++)
            arr[i] = arr[i] - minVal;
    }
 
    // Function to remove every element of the
    // array from the maximum value of the array
    static void removeFromMax(int arr[], int n)
    {
        int i, maxVal = arr[0];
 
        // Get the maximum value from the array
        for (i = 1; i < n; i++)
            maxVal = Math.max(maxVal, arr[i]);
 
        // Remove every element of the array from
        // the maximum value of the array
        for (i = 0; i < n; i++)
            arr[i] = maxVal - arr[i];
    }
 
    // Function to print the modified array
    // after k operations
    static void modifyArray(int arr[], int n, int k)
    {
 
        // If k is odd then remove the minimum element
        // of the array from every element of the array
        if (k % 2 == 0)
            removeMin(arr, n);
 
        // Else remove every element of the array from
        // the maximum value from the array
        else
            removeFromMax(arr, n);
 
        // Print the modified array
        printArray(arr, n);
    }
 
    // Driver code
    public static void main(String args[])
    {
        int arr[] = { 4, 8, 12, 16 };
        int n = arr.length;
        int k = 2;
        modifyArray(arr, n, k);
    }
}


Python3
# Python 3 implementation of the approach
 
# Utility function to print
# the contents of an array
def printArray(arr, n) :
 
    for i in range(n) :
        print(arr[i], end = " ");
 
# Function to remove the minimum
# value of the array from every
# element of the array
def removeMin(arr, n) :
     
    minVal = arr[0];
 
    # Get the minimum value from
    # the array
    for i in range(1, n) :
        minVal = min(minVal, arr[i]);
 
    # Remove the minimum value from
    # every element of the array
    for i in range(n) :
        arr[i] = arr[i] - minVal;
 
# Function to remove every element
# of the array from the maximum
# value of the array
def removeFromMax(arr, n) :
     
    maxVal = arr[0];
 
    # Get the maximum value from
    # the array
    for i in range(1, n) :
        maxVal = max(maxVal, arr[i]);
 
    # Remove every element of the
    # array from the maximum value
    # of the array
    for i in range(n) :
        arr[i] = maxVal - arr[i];
 
# Function to print the modified 
# array after k operations
def modifyArray(arr, n, k) :
 
    # If k is odd then remove the minimum
    # element of the array from every
    # element of the array
    if (k % 2 == 0) :
        removeMin(arr, n);
 
    # Else remove every element of
    # the array from the maximum
    # value from the array
    else :
        removeFromMax(arr, n);
 
    # Print the modified array
    printArray(arr, n);
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 4, 8, 12, 16 ];
    n = len(arr)
     
    k = 2;
    modifyArray(arr, n, k);
 
# This code is contributed by Ryuga


C#
// C# implementation of the approach
using System;
class GFG {
 
    // Utility function to print
    // the contents of an array
    static void printArray(int[] arr, int n)
    {
        for (int i = 0; i < n; i++)
            Console.Write(arr[i] + " ");
    }
 
    // Function to remove the minimum value of
    // the array from every element of the array
    static void removeMin(int[] arr, int n)
    {
        int i, minVal = arr[0];
 
        // Get the minimum value from the array
        for (i = 1; i < n; i++)
            minVal = Math.Min(minVal, arr[i]);
 
        // Remove the minimum value from
        // every element of the array
        for (i = 0; i < n; i++)
            arr[i] = arr[i] - minVal;
    }
 
    // Function to remove every element of the
    // array from the maximum value of the array
    static void removeFromMax(int[] arr, int n)
    {
        int i, maxVal = arr[0];
 
        // Get the maximum value from the array
        for (i = 1; i < n; i++)
            maxVal = Math.Max(maxVal, arr[i]);
 
        // Remove every element of the array from
        // the maximum value of the array
        for (i = 0; i < n; i++)
            arr[i] = maxVal - arr[i];
    }
 
    // Function to print the modified array
    // after k operations
    static void modifyArray(int[] arr, int n, int k)
    {
 
        // If k is odd then remove the minimum element
        // of the array from every element of the array
        if (k % 2 == 0)
            removeMin(arr, n);
 
        // Else remove every element of the array from
        // the maximum value from the array
        else
            removeFromMax(arr, n);
 
        // Print the modified array
        printArray(arr, n);
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int[] arr = { 4, 8, 12, 16 };
        int n = arr.Length;
        int k = 2;
        modifyArray(arr, n, k);
    }
}


PHP


Javascript


输出:
0 4 8 12

时间复杂度: O(n)