Sinchan Garai
BAN USERcould you please say that how can one able to say the starting point of the loop being confirmed that there exists a loop via this algorithm?
- Sinchan Garai June 24, 2013it's not working for a[] = {4, 5, 2, 25, 15, 18, 42, 38, 98, 54};
it shows
4--->5
5--->25
2--->25
25--->-1
15--->18
18--->42
42--->98
38--->98
98--->-1
54--->-1
which is not true.
still I am not getting the problem....could you guys plz elaborate it?
- Sinchan Garai June 07, 2013for the step 2, it should be generate all possible combination with 4 elements as 1. isnt it?
- Sinchan Garai June 04, 2013it's not exactly binary search. it's like binary search only!
- Sinchan Garai May 29, 2013slower
- Sinchan Garai May 27, 2013int binSearch(int arr[], int low, int high, int element)
{
if(low > high)
return -1;
int mid = (low + high) / 2;
if(arr[mid] == element) //found one occurrence
{
//we have to find if any other occurrence is there with less index
if(binSearch(arr, low, mid-1, element) == -1) //no occurrence with less index is found
return mid; // return the only occurrence
else
binSearch(arr, low, mid-1, element);
}
else if(arr[mid] > element) //element is lesser than arr[mid]
binSearch(arr, low, mid-1, element); //search in the lesser half
else if(arr[mid] < element) //element is greater than arr[mid]
binSearch(arr, mid+1, high, element); //search in greater half
else
return -1; //not found at all
}
using a auxiliary array could be a good way too..it would be efficient as well... :)
- Sinchan Garai May 06, 2013
I dont know whether this solution is appropriate or not...
- Sinchan Garai June 24, 2013if I traverse the array linearly twice, I can find the largest and second largest element respectively in O(n+n) = O(n) time.
Thus we are done with the maximum sum as the largest and second largest element gives the maximum sum.
Now if we introduce the condition while finding the second largest element I think, we are done!
Need comment from all of you!