DEV Community

Cover image for Day 81 of 100 days dsa coding challenge
Manasi Patil
Manasi Patil

Posted on

Day 81 of 100 days dsa coding challenge

Taking on a new challenge: solving GeeksforGeeks POTD daily and sharing my solutions! πŸ’»πŸ”₯
The goal: sharpen problem-solving skills, level up coding, and learn something new every day. Follow my journey! πŸš€

100DaysOfCode #CodingChallenge #ProblemSolving #GeeksforGeeks #DeveloperJourney

Problem:

https://www.geeksforgeeks.org/problems/count-elements-less-than-or-equal-to-k-in-a-sorted-rotated-array/1

Count elements less than or equal to k in a sorted rotated array

Difficulty: Medium Accuracy: 65.29%

Given a sorted array arr[] containing distinct positive integers that has been rotated at some unknown pivot, and a value x. Your task is to count the number of elements in the array that are less than or equal to x.
Examples:
Input: arr[] = [4, 5, 8, 1, 3], x = 6
Output: 4
Explanation: 1, 3, 4 and 5 are less than 6, so the count of all elements less than 6 is 4.
Input: arr[] = [6, 10, 12, 15, 2, 4, 5], x = 14
Output: 6
Explanation: All elements except 15 are less than 14, so the count of all elements less than 14 is 6.
Constraints:
1 ≀ arr.size() ≀ 105
0 ≀ arr[i], x ≀ 109

Solution:
class Solution:
def countLessEqual(self, arr, x):
n=len(arr)
l,r=0,n-1
while l m=(l+r)//2
if arr[m]>arr[r]:
l=m+1
else:
r=m
pivot=l
def bs(a,t):
l,r=0,len(a)-1
while l<=r:
m=(l+r)//2
if a[m]<=t:
l=m+1
else:
r=m-1
return l
if pivot==0:
return bs(arr,x)
return bs(arr[:pivot],x)+bs(arr[pivot:],x)

Top comments (0)