Question
Hi I need help ASAP!!!! pleasee help I got this code that counts the number of occurences of a number in a sorted list.Please help
Hi I need help ASAP!!!! pleasee help I got this code that counts the number of occurences of a number in a sorted list.Please help MODIFY this code to return the smallest INDEX and the largest Index in which this word appear. For example, for 'act' in this array arr = [['act', 'act'], ['cat', 'act'], ['tac', 'act'], ['bad', 'abd'], ['fad', 'adf']] it returns index 0 and 2.This is the current code:
# if x is present in arr[] then return # the index of FIRST occurrence of x in # arr[0..n-1], otherwise returns -1 def first(arr, low, high, x, n): if high >= low: # low + (high - low)/2 mid = (low + high)//2 if (mid == 0 or x > arr[mid-1]) and arr[mid] == x: return mid elif x > arr[mid]: return first(arr, (mid + 1), high, x, n) else: return first(arr, low, (mid -1), x, n) return -1;
# if x is present in arr[] then return # the index of LAST occurrence of x # in arr[0..n-1], otherwise returns -1 def last(arr, low, high, x, n): if high >= low: # low + (high - low)/2 mid = (low + high)//2; if(mid == n-1 or x < arr[mid+1]) and arr[mid] == x : return mid elif x < arr[mid]: return last(arr, low, (mid -1), x, n) else: return last(arr, (mid + 1), high, x, n) return -1
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started