Skip to content

Commit c2de33f

Browse files
committed
program to find occurence of a number in sorted array using binary search
1 parent a5fb4bd commit c2de33f

File tree

5 files changed

+110
-17
lines changed

5 files changed

+110
-17
lines changed

Data Structures/LinkedLists/Checking Palindrome List.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ int Palindrome(struct Node *head,stack<int>&s)
7474

7575
return flag;
7676
}
77-
7877
//TIME COMPLEXITY = O(n)
7978

79+
80+
8081

8182

8283
int main() {
@@ -90,13 +91,13 @@ int main() {
9091
struct Node *n4 = new Node();
9192

9293

93-
n1->data=1;
94+
n1->data=2;
9495
n1->next=n2;
95-
n2->data=2;
96+
n2->data=1;
9697
n2->next = n3;
97-
n3->data=2;
98+
n3->data=1;
9899
n3->next = n4;
99-
n4->data = 1;
100+
n4->data = 2;
100101
n4->next = NULL;
101102

102103
struct Node *temp=n1;

Data Structures/LinkedLists/MergePointofLL.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ Node *FindMergePointUsingDiff(Node *headA, Node *headB) {
105105
int d = (n-m);
106106

107107
//if list A is greater in size then simply swap the pointers to head node
108-
if(m>n) {
108+
if(m>n)
109+
{
109110

110111
Node *temp = headA;
111112
headA=headB;
@@ -151,7 +152,7 @@ int main() {
151152

152153
n1->data = 1;
153154
n2->data = 3;
154-
n3->data = 100;
155+
n3->data = 4;
155156
n4->data = 6;
156157

157158
//creating links

Data Structures/LinkedLists/Singly_LL.cpp

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,28 @@ void CountOccurence(struct Node **head,int data)
488488

489489
}
490490

491+
void SwapList(struct Node *head) {
492+
493+
int len = length(head);
494+
struct Node *temp=head;
495+
496+
if(head==NULL) {
497+
return ;
498+
}
499+
500+
if(len%2!=0)
501+
{
502+
cout<<"List not of even length."<<endl;
503+
504+
505+
}
506+
else {
507+
508+
509+
}
510+
}
511+
512+
491513
//TIME COMPLEXITY = O(n)
492514

493515

@@ -516,14 +538,14 @@ int main() {
516538

517539
sortedInsert(&head,10);
518540
sortedInsert(&head,9);
519-
sortedInsert(&head,10);
541+
sortedInsert(&head,1);
520542
sortedInsert(&head,20);
521-
sortedInsert(&head,2);
522-
sortedInsert(&head,51);
523-
sortedInsert(&head,6);
524-
sortedInsert(&head,61);
525-
sortedInsert(&head,10);
526-
sortedInsert(&head,60);
543+
// sortedInsert(&head,2);
544+
// sortedInsert(&head,51);
545+
// sortedInsert(&head,6);
546+
// sortedInsert(&head,61);
547+
// sortedInsert(&head,10);
548+
// sortedInsert(&head,60);
527549

528550

529551

@@ -535,9 +557,13 @@ int main() {
535557
//
536558
cout<<listlength(head)<<endl;
537559

538-
cout<<"Middle element is :"<<MiddleNode(&head)<<endl;
560+
SwapList(head);
561+
562+
cout<<listlength(head)<<endl;
563+
564+
565+
539566

540-
CountOccurence(&head,10);
541567

542568

543569

Data Structures/SORTING/QuickSort.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ int Partition(vector<int> &arr,int start, int end) {
4141
}
4242
}
4343

44-
swap(arr[Pindex],arr[end]);//swapping PIVOT and element at Pindex
44+
swap(arr[end],arr[Pindex]);//swapping PIVOT and element at Pindex
4545

4646
return Pindex;//now at the end Pindex is the index of the PIVOT
4747
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include<iostream>
2+
3+
//Finding number of occurences of a number in an sorted array using Binary Search
4+
5+
using namespace std;
6+
7+
//Binary search can also be used to find first and last occurences of a repeating number in an array
8+
9+
int BinarySearch(int arr[],int n , int x , bool searchFirst)
10+
{
11+
int l = 0;
12+
int h = n-1;
13+
int result = -1;
14+
while(l <= h)
15+
{
16+
int mid = (l + (h-l)/2);
17+
18+
if(arr[mid]==x)
19+
{
20+
result = mid;
21+
22+
//if want to search for first occurence or a repeating no
23+
if(searchFirst) {
24+
//search only in the lower indices
25+
h = mid-1;
26+
}
27+
//if want to search for the last occurence of a repeating number
28+
else {
29+
//search only in the higher indices or right subpart of array
30+
l = mid + 1;
31+
}
32+
}
33+
34+
//search in higher indices or right subpart of array
35+
else if(arr[mid] <= x) l = (mid + 1);
36+
37+
//search in lower indices or left subpart of array
38+
else h = (mid-1);
39+
40+
}
41+
42+
return result;
43+
}
44+
45+
46+
int main () {
47+
48+
int arr [] = {1,2,2,2,3,3,6,7,9};
49+
50+
51+
//finding the index of the first occurecne of searched number
52+
int firstOcc = BinarySearch(arr,sizeof(arr)/sizeof(arr[0]), 2 , true);
53+
54+
if(firstOcc==-1) {
55+
56+
cout<<"0 occurence."<<endl;
57+
}
58+
59+
else {
60+
//findind the index of the last occurence of the searched number
61+
int lastOcc = BinarySearch(arr,sizeof(arr)/sizeof(arr[0]), 2 , false);
62+
63+
cout<<"Count of occurence is :"<<(lastOcc-firstOcc +1)<<endl;
64+
}
65+
}

0 commit comments

Comments
 (0)