Facing the sun Java

PROGRAM TO FIND THE NUMBER OF BUILDINGS FACING THE SUN



class Test
{
    // Returns count buildings that can see sunlight
    static int countBuildings(int arr[], int n)
    {
        // Initialuze result  (Note that first building
        // always sees sunlight)
        int count = 1;
       
        // Start traversing element
        int curr_max = arr[0];
        for (int i=1; i<n; i++)
        {
            // If curr_element is maximum,
            // update maximum and increment count
            if (arr[i] > curr_max)
            {
                count++;
                curr_max=arr[i];
            }
        }
       
        return count;
    }
      
    // Driver method
    public static void main(String[] args) 
    {
        int arr[] = {7, 4, 8, 2, 9};
          
        System.out.println(countBuildings(arr, arr.length));
          
    }
}

OUTPUT
3

Comments

Popular posts from this blog

Snake and Ladder Problem Python

Add two numbers represented by linked lists Java