Given an array arr[] of size N and an integer K. The task is to multiply each element of the array by K.
Examples :
Input: arr[] = { 3, 4 }, K = 2
Output: 6 8
Explanation: The elements become 3*2 = 6 and 4*2 = 8.Input: arr[] = { 0, 1, 2 }, K = 7
Output: { 0, 7, 14 }
Approach: The given problem can be solved using the following steps :
- Iterate through all the elements in the list
- Multiply each element by K
- Returned the modified list
Below is the implementation of the above approach.
C++
// C++ code for the above approach#include <bits/stdc++.h>using namespace std;Â
// Function to multiply all// the elements of array by Kvoid multiplyAllByK(vector<int> 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 codeint main(){Â
    vector<int> 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 approachimport java.io.*;import java.util.*;Â
class GFG {Â
    // Function to multiply all    // the elements of array by K    public static void multiplyAllByK(        ArrayList<Integer> 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<Integer> arr = new ArrayList<Integer>();        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 Kdef 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 codearr = [3, 4]K = 2multiplyAllByK(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<int> 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<int> arr = new List<int>();        arr.Add(3);        arr.Add(4);        int K = 2;        multiplyAllByK(arr, K);    }}Â
// This code is contributed by 29AjayKumar |
Javascript
<script>Â Â Â Â // JavaScript code for the above approachÂ
    // Function to multiply all    // the elements of array by K    const multiplyAllByK = (arr, K) => {        let N = arr.length;Â
        // Loop to multiply all        // the array elements        for (let i = 0; i < N; i++) {            let x = arr[i];            arr[i] = K * x;        }Â
        // Print the modified array        for (let i = 0; i < N; i++)            document.write(`${arr[i]} `);    }Â
    // Driver code    let arr = [];    arr.push(3);    arr.push(4);    let K = 2;    multiplyAllByK(arr, K);Â
// This code is contributed by rakeshsahniÂ
</script> |
6 8
Time Complexity: O(N)
Auxiliary Space: O(1)
Approach Using Lambda Expression: This can also be implemented using lambda expression.Â
n -> n * KÂ
where n can be a particular element, or complete array.
Below is the implementation of the above approach:
C++
// C++ code to implement above approach#include <iostream>using namespace std;Â
// Function to multiply all// the elements of array by Kvoid 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 codeint 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 <stdio.h>Â
// Function to multiply all// the elements of array by Kvoid 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 codeint 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 approachimport java.io.*;import java.util.*;Â
class GFG {Â
    // Function to multiply all    // the elements of array by K    public static void multiplyAllByK(        ArrayList<Integer> 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<Integer> arr            = new ArrayList<Integer>();        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 Kdef multiplyAllByK(arr, K):Â Â Â Â Â Â Â Â Â Â Â Â Â lambda_func = lambda n: n * KÂ Â Â Â for i in range(len(arr)):Â Â Â Â Â Â Â Â print(lambda_func(arr[i]), end = ' ')Â
# Driver codearr = [3, 4]K = 2Â
multiplyAllByK(arr, K)Â
# This code is contributed by Samim Hossain Mondal. |
C#
// C# code to implement above approachusing 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<int> 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<int> arr            = new List<int>();        arr.Add(3);        arr.Add(4);        int K = 2;        multiplyAllByK(arr, K);    }}Â
// This code is contributed by shikhasingrajput |
Javascript
<script>// Javascript code to implement above approachÂ
// Function to multiply all// the elements of array by Kfunction multiplyAllByK(arr, K) {Â Â Â Â arr = arr.map(k => {Â Â Â Â Â Â Â Â return k * KÂ Â Â Â });Â Â Â Â for (x of arr)Â Â Â Â Â Â Â Â document.write(x + " ");}Â
// Driver codelet arr = [];arr.push(3);arr.push(4);let K = 2;multiplyAllByK(arr, K);Â
// This code is contributed by saurabh_jaiswal.</script> |
6 8
 Time Complexity: O(N)
Auxiliary Space: O(1)Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
