Delete array element in given index range [L - R]
Last Updated :
01 Mar, 2023
Given an array A[] and the size of an array is N. The task is to delete elements of array A[] that are in the 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 the range L to R with extra space.
Below is the implementation of the above approach:
C
// C++ code to delete element
// in given range
#include <stdio.h>
#include <stdlib.h>
// Delete L to R element
void deleteElement(int A[], int L, int R, int N,int *size,int *B)
{
int index=0;
for (int i = 0; i < N; i++)
if (i <= L || i >= R)
B[index++]=A[i];
*size=index;
}
// main Driver
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]);
int B[n-abs(L-R)];
int size=0;
deleteElement(A, L, R, n,&size,B);
for(int i=0;i<size;i++)
printf("%d ",B[i]);
return 0;
}
C++
// C++ code to delete element
// in given range
#include <bits/stdc++.h>
using namespace std;
// Delete L to R element
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;
}
// main Driver
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;
// Java code to delete element
// in given range
class GFG {
// Delete L to R element
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;
}
// main Driver
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 + " ");
}
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python 3 code to delete element
# in given range
# Delete L to R element
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
# Driver Code
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 = " ")
# THis code is implemented by
# Surendra_Gangwar
C#
// C# code to delete element
// in given range
using System;
using System.Collections.Generic;
class GFG
{
// Delete L to R element
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;
}
// Driver code
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 + " ");
}
}
}
// This code is contributed by Rajput-Ji
PHP
<?php
// PHP code to delete element
// in given range
// Delete L to R element
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;
}
// Driver Code
$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] ";
// This code is implemented by
// Srathore
?>
JavaScript
<script>
// Javascript code to delete element in given range
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] + " ");
// This code is contributed by divyeshrabadiya07.
</script>
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space : O(n)
An efficient solution without using extra space.
Below is the implementation of the above approach:
C
// C code to delete element
// in given range
#include <stdio.h>
// Delete L to R elements
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 size of Array
// after delete element
return j;
}
// Driver Code
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++)
printf("%d ", A[i]);
return 0;
}
C++
// C++ code to delete element
// in given range
#include <bits/stdc++.h>
using namespace std;
// Delete L to R elements
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 size of Array
// after delete element
return j;
}
// main Driver
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
// Java code to delete element
// in given range
class GFG
{
// Delete L to R elements
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 size of Array
// after delete element
return j;
}
// Driver Code
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] + " ");
}
}
// This code is contributed
// by Kirti_Mangal
Python 3
# Python 3 program to delete element
# in given range
# Function to delete L to R element
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 size of Array
# after delete element
return j
# Driver Code
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 = " ")
# This code is contributed by ANKITRAI1
C#
// C# code to delete element
// in given range
using System;
class GFG
{
// Delete L to R elements
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 size of Array
// after delete element
return j;
}
// Driver Code
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] + " ");
}
}
// This code is contributed by 29AjayKumar
PHP
<?php
// PHP code to delete element
// in given range
// Delete L to R elements
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 size of Array
// after delete element
return $j;
}
// Driver Code
$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 (" ");
}
// This code is contributed
// by Shivi_Aggarwal
?>
JavaScript
<script>
// JavaScript code to delete element in given range
// Delete L to R elements
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 size of Array
// after delete element
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>
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space : O(1)
Similar Reads
Delete an Element from a Given Position in an Array Given an array of integers, the task is to delete an element from a given position in the array.Examples:Input: arr[] = [10, 20, 30, 40], pos = 1Output: [20, 30, 40]Input: arr[] = [10, 20, 30, 40], pos = 2Output: [10, 30, 40]Input: arr[] = [10, 20, 30, 40], pos = 4Output: [10, 20, 30]Table of Conten
6 min read
Find array elements with frequencies in range [l , r] Given an array of integers, find the elements from the array whose frequency lies in the range [l, r]. Examples: Input : arr[] = { 1, 2, 3, 3, 2, 2, 5 } l = 2, r = 3 Output : 2 3 3 2 2 Approach : Take a hash map, which will store the frequency of all the elements in the array.Now, traverse once agai
9 min read
Delete an Element from the end of an array Given an array of integers, the task is to delete an element from the end of the array.Examples:Input: arr[] = [10, 20, 30, 40]Output: [10, 20, 30]Input: arr[] = [20]Output: []Table of Content[Approach 1] Using Built-In Methods[Approach 2] Using Custom Method[Approach 1] Using Built-In MethodsWe wil
5 min read
Delete First Occurrence of Given Element from an Array Given an array of integers, the task is to delete a given element from the array. If there are multiple occurrences of the element, we need to remove only its first occurrence.Examples:Input: arr[] = [10, 20, 30, 40], ele = 20Output: [10, 30, 40]Input: arr[] = [10, 20, 30, 40], ele = 25Output: [10,
8 min read
Count 1s present in a range of indices [L, R] in a given array Given an array arr[] consisting of a single element N (1 ⤠N ⤠106) and two integers L and R, ( 1 ⤠L ⤠R ⤠105), the task is to make all array elements either 0 or 1 using the following operations : Select an element P such that P > 1 from the array arr[].Replace P with three elements at the sam
10 min read