给定一个由n个整数和一个整数K组成的数组arr [] 。在一个操作中,将数组中的每个元素替换为该元素与数组最大值的差。任务是在执行K次操作后打印阵列。
例子:
Input: arr[] = {4, 8, 12, 16}, k = 2
Output: 0 4 8 12
k = 1, arr[] = {12, 8, 4, 0}
k = 2, arr[] = {0, 4, 8, 12}
Input: arr[] = {14, 28, 14, 106, 223}, k = 12
Output: 0 14 0 92 209
方法:有两种可能的情况:
- 如果k为奇数,则对于范围[0,n)中的所有i ,新数组将为max(arr)– arr [i] 。
- 如果k为偶数,则对于范围[0,n)中的所有i ,新数组将为arr [i] – min(arr ) 。
例如:
Let arr[] = {4, 8, 12, 16}
For k = 1, arr[] = (12, 8, 4, 0}
For k = 2, arr[] = (0, 4, 8, 12}
For k = 3, arr[] = (12, 8, 4, 0}
For k = 4, arr[] = (0, 4, 8, 12}
It can be observed that the array is repeating after every 2 operations.
下面是上述方法的实现:
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)