I made a small program with window size k = 2; int max(int n, int m) { if ( n > m ) return n; else return m; } int main() { int arr[8]={3,5,1,9,0,4,-1,7}; int i,j; for (i=0;i!=j && i<=7;i++) for (j=0;j!=i && j<=7; j++) { int k = max(arr[i], arr[j]); cout<<"["<<arr[i]<<","<<arr[j]<<"] = "; cout<<k<<"\n"; } return 0; }
output is : [5,3] = 5 [1,3] = 3 [1,5] = 5 [9,3] = 9 [9,5] = 9 [9,1] = 9 [0,3] = 3 [0,5] = 5 [0,1] = 1 [0,9] = 9 [4,3] = 4 [4,5] = 5 [4,1] = 4 [4,9] = 9 [4,0] = 4 [-1,3] = 3 [-1,5] = 5 [-1,1] = 1 [-1,9] = 9 [-1,0] = 0 [-1,4] = 4 [7,3] = 7 [7,5] = 7 [7,1] = 7 [7,9] = 9 [7,0] = 7 [7,4] = 7 [7,-1] = 7 On Fri, Sep 2, 2011 at 3:43 PM, WgpShashank <shashank7andr...@gmail.com>wrote: > Hi Anup , here is naive approach > > There is a sliding window of size w which is moving from the very left of > the array to the very right. You can only see the w numbers in the window. > Each time the sliding window moves rightwards by one position. > > Following is an example: > The array is [1 3 -1 -3 5 3 6 7], and w is 3. > > Window position Max > --------------- ----- > [1 3 -1] -3 5 3 6 7 3 > 1 [3 -1 -3] 5 3 6 7 3 > 1 3 [-1 -3 5] 3 6 7 5 > 1 3 -1 [-3 5 3] 6 7 5 > 1 3 -1 -3 [5 3 6] 7 6 > 1 3 -1 -3 5 [3 6 7] 7 > > The obvious solution with run time complexity of O(nw) is which is not > efficient enough. Every time the window is moved, we have to search for the > maximum from w elements in the window. where w is size of window & n is size > of array > > 1st Method(Naive Approach) > > Data Structure Used : Array > Algorithm: A.for all i=0 to n-w+1 (we should have at-least w elements in > window) > B.for all j=i to i+w (keep incrementing windows size form left to right) > C find maximum inn each window & print it or put in array (Auxiliary) > > For more efficient approaches you can have a look here > > http://shashank7s.blogspot.com/2011/06/given-array-and-integer-k-find-maximum.html > > correct me if anything wrong or other approaches you can thing of ? > > Thanks > Shashank Mani > Computer Science > Birla Institute of Technology Mesra > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/algogeeks/-/m1R83UHfc2UJ. > > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.