Given an array A[] and size of an array is N. The task is to delete elements of array A[] are in given range L to R both are exclusive.
Examples:
Input : N = 12
A[] = { 3, 5, 3, 4, 9, 3, 1, 6, 3, 11, 12, 3}
L = 2
R = 7
Output : 3 5 3 6 3 11 12 3
since A[2] = 3 but this is exclude
A[7] = 6 this also exclude
Input : N = 10
A[] ={ 5, 8, 11, 15, 26, 14, 19, 17, 10, 14 }
L = 4
R = 6
Output :5 8 11 15 26 19 17 10 14
A naive approach is to delete elements in range L to R with extra space.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector<int> deleteElement(int A[], int L, int R, int N)
{
vector<int> B;
for (int i = 0; i < N; i++)
if (i <= L || i >= R)
B.push_back(A[i]);
return B;
}
int main()
{
int A[] = { 3, 5, 3, 4, 9, 3, 1, 6, 3, 11, 12, 3 };
int L = 2, R = 7;
int n = sizeof(A) / sizeof(A[0]);
vector<int> res = deleteElement(A, L, R, n);
for (auto x : res)
cout << x << " ";
return 0;
}
|
Java
import java.util.Vector;
class GFG {
static Vector<Integer> deleteElement(int A[], int L, int R, int N) {
Vector<Integer> B = new Vector<>();
for (int i = 0; i < N; i++) {
if (i <= L || i >= R) {
B.add(A[i]);
}
}
return B;
}
public static void main(String[] args) {
int A[] = {3, 5, 3, 4, 9, 3, 1, 6, 3, 11, 12, 3};
int L = 2, R = 7;
int n = A.length;
Vector<Integer> res = deleteElement(A, L, R, n);
for (Integer x : res) {
System.out.print(x + " ");
}
}
}
|
Python3
def deleteElement(A, L, R, N):
B = []
for i in range(0, N, 1):
if (i <= L or i >= R):
B.append(A[i])
return B
if __name__ == '__main__':
A = [3, 5, 3, 4, 9, 3, 1,
6, 3, 11, 12, 3]
L = 2
R = 7
n = len(A)
res = deleteElement(A, L, R, n)
for i in range(len(res)):
print(res[i], end = " ")
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static List<int> deleteElement(int []A,
int L, int R, int N)
{
List<int> B = new List<int>();
for (int i = 0; i < N; i++)
{
if (i <= L || i >= R)
{
B.Add(A[i]);
}
}
return B;
}
public static void Main()
{
int []A = {3, 5, 3, 4, 9, 3, 1, 6,
3, 11, 12, 3};
int L = 2, R = 7;
int n = A.Length;
List<int> res = deleteElement(A, L, R, n);
foreach (int x in res)
{
Console.Write(x + " ");
}
}
}
|
PHP
<?php
function deleteElement($A, $L, $R, $N)
{
$B = array();
for ($i = 0; $i < $N; $i++)
{
if ($i <= $L or $i >= $R)
$B[] = $A[$i];
}
return $B;
}
$A = array(3, 5, 3, 4, 9, 3, 1,
6, 3, 11, 12, 3);
$L = 2;
$R = 7;
$n = count($A);
$res = deleteElement($A, $L, $R, $n);
for ($i = 0; $i < count($res); $i++)
echo "$res[$i] ";
?>
|
Javascript
<script>
function deleteElement(A, L, R, N) {
let B = [];
for (let i = 0; i < N; i++) {
if (i <= L || i >= R) {
B.push(A[i]);
}
}
return B;
}
let A = [3, 5, 3, 4, 9, 3, 1, 6, 3, 11, 12, 3];
let L = 2, R = 7;
let n = A.length;
let res = deleteElement(A, L, R, n);
for(let i = 0; i < res.length; i++)
document.write(res[i] + " ");
</script>
|
Output: 3 5 3 6 3 11 12 3
Time Complexity: O(n)
Auxiliary Space : O(n)
An efficient solution without using extra space.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int deleteElement(int A[], int L, int R, int N)
{
int i, j = 0;
for (i = 0; i < N; i++) {
if (i <= L || i >= R) {
A[j] = A[i];
j++;
}
}
return j;
}
int main()
{
int A[] = { 5, 8, 11, 15, 26, 14, 19, 17, 10, 14 };
int L = 2, R = 7;
int n = sizeof(A) / sizeof(A[0]);
int res_size = deleteElement(A, L, R, n);
for (int i = 0; i < res_size; i++)
cout << A[i] << " ";
return 0;
}
|
Java
class GFG
{
static int deleteElement(int A[], int L,
int R, int N)
{
int i, j = 0;
for (i = 0; i < N; i++)
{
if (i <= L || i >= R)
{
A[j] = A[i];
j++;
}
}
return j;
}
public static void main(String args[])
{
int A[] = new int[] { 5, 8, 11, 15, 26,
14, 19, 17, 10, 14 };
int L = 2, R = 7;
int n = A.length;
int res_size = deleteElement(A, L, R, n);
for (int i = 0; i < res_size; i++)
System.out.print(A[i] + " ");
}
}
|
Python 3
def deleteElement(A, L, R, N) :
j = 0
for i in range(N) :
if i <= L or i >= R :
A[j] = A[i]
j += 1
return j
if __name__ == "__main__" :
A = [5, 8, 11, 15, 26, 14, 19, 17, 10, 14]
L, R = 2,7
n = len(A)
res_size = deleteElement(A, L, R, n)
for i in range(res_size) :
print(A[i],end = " ")
|
C#
using System;
class GFG
{
static int deleteElement(int []A, int L,
int R, int N)
{
int i, j = 0;
for (i = 0; i < N; i++)
{
if (i <= L || i >= R)
{
A[j] = A[i];
j++;
}
}
return j;
}
public static void Main()
{
int []A = new int[] { 5, 8, 11, 15, 26,
14, 19, 17, 10, 14 };
int L = 2, R = 7;
int n = A.Length;
int res_size = deleteElement(A, L, R, n);
for (int i = 0; i < res_size; i++)
Console.Write(A[i] + " ");
}
}
|
PHP
<?php
function deleteElement(&$A, $L, $R, $N)
{
$i= 0;
$j = 0;
for ($i = 0; $i < $N; $i++)
{
if ($i <= $L || $i >= $R)
{
$A[$j] = $A[$i];
$j++;
}
}
return $j;
}
$A = array(5, 8, 11, 15, 26,
14, 19, 17, 10, 14);
$L = 2;
$R = 7;
$n = sizeof($A);
$res_size = deleteElement($A, $L, $R, $n);
for ($i = 0; $i < $res_size; $i++)
{
echo ($A[$i]);
echo (" ");
}
?>
|
Javascript
<script>
function deleteElement(A, L, R, N)
{
let i, j = 0;
for (i = 0; i < N; i++) {
if (i <= L || i >= R) {
A[j] = A[i];
j++;
}
}
return j;
}
let A = [ 5, 8, 11, 15, 26, 14, 19, 17, 10, 14 ];
let L = 2, R = 7;
let n = A.length;
let res_size = deleteElement(A, L, R, n);
for (let i = 0; i < res_size; i++)
document.write(A[i] + " ");
</script>
|
Time Complexity: O(n)
Auxiliary Space : O(1)