Skip to content

Commit 87a64cc

Browse files
committed
new question added
1 parent 461bf0a commit 87a64cc

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ max edge or path to be traversed to minimize work.
133133
used where if an element should be repeating a given number of times, you can check its value at i and then
134134
i+given number of times to see if thats true or not.
135135
- In divide and conquer even number of multiplications can be reduced from n to logn to get the same result. Example is the pow function where the base value is square everytime and power is halved everytime to get the same answer in logn multiplications. In case the power value is odd, the result is given the base value such that power value is converted to even and then same operations are applied.
136+
- In binary search whatever the algo be, always start from the middle and compare values to the left or right. You may even compare the extreme left and the right values to see lower and upper bound or some pattern to solve the algo. But in binary search always start from the middle.
136137

137138
# Topic0: Programming Questions
138139

@@ -318,6 +319,7 @@ i+given number of times to see if thats true or not.
318319
- [Find a majority element using linear search that occurs more than n/2 times](/divide-and-conquer/question1.c)
319320
- [Nuts and bolts problem](/divide-and-conquer/question2.c)
320321
- [Write a custom C function to implement a pow function](/divide-and-conquer/question3.c)
322+
- [Select an element in sorted rotated array](/divide-and-conquer/question4.c)
321323

322324

323325
## Some important concepts to solve algos better

divide-and-conquer/a.exe

648 Bytes
Binary file not shown.

divide-and-conquer/question4.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Select an element in sorted rotated array
3+
4+
sorted rotated array means, the array was sorted and then some elements were picked and rotated.
5+
eg: 1 2 3 4 5 6 7 8 9 10 became
6+
7 8 9 10 1 2 3 4 5 6
7+
8+
and we need to search for some element in this array.
9+
10+
Linear search can be done. But binary search can also be applied as given below.
11+
12+
METHOD1:
13+
First using binary search we need to find the place where max element is placed. (given the rotation order)
14+
assume in the question and max number as well. Therefore we need to locate 10 as per the above eg.
15+
Now we start from middle element and see left bound which is 7 and right bound which is 6. Now middle
16+
element being 2 it is clear that the sequence is increasing from middle to right, hence 10 cannot be there.
17+
Now if we see towards left, there is a higher number instead of lesser. Therefore we need to search the left
18+
array. Therefore, again we will apply the same method on the left array. Now once 10 is located. We will have
19+
two different increasing components with uppper and lower bounds, and we will clearly know where to search any
20+
element using binary search again.
21+
22+
Time complexity: O(2logn) = O(logn) //twice binary search will be applied
23+
Space complexity: O(logn) or O(1) //recursive or iterative
24+
25+
METHOD2:
26+
Using binary search only but there is no need to divide the array into two components.
27+
Therefore there will be a single array (in the question it will be mentioned whether its a increasing sequence
28+
or not). Therefore from the middle element, one side has to be sorted and the other will not be sorted
29+
no matter at whatstage in the recursion we are. Always from the middle element, one side will be in the right
30+
order and one will be increasing to a point and then decreasing. Therefore we can find the range of middle
31+
to right if it falls in that it exists there otherwise it exists in the other part. It can be done for
32+
all the sub problems.
33+
34+
Time complexity: O(logn) //binary search
35+
Space complexity: O(1) or O(logn) //iterative or recursive
36+
*/
37+
//METHOD2
38+
#include <stdio.h>
39+
#include <stdlib.h>
40+
41+
int search(int *arr,int start,int end, int elm){
42+
if(start > end) return -1;
43+
int mid = (start + end)/2;
44+
if(arr[mid] == elm) return mid;
45+
46+
if(arr[start] <= arr[mid]){
47+
return (elm >= arr[start] && elm <=arr[mid])?search(arr,start,mid-1,elm):search(arr,mid+1,end,elm);
48+
}
49+
return (elm >= arr[mid] && elm <= arr[end])?search(arr,mid+1,end,elm):search(arr,start,mid-1,elm);
50+
}
51+
52+
int main(){
53+
int arr[] = {8,9,10,1,2,3,4,5,6,7};
54+
int size = sizeof(arr)/sizeof(arr[0]);
55+
int elm;
56+
while(1){
57+
printf("enter the element to be searched\n");
58+
scanf("%d",&elm);
59+
int index = search(arr,0,size-1,elm);
60+
if(index < 0){
61+
printf("element does not exist\n");
62+
}else{
63+
printf("element exists at %d\n", index);
64+
}
65+
}
66+
67+
return 0;
68+
}

0 commit comments

Comments
 (0)