Search Results

Search found 4 results on 1 pages for 'guirgis'.

Page 1/1 | 1 

  • Getting the submatrix with maximum sum?

    - by guirgis
    With the help of the Algorithmist and Larry and a modification of Kadane's Algorithm, here is my solution: int dim = matrix.length; //computing the vertical prefix sum for columns int[][] ps = new int[dim][dim]; for (int i = 0; i < dim; i++) { for (int j = 0; j < dim; j++) { if (j == 0) { ps[j][i] = matrix[j][i]; } else { ps[j][i] = matrix[j][i] + ps[j - 1][i]; } } } int maxSoFar = 0; int min , subMatrix; //iterate over the possible combinations applying Kadane's Alg. //int toplefti =0, topleftj=0, bottomrighti=0, bottomrightj=0; for (int i = 0; i < dim; i++) { for (int j = i; j < dim; j++) { min = 0; subMatrix = 0; for (int k = 0; k < dim; k++) { if (i == 0) { subMatrix += ps[j][k]; } else { subMatrix += ps[j][k] - ps[i-1][k]; } if(subMatrix < min){ min = subMatrix; } if((subMatrix - min) > maxSoFar){ maxSoFar = subMatrix - min; } } } } The only problem left is to determine the submatrix elements, i mean the top left and the bottom right corners. I managed to do this in one dimensional case. Any suggestions?

    Read the article

  • Seaching for an element in a circular sorted array

    - by guirgis
    I wanted to share this with you, i had this problem in a google interview. we want to search for a given element in a circular sorted array in complexity not greater than O(Log n). ex: search for 13 in {5,9,13,1,3}. My idea was to convert the circular array into a regular sorted array then do a binary search on the resulting array, but my problem was the algorithm i came up was stupid that it takes O(n) in the worst case: for(i = 1; i < a.length; i++){ if (a[i] < a[i-1]){ minIndex = i; break; } } then the corresponding index of ith element will be determined from the following relation: (i + minInex - 1) % a.length it is clear that my conversion (from circular to regular) algorithm may take O(n), so we need a better one, any suggestions?

    Read the article

1