Insert duplicate of K adjacent to it for it’s every occurrence in array
Given an array arr consisting of N integers and an integer K, the task is to insert an adjacent K for every occurrence of it in the original sequence and then truncate the array to the original length using an O(1) auxiliary space.
Examples:
Input: arr[] = {1, 0, 2, 3, 0, 4, 5, 0}, K = 0
Output: {1, 0, 0, 2, 3, 0, 0, 4}
Explanation:
The given array {1, 0, 2, 3, 0, 4, 5, 0} is modified to {1, 0, 0, 2, 3, 0, 0, 4] after insertion of two 0’s and truncation of extra elements.
Input: arr[] = {7, 5, 8}, K = 8
Output: {7, 5, 8}
Explanation:
After inserting an adjacent 8 into the array, it got truncated to restore the original size of the array.
Approach 1: Using STL functions
This problem can be solved by using built-in functions pop_back() and insert() .
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
void duplicateK(vector< int >& arr, int & k)
{
int N = arr.size();
for ( int i=0;i<N;i++)
{
if (arr[i] == k)
{
arr.insert(arr.begin() + i + 1, k);
i++;
arr.pop_back();
}
}
}
int main( int argc, char * argv[])
{
vector< int > arr
= { 1, 0, 2, 3, 0, 4, 5, 0 };
int k=0;
duplicateK(arr,k);
for ( int i = 0; i < arr.size(); i++)
cout << arr[i] << " " ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static Vector<Integer> duplicateK(Vector<Integer> arr, int k)
{
int N = arr.size();
for ( int i = 0 ; i < N; i++)
{
if (arr.get(i) == k)
{
arr.add(i + 1 , k);
i++;
arr.remove(arr.size() - 1 );
}
}
return arr;
}
public static void main(String[] args)
{
Integer []arr = { 1 , 0 , 2 , 3 , 0 , 4 , 5 , 0 };
Vector<Integer> vec = new Vector<Integer>();;
for ( int i = 0 ; i < arr.length; i++)
vec.add(arr[i]);
int k= 0 ;
Vector<Integer> ans = duplicateK(vec,k);
for ( int i = 0 ; i < ans.size(); i++)
System.out.print(ans.get(i) + " " );
}
}
|
Python3
def duplicateK(arr, k):
N = len (arr)
i = 0
while (i < N):
if (arr[i] = = k):
arr.insert(i + 1 , k)
i + = 1
arr.pop()
i + = 1
if __name__ = = "__main__" :
arr = [ 1 , 0 , 2 , 3 , 0 , 4 , 5 , 0 ]
k = 0
duplicateK(arr, k)
for i in range ( 0 , len (arr)):
print (arr[i], end = " " )
|
C#
using System;
using System.Collections.Generic;
class GFG{
static List< int > duplicateK(List< int > arr, int k)
{
int N = arr.Count;
for ( int i = 0; i < N; i++)
{
if (arr[i] == k)
{
arr.Insert(i + 1, k);
i++;
arr.RemoveAt(arr.Count - 1);
}
}
return arr;
}
public static void Main(String[] args)
{
int []arr = { 1, 0, 2, 3, 0, 4, 5, 0 };
int k=0;
List< int > vec = new List< int >();;
for ( int i = 0; i < arr.Length; i++)
vec.Add(arr[i]);
List< int > ans = duplicateK(vec,k);
for ( int i = 0; i < ans.Count; i++)
Console.Write(ans[i] + " " );
}
}
|
Javascript
<script>
function duplicateK(arr,k)
{
var N = arr.length;
for ( var i=0;i<N;i++)
{
if (arr[i] == k)
{
arr.splice(i+1, 0, k);
i++;
arr.pop();
}
}
return arr;
}
var arr=[ 1, 0, 2, 3, 0, 4, 5, 0 ];
var k=0;
var ans = duplicateK(arr,k);
for ( var i = 0; i < ans.length; i++)
document.write(ans[i] + " " );
</script>
|
Output:
1 0 0 2 3 0 0 4
Approach 2: Using Two Pointer Technique
- Since each K needs to be updated with two K entries adjacent to each other, the array will increase in length by an amount equal to the number of K that are present in the original array arr[].
- Find the total number of K, then we assume we have an array with enough space to accommodate every element.
- Initialize a variable write_idx that will point to the index at the end of this imaginary array and another pointer curr at the end of the current array, which is arr[N-1].
- Iterate from the end and for each element we assume that we are copying the element to its current position, but copy only if the write_idx < N, and keep updating the write_idx each time. For an element with a value of zero, write it twice.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
vector< int > duplicateK(vector< int >& arr, int k)
{
const int N = arr.size();
int cnt = count(arr.begin(), arr.end(), k);
int write_idx = N + cnt - 1;
int curr = N - 1;
while (curr >= 0 && write_idx >= 0) {
if (write_idx < N)
arr[write_idx] = arr[curr];
write_idx -= 1;
if (arr[curr] == k) {
if (write_idx < N)
arr[write_idx] = k;
write_idx -= 1;
}
--curr;
}
return arr;
}
int main( int argc, char * argv[])
{
vector< int > arr = { 1, 0, 2, 3, 0, 4, 5, 0 };
int k=0;
vector< int > ans = duplicateK(arr,k);
for ( int i = 0; i < ans.size(); i++)
cout << ans[i] << " " ;
return 0;
}
|
Java
class GFG{
static int [] duplicateK( int []arr, int k)
{
int N = arr.length;
int cnt = count(arr, k);
int write_idx = N + cnt - 1 ;
int curr = N - 1 ;
while (curr >= 0 && write_idx >= 0 )
{
if (write_idx < N)
arr[write_idx] = arr[curr];
write_idx -= 1 ;
if (arr[curr] == k)
{
if (write_idx < N)
arr[write_idx] = k;
write_idx -= 1 ;
}
--curr;
}
return arr;
}
static int count( int []arr, int num)
{
int ans = 0 ;
for ( int i : arr)
if (i == num)
ans++;
return ans;
}
public static void main(String[] args)
{
int []arr = { 1 , 0 , 2 , 3 , 0 , 4 , 5 , 0 };
int k= 0 ;
int []ans = duplicateK(arr,k);
for ( int i = 0 ; i < ans.length; i++)
System.out.print(ans[i] + " " );
}
}
|
Python3
def duplicateK(arr,k):
N = len (arr)
cnt = arr.count(k)
write_idx = N + cnt - 1
curr = N - 1
while (curr > = 0 and write_idx > = 0 ):
if (write_idx < N):
arr[write_idx] = arr[curr]
write_idx - = 1
if (arr[curr] = = k):
if (write_idx < N):
arr[write_idx] = k
write_idx - = 1
curr - = 1
return arr
arr = [ 1 , 0 , 2 , 3 , 0 , 4 , 5 , 0 ]
k = 0
ans = duplicateK(arr,k)
for i in range ( len (ans)):
print (ans[i], end = " " )
|
C#
using System;
class GFG{
static int [] duplicateK( int []arr, int k)
{
int N = arr.Length;
int cnt = count(arr, k);
int write_idx = N + cnt - 1;
int curr = N - 1;
while (curr >= 0 && write_idx >= 0)
{
if (write_idx < N)
arr[write_idx] = arr[curr];
write_idx -= 1;
if (arr[curr] == k)
{
if (write_idx < N)
arr[write_idx] = k;
write_idx -= 1;
}
--curr;
}
return arr;
}
static int count( int []arr, int num)
{
int ans = 0;
foreach ( int i in arr)
{
if (i == num)
ans++;
}
return ans;
}
public static void Main(String[] args)
{
int []arr = { 1, 0, 2, 3, 0, 4, 5, 0 };
int k=0;
int []ans = duplicateK(arr,k);
for ( int i = 0; i < ans.Length; i++)
Console.Write(ans[i] + " " );
}
}
|
Javascript
<script>
function duplicateK(arr,k)
{
const N = arr.length;
let cnt = 0;
for (let i = 0; i < arr.length; ++i){
if (arr[i] == k)
cnt++;
}
let write_idx = N + cnt - 1;
let curr = N - 1;
while (curr >= 0 && write_idx >= 0) {
if (write_idx < N)
arr[write_idx] = arr[curr];
write_idx -= 1;
if (arr[curr] == k) {
if (write_idx < N)
arr[write_idx] = k;
write_idx -= 1;
}
--curr;
}
return arr;
}
let arr = [ 1, 0, 2, 3, 0, 4, 5, 0 ];
let k=0;
let ans = duplicateK(arr,k);
for (let i = 0; i < ans.length; i++)
document.write(ans[i] + " " );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
08 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...