import java.util.*;
import java.lang.*;
import java.io.*;
class FinderOfOnes
{
static int maxOnes=0;
public static void main (String[] args) throws java.lang.Exception
{
// Test input.
int[][] inputArr = new int[5][5];
for(int i = 0; i < 5; i++)
{
for( int j =0; j < 5; j++)
{
inputArr[i][j] = 1;
}
}
System.out.println( "Max ones are " + findMaxOnes(inputArr) );
}
static int findMaxOnes(int[][] Arr)
{
int start = 0;
int end = Arr.length;
int mid = (start + end )/ 2;
binarySearch(Arr, start, mid, end);
return maxOnes;
}
static void binarySearch(int[][] Arr, int start, int mid, int end)
{
if( end == start+1 )
{
if( end == Arr.length - 1 )
{
updateMaxIfFoundOn(end, Arr);
}
else
{
updateMaxIfFoundOn(start, Arr);
}
return;
}
if( updateMaxIfFoundOn(mid, Arr) )
{
end = mid;
}
else
{
start = mid;
}
mid = (start + end)/2;
binarySearch(Arr, start, mid, end);
}
static boolean foundOneOn(int columnNumber, int[][] Arr)
{
for( int row=0; row < Arr.length; row++ )
{
if( Arr[row][columnNumber] == 1 )
{
return true;
}
}
return false;
}
static boolean updateMaxIfFoundOn(int columnNumber, int[][] Arr)
{
boolean found = foundOneOn(columnNumber, Arr);
if( found &&
maxOnes < ( Arr.length - columnNumber) )
{
maxOnes = Arr.length - columnNumber;
}
return found;
}
}
Test Execute it here: http://ideone.com/c9Xs0n
No comments:
Post a Comment