-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdelete.cpp
More file actions
39 lines (37 loc) · 844 Bytes
/
delete.cpp
File metadata and controls
39 lines (37 loc) · 844 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int binarySearch(int a[],int s,int l,int key);
int deleteElement(int a[],int n,int key){
int pos=binarySearch(a,0,n-1,key);
if(pos==-1){
cout<<"element not found"<<endl;
return n;
}
for(int i=pos;i<n-1;i++)
a[i]=a[i+1];
return n-1;
}
int binarySearch(int a[],int s,int l,int key){
if(s>l)
return -1;
int mid=(s+l)/2;
if(a[mid]==key)
return mid;
if(key>a[mid])
return(a,mid+1,l,key);
return(a,s,mid-1,key);
}
int main()
{
int a[]={10,20,35,40,45,50};
int n=sizeof(a)/sizeof(a[0]);
int key=35;
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
n=deleteElement(a,n,key);
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
}