📌  相关文章
📜  通过将给定 Array 的每个元素乘以 K 来生成一个 Array

📅  最后修改于: 2022-05-13 01:56:08.525000             🧑  作者: Mango

通过将给定 Array 的每个元素乘以 K 来生成一个 Array

给定一个大小为N的数组arr[]和一个整数K 。任务是将数组的每个元素乘以K

例子 :

方法:可以使用以下步骤解决给定的问题:

  • 遍历列表中的所有元素
  • 将每个元素乘以 K
  • 返回修改后的列表

下面是上述方法的实现。

C++
// C++ code for the above approach
#include 
using namespace std;
 
// Function to multiply all
// the elements of array by K
void multiplyAllByK(vector arr, int K)
{
    int N = arr.size();
 
    // Loop to multiply all
    // the array elements
    for (int i = 0; i < N; i++) {
        int x = arr[i];
        arr[i] = K * x;
    }
 
    // Print the modified array
    for (int i = 0; i < N; i++)
        cout << (arr[i]) << " ";
}
 
// Driver code
int main()
{
 
    vector arr;
    arr.push_back(3);
    arr.push_back(4);
    int K = 2;
    multiplyAllByK(arr, K);
    return 0;
}
 
// This code is contributed by lokeshpotta20.


Java
// Java code to implement above approach
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to multiply all
    // the elements of array by K
    public static void multiplyAllByK(
        ArrayList arr, int K)
    {
        int N = arr.size();
 
        // Loop to multiply all
        // the array elements
        for (int i = 0; i < N; i++) {
            int x = arr.get(i);
            arr.set(i, K * x);
        }
 
        // Print the modified array
        for (int i = 0; i < N; i++)
            System.out.print(arr.get(i) + " ");
    }
 
    // Driver code
    public static void main(String[] args)
    {
        ArrayList arr = new ArrayList();
        arr.add(3);
        arr.add(4);
        int K = 2;
        multiplyAllByK(arr, K);
    }
}


Python
# Python code to implement above approach
 
# Function to multiply all
# the elements of array by K
def multiplyAllByK(arr, K):
     
    n = len(arr)
     
    for i in range(n):
        x = arr[i]
        arr[i] = K * x
         
    for i in range(n):
        print(arr[i], end = ' ')
 
# Driver code
arr = [3, 4]
K = 2
multiplyAllByK(arr, K)
 
# This code is contributed by Samim Hossain Mondal.


C#
// C# code to implement above approach
 
using System;
using System.Collections.Generic;
 
public class GFG {
 
    // Function to multiply all
    // the elements of array by K
    public static void multiplyAllByK(
        List arr, int K)
    {
        int N = arr.Count;
 
        // Loop to multiply all
        // the array elements
        for (int i = 0; i < N; i++) {
            int x = arr[i];
            arr[i] =( K * x);
        }
 
        // Print the modified array
        for (int i = 0; i < N; i++)
            Console.Write(arr[i] + " ");
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        List arr = new List();
        arr.Add(3);
        arr.Add(4);
        int K = 2;
        multiplyAllByK(arr, K);
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


C++
// C++ code to implement above approach
#include 
using namespace std;
 
// Function to multiply all
// the elements of array by K
void multiplyAllByK(int arr[], int K)
{
  for(int i = 0; i < 2; i++)
    arr[i] *= K;
  for (int i = 0; i < 2; i++)
    cout << arr[i] << " ";
}
 
// Driver code
int main()
{
  int arr[2];
  arr[0] = 3;
  arr[1] = 4;
  int K = 2;
  multiplyAllByK(arr, K);
 
  return 0;
}
 
// This code is contributed by Shubham Singh


C
// C code to implement above approach
#include 
 
// Function to multiply all
// the elements of array by K
void multiplyAllByK(int arr[], int K)
{
    for(int i = 0; i < 2; i++) arr[i] *= K;
    for (int i = 0; i<2; i++)
        printf("%d ",arr[i]);
}
 
// Driver code
int main()
{
    int arr[2];
    arr[0] = 3;
    arr[1] = 4;
    int K = 2;
    multiplyAllByK(arr, K);
     
    return 0;
}
 
//This code is contributed by Shubham Singh


Java
// Java code to implement above approach
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to multiply all
    // the elements of array by K
    public static void multiplyAllByK(
        ArrayList arr, int K)
    {
        arr.replaceAll(n -> n * K);
        for (Integer x : arr)
            System.out.print(x + " ");
    }
 
    // Driver code
    public static void main(String[] args)
    {
        ArrayList arr
            = new ArrayList();
        arr.add(3);
        arr.add(4);
        int K = 2;
        multiplyAllByK(arr, K);
    }
}


Python3
# Python3 code to implement above approach
 
# Function to multiply all
# the elements of array by K
def multiplyAllByK(arr, K):
         
