Generate an Array by multiplying each element of given Array by K
Last Updated :
19 Jan, 2022
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++
#include <bits/stdc++.h>
using namespace std;
void multiplyAllByK(vector< int > arr, int K)
{
int N = arr.size();
for ( int i = 0; i < N; i++) {
int x = arr[i];
arr[i] = K * x;
}
for ( int i = 0; i < N; i++)
cout << (arr[i]) << " " ;
}
int main()
{
vector< int > arr;
arr.push_back(3);
arr.push_back(4);
int K = 2;
multiplyAllByK(arr, K);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static void multiplyAllByK(
ArrayList<Integer> arr, int K)
{
int N = arr.size();
for ( int i = 0 ; i < N; i++) {
int x = arr.get(i);
arr.set(i, K * x);
}
for ( int i = 0 ; i < N; i++)
System.out.print(arr.get(i) + " " );
}
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
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 = ' ' )
arr = [ 3 , 4 ]
K = 2
multiplyAllByK(arr, K)
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public static void multiplyAllByK(
List< int > arr, int K)
{
int N = arr.Count;
for ( int i = 0; i < N; i++) {
int x = arr[i];
arr[i] =( K * x);
}
for ( int i = 0; i < N; i++)
Console.Write(arr[i] + " " );
}
public static void Main(String[] args)
{
List< int > arr = new List< int >();
arr.Add(3);
arr.Add(4);
int K = 2;
multiplyAllByK(arr, K);
}
}
|
Javascript
<script>
const multiplyAllByK = (arr, K) => {
let N = arr.length;
for (let i = 0; i < N; i++) {
let x = arr[i];
arr[i] = K * x;
}
for (let i = 0; i < N; i++)
document.write(`${arr[i]} `);
}
let arr = [];
arr.push(3);
arr.push(4);
let K = 2;
multiplyAllByK(arr, K);
</script>
|
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++
#include <iostream>
using namespace std;
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] << " " ;
}
int main()
{
int arr[2];
arr[0] = 3;
arr[1] = 4;
int K = 2;
multiplyAllByK(arr, K);
return 0;
}
|
C
#include <stdio.h>
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]);
}
int main()
{
int arr[2];
arr[0] = 3;
arr[1] = 4;
int K = 2;
multiplyAllByK(arr, K);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static void multiplyAllByK(
ArrayList<Integer> arr, int K)
{
arr.replaceAll(n -> n * K);
for (Integer x : arr)
System.out.print(x + " " );
}
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
def multiplyAllByK(arr, K):
lambda_func = lambda n: n * K
for i in range ( len (arr)):
print (lambda_func(arr[i]), end = ' ' )
arr = [ 3 , 4 ]
K = 2
multiplyAllByK(arr, K)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
public class GFG {
public static void multiplyAllByK(
List< int > arr, int K)
{
var temp = arr.Select(n => n * K);
foreach ( int x in temp)
Console.Write(x + " " );
}
public static void Main(String[] args)
{
List< int > arr
= new List< int >();
arr.Add(3);
arr.Add(4);
int K = 2;
multiplyAllByK(arr, K);
}
}
|
Javascript
<script>
function multiplyAllByK(arr, K) {
arr = arr.map(k => {
return k * K
});
for (x of arr)
document.write(x + " " );
}
let arr = [];
arr.push(3);
arr.push(4);
let K = 2;
multiplyAllByK(arr, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...