From 928baa858fd051f3b6ce0f416c67a981843b20a5 Mon Sep 17 00:00:00 2001 From: Samruddhi Ghodake <72791227+samughodake@users.noreply.github.com> Date: Sat, 25 Sep 2021 03:20:23 +0530 Subject: [PATCH] chore(CPlusPlus): added new array program (#479) --- .../occurence-of-one-in-sorted-array.cpp | 64 +++++++++++++++++++ algorithms/CPlusPlus/README.md | 1 + 2 files changed, 65 insertions(+) create mode 100644 algorithms/CPlusPlus/Arrays/occurence-of-one-in-sorted-array.cpp diff --git a/algorithms/CPlusPlus/Arrays/occurence-of-one-in-sorted-array.cpp b/algorithms/CPlusPlus/Arrays/occurence-of-one-in-sorted-array.cpp new file mode 100644 index 00000000..6b6e8a7e --- /dev/null +++ b/algorithms/CPlusPlus/Arrays/occurence-of-one-in-sorted-array.cpp @@ -0,0 +1,64 @@ +/* +Description: Index of first 1 in a sorted array of 0s and 1s. +Given a sorted array consisting 0’s and 1’s. The task is to find the index of first ‘1’ in the given array. + +Approach: Use binary search to solve in least time complexity (O log n). +*/ + +#include +#include +using namespace std; + int firstIndex(int a[], int n) { + + int low=0; + int high=n-1; + + while(low<=high) { + int mid = (low+high)/2; + //if mid appears to be 0, update low as mid+1 index because we want to search 1 in array + if(a[mid]==0){ + low=mid+1; + } + //checking if mid element is 1 and the element at mid-1 is 0 + //if it is so, then we have found index of first 1 + //simply return mid + else if(a[mid]==1 && (mid==0 || a[mid-1]==0)){ + return mid; + } + //updating the high to mid-1 + //because the element at mid-1 index is not zero + else if (a[mid]==1){ + high=mid-1; + } + } + //if index not found, return -1 + return -1; + } +int main() { + int n; + cout << "Enter number of elements in an array\n"; + cin>>n; + int a[n]; + cout<<"Enter array elements: (only 0 and 1 in sorted order)\n"; + for(int i=0;i>a[i]; + } + //if someone forgets to pass sorted array + sort(a,a+n); + int ind = firstIndex(a,n); + cout<<"Index of first 1 in sorted array: "<