    lambda_func = lambda n: n * K
    for i in range(len(arr)):
        print(lambda_func(arr[i]), end = ' ')
 
# Driver code
arr = [3, 4]
K = 2
 
multiplyAllByK(arr, K)
 
# This code is contributed by Samim Hossain Mondal.


C#
// C# code to implement above approach
using System;
using System.Collections.Generic;
using System.Linq;
 
public class GFG {
 
    // Function to multiply all
    // the elements of array by K
    public static void multiplyAllByK(
        List arr, int K)
    {
        var temp = arr.Select(n => n * K);
        foreach (int x in temp)
            Console.Write(x + " ");
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        List arr
            = new List();
        arr.Add(3);
        arr.Add(4);
        int K = 2;
        multiplyAllByK(arr, K);
    }
}
 
// This code is contributed by shikhasingrajput


Javascript


输出
6 8 

时间复杂度: O(N)
辅助空间: O(1)

使用 Lambda 表达式的方法:这也可以使用 lambda 表达式来实现。

下面是上述方法的实现:

C++

// C++ code to implement above approach
#include 
using namespace std;
 
// Function to multiply all
// the elements of array by K
void multiplyAllByK(int arr[], int K)
{
  for(int i = 0; i < 2; i++)
    arr[i] *= K;
  for (int i = 0; i < 2; i++)
    cout << arr[i] << " ";
}
 
// Driver code
int main()
{
  int arr[2];
  arr[0] = 3;
  arr[1] = 4;
  int K = 2;
  multiplyAllByK(arr, K);
 
  return 0;
}
 
// This code is contributed by Shubham Singh

C

// C code to implement above approach
#include 
 
// Function to multiply all
// the elements of array by K
void multiplyAllByK(int arr[], int K)
{
    for(int i = 0; i < 2; i++) arr[i] *= K;
    for (int i = 0; i<2; i++)
        printf("%d ",arr[i]);
}
 
// Driver code
int main()
{
    int arr[2];
    arr[0] = 3;
    arr[1] = 4;
    int K = 2;
    multiplyAllByK(arr, K);
     
    return 0;
}
 
//This code is contributed by Shubham Singh

Java

// Java code to implement above approach
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to multiply all
    // the elements of array by K
    public static void multiplyAllByK(
        ArrayList arr, int K)
    {
        arr.replaceAll(n -> n * K);
        for (Integer x : arr)
            System.out.print(x + " ");
    }
 
    // Driver code
    public static void main(String[] args)
    {
        ArrayList arr
            = new ArrayList();
        arr.add(3);
        arr.add(4);
        int K = 2;
        multiplyAllByK(arr, K);
    }
}

Python3

# Python3 code to implement above approach
 
# Function to multiply all
# the elements of array by K
def multiplyAllByK(arr, K):
         
    lambda_func = lambda n: n * K
    for i in range(len(arr)):
        print(lambda_func(arr[i]), end = ' ')
 
# Driver code
arr = [3, 4]
K = 2
 
multiplyAllByK(arr, K)
 
# This code is contributed by Samim Hossain Mondal.

C#

// C# code to implement above approach
using System;
using System.Collections.Generic;
using System.Linq;
 
public class GFG {
 
    // Function to multiply all
    // the elements of array by K
    public static void multiplyAllByK(
        List arr, int K)
    {
        var temp = arr.Select(n => n * K);
        foreach (int x in temp)
            Console.Write(x + " ");
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        List arr
            = new List();
        arr.Add(3);
        arr.Add(4);
        int K = 2;
        multiplyAllByK(arr, K);
    }
}
 
// This code is contributed by shikhasingrajput

Javascript


输出
6 8 

时间复杂度: O(N)
辅助空间: O(1)