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++
// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to multiply all
// the elements of array by K
void 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 code
int 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 approach
import 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 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<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>
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 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 <stdio.h>
// 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<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 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<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 K
function multiplyAllByK(arr, K) {
arr = arr.map(k => {
return k * K
});
for (x of arr)
document.write(x + " ");
}
// Driver code
let arr = [];
arr.push(3);
arr.push(4);
let K = 2;
multiplyAllByK(arr, K);
// This code is contributed by saurabh_jaiswal.
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Queries to calculate GCD of an array after multiplying first or last K elements by X Given an array arr[] consisting of N positive integers and a 2D array queries[][] of the type {a, K, X} such that if the value of a is 1, then multiply first K array elements by X. Otherwise, multiply last K array elements by X. The task is to calculate GCD of the array after performing each query o
10 min read
Generate an array having sum of Bitwise OR of same-indexed elements with given array equal to K Given an array arr[] consisting of N integers and an integer K, the task is to print an array generated such that the sum of Bitwise OR of same indexed elements of the generated array with the given array is equal to K. If it is not possible to generate such an array, then print "-1". Examples: Inpu
7 min read
Generate an N-length array having sum of each subarray divisible by K Given two positive integers N and K, the task is to generate an array consisting of N distinct integers such that the sum of elements of each subarray of the constructed array is divisible by K. Examples: Input: N = 3, K = 3Output: 3 6 9Explanation:The subarrays of the resultant array are {3}, {6},
4 min read
Check if all array elements can be made divisible by K by replacing array elements with sum of pairs Given an array arr[] of size N and a positive integer K, the task is to make every element of the array divisible by K by repeatedly selecting a pair of indices (i, j) and perform arr[i] = arr[i] + arr[j]. Examples: Input: arr[] = {3, 6, 3}, K = 6Output: TrueExplanation: Update arr[0] = arr[0] + arr
7 min read
Modify given array by incrementing first occurrence of every element by K Given an array arr[] consisting of N integers, read every element of the array one by one and perform the following operations: If the current element arr[i] had previously occurred in the array, increase its first occurrence by K.Otherwise, insert arr[i] into the sequenceThe task is to print the fi
7 min read
XOR of every element of an Array with a given number K Given an array arr and a number K, find the new array formed by performing XOR of the corresponding element from the given array with the given number K.Examples: Input: arr[] = { 2, 4, 1, 3, 5 }, K = 5 Output: 7 1 4 6 0 Explanation: 2 XOR 5 = 7 4 XOR 5 = 1 1 XOR 5 = 4 3 XOR 5 = 6 5 XOR 5 = 0 Input:
5 min read