Encuentre la array modificada después de realizar k operaciones de tipo dado

Dada una array arr[] de n enteros y un entero K . En una operación, cada elemento de la array se reemplaza por la diferencia de ese elemento y el valor máximo de la array. La tarea es imprimir la array después de realizar operaciones K.
Ejemplos: 
 

Entrada: arr[] = {4, 8, 12, 16}, k = 2 
Salida: 0 4 8 12 
k = 1, arr[] = {12, 8, 4, 0} 
k = 2, arr[] = {0, 4, 8, 12}
Entrada: arr[] = {14, 28, 14, 106, 223}, k = 12 
Salida: 0 14 0 92 209 
 

Enfoque: Hay dos casos posibles: 
 

  1. Si k es impar , la nueva array será max(arr) – arr[i] para todos los i en el rango [0, n) .
  2. Si k es par , entonces la nueva array será arr[i] – min(arr) para todos los i en el rango [0, n) .

Por ejemplo: 
 

Sea arr[] = {4, 8, 12, 16} 
Para k = 1, arr[] = (12, 8, 4, 0} 
Para k = 2, arr[] = (0, 4, 8, 12} 
Para k = 3, arr[] = (12, 8, 4, 0} 
Para k = 4, arr[] = (0, 4, 8, 12} 
Se puede observar que el arreglo se repite cada 2 operaciones. 
 

A continuación se muestra la implementación del enfoque anterior:
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
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

<?php
// PHP implementation of the approach
 
// Utility function to print
// the contents of an array
function printArray($arr, $n)
{
    for ($i = 0; $i < $n; $i++)
        echo $arr[$i] . " ";
}
 
// Function to remove the minimum value of
// the array from every element of the array
function removeMin(&$arr, $n)
{
    $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
function removeFromMax(&$arr, $n)
{
    $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
function 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
$arr = array( 4, 8, 12, 16 );
$n = count($arr);
$k = 2;
modifyArray($arr, $n, $k);
 
// This code is contributed by mits
?>

Javascript

<script>
// Javascript implementation of the approach
 
// Utility function to print
// the contents of an array
function printArray(arr, n)
{
    for (var i = 0; i < n; i++)
        document.write(arr[i] + " ");
}
 
// Function to remove the minimum value of
// the array from every element of the array
function removeMin(arr, n)
{
    var 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
function removeFromMax(arr, n)
{
    var 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
function 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
arr = [ 4, 8, 12, 16 ]
var n = arr.length;
var k = 2;
modifyArray(arr, n, k);
 
// This code is contributed by noob2000.
</script>
Producción: 

0 4 8 12

 

Complejidad de tiempo: O(n), donde n representa el tamaño de la array dada.
Espacio auxiliar: O(1), no se requiere espacio adicional, por lo que es una constante.

Publicación traducida automáticamente

Artículo escrito por spp____ y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *