Posted on by Kalkicode
Code Probability

Container with most water

The problem of finding the container with the most water involves an array of non-negative integers, where each element represents the height of a vertical line. The goal is to find two lines that, together with the x-axis, form a container that holds the maximum amount of water.

Explanation of the Problem:

Given an array of heights [h1, h2, h3, ..., hn], where each height represents the height of a line at index i, we need to find two lines, represented by indices i and j (i != j), such that the container formed by these lines and the x-axis contains the maximum amount of water.

The volume of water that can be contained between two lines is determined by the shorter line and the distance between the two lines. So, to maximize the amount of water, we need to consider lines that have greater heights and larger distances between them.

Approach and Algorithm:

One way to solve this problem is to use a brute-force approach. We can iterate over all possible pairs of lines and calculate the area of the container formed by each pair. By keeping track of the maximum area encountered so far, we can find the container with the most water.

  1. Initialize a variable 'result' to store the maximum water container area and set it to 0.
  2. Iterate through all pairs of lines using two nested loops:
    • The outer loop iterates from the first line (index i = 0) to the second-to-last line (index i = n - 2).
    • The inner loop iterates from the second line (index j = i + 1) to the last line (index j = n - 1).
  3. Calculate the area of the container formed by the current pair of lines using the formula:
    • area = min(arr[i], arr[j]) * (j - i)
    • min(arr[i], arr[j]) represents the height of the shorter line.
    • (j - i) represents the distance between the two lines.
  4. Update the 'result' variable if the calculated area is greater than the current maximum.
  5. After the loops, return the 'result' variable, which contains the maximum water container area.

Code Solution

/*
      C program for
      Container with most water
   */
   #include <stdio.h>
   
   int min(int a, int b)
   {
       if(a < b)
       {
           return a;
       }
       return b;
   }
   int max(int a, int b)
   {
       if(a > b)
       {
           return a;
       }
       return b;
   }
   // Return the size of most water container
   int maxWater(int arr[], int n)
   {
       int result = 0;
   
       for (int i = 0; i < n - 1; ++i)
       {
           for (int j = i + 1; j < n; ++j) 
           {
               // Find resultant limit of max water container
               result = max(result, (j - i) * min(arr[i], arr[j]));
           }
       }
       return result;
   }
   
   int main()
   {
       int arr[] = 
       {
          1, 2, 7, 4, 8, 1, 3
       };
   
       /* 
                   
                         ┃
                 ┃       ┃
                 ┃       ┃
                 ┃       ┃
                 ┃   ┃   ┃           
                 ┃   ┃   ┃        ┃
              ┃  ┃   ┃   ┃        ┃
           ┃  ┃  ┃   ┃   ┃   ┃    ┃
          ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
           1  2  7   4   8   1    3
       ----------------------------------     
                    14
                    ↓ 
                  ▁▁▁▁▁▁▁┃
                 ┃       ┃
              8  ┃       ┃
                ↘┃▁▁▁▁▁▁▁┃
           10    ┃▁▁▁┃▁▁▁┃▁▁▁▁▁▁▁▁ ↙ 12   
        6    ↘ ▁▁┃▁▁▁┃▁▁▁┃▁▁▁▁▁▁▁▁┃
          ↘ ▁▁┃▁▁┃▁▁▁┃▁▁▁┃▁▁▁ ▁▁▁▁┃
           ┃  ┃  ┃   ┃   ┃   ┃    ┃
          ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
           1  2  7   4   8   1    3
         --------------------------------
         
         Result : 14  
       */
   
       // Get the number of elements
       int n = sizeof(arr) / sizeof(arr[0]);
   
       // Display result
       printf(" Result : %d\n", maxWater(arr, n) );
       return 0;
   }

Output

 Result : 14
/*
       Java program
       Container with most water
   */
   public class Capacity
   {
       public int min(int a, int b)
       {
           if (a < b)
           {
               return a;
           }
           return b;
       }
       public int max(int a, int b)
       {
           if (a > b)
           {
               return a;
           }
           return b;
       }
       // Return the size of most water container
       public int maxWater(int[] arr, int n)
       {
           int result = 0;
           for (int i = 0; i < n - 1; ++i)
           {
               for (int j = i + 1; j < n; ++j)
               {
                   // Find resultant limit of max water container
                   result = max(result, (j - i) * min(arr[i], arr[j]));
               }
           }
           return result;
       }
       public static void main(String[] args)
       {
           Capacity task = new Capacity();
           int[] arr = {
               1 , 2 , 7 , 4 , 8 , 1 , 3
           };
           /* 
                       
                             ┃
                     ┃       ┃
                     ┃       ┃
                     ┃       ┃
                     ┃   ┃   ┃           
                     ┃   ┃   ┃        ┃
                  ┃  ┃   ┃   ┃        ┃
               ┃  ┃  ┃   ┃   ┃   ┃    ┃
              ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
               1  2  7   4   8   1    3
           ----------------------------------     
                        14
                        ↓ 
                      ▁▁▁▁▁▁▁┃
                     ┃       ┃
                  8  ┃       ┃
                    ↘┃▁▁▁▁▁▁▁┃
               10    ┃▁▁▁┃▁▁▁┃▁▁▁▁▁▁▁▁ ↙ 12   
            6    ↘ ▁▁┃▁▁▁┃▁▁▁┃▁▁▁▁▁▁▁▁┃
              ↘ ▁▁┃▁▁┃▁▁▁┃▁▁▁┃▁▁▁ ▁▁▁▁┃
               ┃  ┃  ┃   ┃   ┃   ┃    ┃
              ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
               1  2  7   4   8   1    3
             --------------------------------
             
             Result : 14  
           */
           // Get the number of elements
           int n = arr.length;
           // Display result
           System.out.print(" Result : " + task.maxWater(arr, n) + "\n");
       }
   }

Output

 Result : 14
// Include header file
   #include <iostream>
   
   using namespace std;
   /*
       C++ program
       Container with most water
   */
   class Capacity
   {
       public: int min(int a, int b)
       {
           if (a < b)
           {
               return a;
           }
           return b;
       }
       int max(int a, int b)
       {
           if (a > b)
           {
               return a;
           }
           return b;
       }
       // Return the size of most water container
       int maxWater(int arr[], int n)
       {
           int result = 0;
           for (int i = 0; i < n - 1; ++i)
           {
               for (int j = i + 1; j < n; ++j)
               {
                   // Find resultant limit of max water container
                   result = this->max(result, (j - i) *this->min(arr[i], arr[j]));
               }
           }
           return result;
       }
   };
   int main()
   {
       Capacity *task = new Capacity();
       int arr[] = {
           1 , 2 , 7 , 4 , 8 , 1 , 3
       };
       /*
                         ┃
                 ┃       ┃
                 ┃       ┃
                 ┃       ┃
                 ┃   ┃   ┃           
                 ┃   ┃   ┃        ┃
              ┃  ┃   ┃   ┃        ┃
           ┃  ┃  ┃   ┃   ┃   ┃    ┃
          ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
           1  2  7   4   8   1    3
       ----------------------------------     
                    14
                    ↓ 
                  ▁▁▁▁▁▁▁┃
                 ┃       ┃
              8  ┃       ┃
                ↘┃▁▁▁▁▁▁▁┃
           10    ┃▁▁▁┃▁▁▁┃▁▁▁▁▁▁▁▁ ↙ 12   
        6    ↘ ▁▁┃▁▁▁┃▁▁▁┃▁▁▁▁▁▁▁▁┃
          ↘ ▁▁┃▁▁┃▁▁▁┃▁▁▁┃▁▁▁ ▁▁▁▁┃
           ┃  ┃  ┃   ┃   ┃   ┃    ┃
          ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
           1  2  7   4   8   1    3
         --------------------------------
         
         Result : 14  
       */
       // Get the number of elements
       int n = sizeof(arr) / sizeof(arr[0]);
       // Display result
       cout << " Result : " << task->maxWater(arr, n) << "\n";
       return 0;
   }

Output

 Result : 14
// Include namespace system
   using System;
   /*
       Csharp program
       Container with most water
   */
   public class Capacity
   {
       public int min(int a, int b)
       {
           if (a < b)
           {
               return a;
           }
           return b;
       }
       public int max(int a, int b)
       {
           if (a > b)
           {
               return a;
           }
           return b;
       }
       // Return the size of most water container
       public int maxWater(int[] arr, int n)
       {
           int result = 0;
           for (int i = 0; i < n - 1; ++i)
           {
               for (int j = i + 1; j < n; ++j)
               {
                   // Find resultant limit of max water container
                   result = this.max(result, (j - i) * this.min(arr[i], arr[j]));
               }
           }
           return result;
       }
       public static void Main(String[] args)
       {
           Capacity task = new Capacity();
           int[] arr = {
               1 , 2 , 7 , 4 , 8 , 1 , 3
           };
           /*
                             ┃
                     ┃       ┃
                     ┃       ┃
                     ┃       ┃
                     ┃   ┃   ┃           
                     ┃   ┃   ┃        ┃
                  ┃  ┃   ┃   ┃        ┃
               ┃  ┃  ┃   ┃   ┃   ┃    ┃
              ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
               1  2  7   4   8   1    3
           ----------------------------------     
                        14
                        ↓ 
                      ▁▁▁▁▁▁▁┃
                     ┃       ┃
                  8  ┃       ┃
                    ↘┃▁▁▁▁▁▁▁┃
               10    ┃▁▁▁┃▁▁▁┃▁▁▁▁▁▁▁▁ ↙ 12   
            6    ↘ ▁▁┃▁▁▁┃▁▁▁┃▁▁▁▁▁▁▁▁┃
              ↘ ▁▁┃▁▁┃▁▁▁┃▁▁▁┃▁▁▁ ▁▁▁▁┃
               ┃  ┃  ┃   ┃   ┃   ┃    ┃
              ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
               1  2  7   4   8   1    3
             --------------------------------
             
             Result : 14  
           */
           // Get the number of elements
           int n = arr.Length;
           // Display result
           Console.Write(" Result : " + task.maxWater(arr, n) + "\n");
       }
   }

Output

 Result : 14
<?php
   /*
       Php program
       Container with most water
   */
   class Capacity
   {
       public	function min($a, $b)
       {
           if ($a < $b)
           {
               return $a;
           }
           return $b;
       }
       public	function max($a, $b)
       {
           if ($a > $b)
           {
               return $a;
           }
           return $b;
       }
       // Return the size of most water container
       public	function maxWater($arr, $n)
       {
           $result = 0;
           for ($i = 0; $i < $n - 1; ++$i)
           {
               for ($j = $i + 1; $j < $n; ++$j)
               {
                   // Find resultant limit of max water container
                   $result = $this->max($result, ($j - $i) * 
                                        $this->min($arr[$i], $arr[$j]));
               }
           }
           return $result;
       }
   }
   
   function main()
   {
       $task = new Capacity();
       $arr = array(1, 2, 7, 4, 8, 1, 3);
       /*
                         ┃
                 ┃       ┃
                 ┃       ┃
                 ┃       ┃
                 ┃   ┃   ┃           
                 ┃   ┃   ┃        ┃
              ┃  ┃   ┃   ┃        ┃
           ┃  ┃  ┃   ┃   ┃   ┃    ┃
          ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
           1  2  7   4   8   1    3
       ----------------------------------     
                    14
                    ↓ 
                  ▁▁▁▁▁▁▁┃
                 ┃       ┃
              8  ┃       ┃
                ↘┃▁▁▁▁▁▁▁┃
           10    ┃▁▁▁┃▁▁▁┃▁▁▁▁▁▁▁▁ ↙ 12   
        6    ↘ ▁▁┃▁▁▁┃▁▁▁┃▁▁▁▁▁▁▁▁┃
          ↘ ▁▁┃▁▁┃▁▁▁┃▁▁▁┃▁▁▁ ▁▁▁▁┃
           ┃  ┃  ┃   ┃   ┃   ┃    ┃
          ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
           1  2  7   4   8   1    3
         --------------------------------
         
         Result : 14  
       */
       // Get the number of elements
       $n = count($arr);
       // Display result
       echo(" Result : ".$task->maxWater($arr, $n)."\n");
   }
   main();

Output

 Result : 14
/*
       Node JS program
       Container with most water
   */
   class Capacity
   {
       min(a, b)
       {
           if (a < b)
           {
               return a;
           }
           return b;
       }
       max(a, b)
       {
           if (a > b)
           {
               return a;
           }
           return b;
       }
       // Return the size of most water container
       maxWater(arr, n)
       {
           var result = 0;
           for (var i = 0; i < n - 1; ++i)
           {
               for (var j = i + 1; j < n; ++j)
               {
                   // Find resultant limit of max water container
                   result = this.max(result, (j - i) * 
                                     this.min(arr[i], arr[j]));
               }
           }
           return result;
       }
   }
   
   function main()
   {
       var task = new Capacity();
       var arr = [1, 2, 7, 4, 8, 1, 3];
       /*
                         ┃
                 ┃       ┃
                 ┃       ┃
                 ┃       ┃
                 ┃   ┃   ┃           
                 ┃   ┃   ┃        ┃
              ┃  ┃   ┃   ┃        ┃
           ┃  ┃  ┃   ┃   ┃   ┃    ┃
          ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
           1  2  7   4   8   1    3
       ----------------------------------     
                    14
                    ↓ 
                  ▁▁▁▁▁▁▁┃
                 ┃       ┃
              8  ┃       ┃
                ↘┃▁▁▁▁▁▁▁┃
           10    ┃▁▁▁┃▁▁▁┃▁▁▁▁▁▁▁▁ ↙ 12   
        6    ↘ ▁▁┃▁▁▁┃▁▁▁┃▁▁▁▁▁▁▁▁┃
          ↘ ▁▁┃▁▁┃▁▁▁┃▁▁▁┃▁▁▁ ▁▁▁▁┃
           ┃  ┃  ┃   ┃   ┃   ┃    ┃
          ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
           1  2  7   4   8   1    3
         --------------------------------
         
         Result : 14  
       */
       // Get the number of elements
       var n = arr.length;
       // Display result
       process.stdout.write(" Result : " + task.maxWater(arr, n) + "\n");
   }
   main();

Output

 Result : 14
#    Python 3 program
   #    Container with most water
   class Capacity :
       def min(self, a, b) :
           if (a < b) :
               return a
           
           return b
       
       def max(self, a, b) :
           if (a > b) :
               return a
           
           return b
       
       #  Return the size of most water container
       def maxWater(self, arr, n) :
           result = 0
           i = 0
           while (i < n - 1) :
               j = i + 1
               while (j < n) :
                   #  Find resultant limit of max water container
                   result = self.max(result, (j - i) * 
                                     self.min(arr[i], arr[j]))
                   j += 1
               
               i += 1
           
           return result
       
   
   def main() :
       task = Capacity()
       arr = [1, 2, 7, 4, 8, 1, 3]
       #                  ┃
       #          ┃       ┃
       #          ┃       ┃
       #          ┃       ┃
       #          ┃   ┃   ┃           
       #          ┃   ┃   ┃        ┃
       #       ┃  ┃   ┃   ┃        ┃
       #    ┃  ┃  ┃   ┃   ┃   ┃    ┃
       #   ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
       #    1  2  7   4   8   1    3
       # ----------------------------------     
       #             14
       #             ↓ 
       #           ▁▁▁▁▁▁▁┃
       #          ┃       ┃
       #       8  ┃       ┃
       #         ↘┃▁▁▁▁▁▁▁┃
       #    10    ┃▁▁▁┃▁▁▁┃▁▁▁▁▁▁▁▁ ↙ 12   
       # 6    ↘ ▁▁┃▁▁▁┃▁▁▁┃▁▁▁▁▁▁▁▁┃
       #   ↘ ▁▁┃▁▁┃▁▁▁┃▁▁▁┃▁▁▁ ▁▁▁▁┃
       #    ┃  ┃  ┃   ┃   ┃   ┃    ┃
       #   ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
       #    1  2  7   4   8   1    3
       #  --------------------------------
       #  Result : 14  
       #  Get the number of elements
       n = len(arr)
       #  Display result
       print(" Result : ", task.maxWater(arr, n) )
   
   if __name__ == "__main__": main()

Output

 Result :  14
#    Ruby program
   #    Container with most water
   class Capacity 
       def min(a, b) 
           if (a < b) 
               return a
           end
   
           return b
       end
   
       def max(a, b) 
           if (a > b) 
               return a
           end
   
           return b
       end
   
       #  Return the size of most water container
       def maxWater(arr, n) 
           result = 0
           i = 0
           while (i < n - 1) 
               j = i + 1
               while (j < n) 
                   #  Find resultant limit of max water container
                   result = self.max(result, (j - i) * 
                                     self.min(arr[i], arr[j]))
                   j += 1
               end
   
               i += 1
           end
   
           return result
       end
   
   end
   
   def main() 
       task = Capacity.new()
       arr = [1, 2, 7, 4, 8, 1, 3]
       #                  ┃
       #          ┃       ┃
       #          ┃       ┃
       #          ┃       ┃
       #          ┃   ┃   ┃           
       #          ┃   ┃   ┃        ┃
       #       ┃  ┃   ┃   ┃        ┃
       #    ┃  ┃  ┃   ┃   ┃   ┃    ┃
       #   ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
       #    1  2  7   4   8   1    3
       # ----------------------------------     
       #             14
       #             ↓ 
       #           ▁▁▁▁▁▁▁┃
       #          ┃       ┃
       #       8  ┃       ┃
       #         ↘┃▁▁▁▁▁▁▁┃
       #    10    ┃▁▁▁┃▁▁▁┃▁▁▁▁▁▁▁▁ ↙ 12   
       # 6    ↘ ▁▁┃▁▁▁┃▁▁▁┃▁▁▁▁▁▁▁▁┃
       #   ↘ ▁▁┃▁▁┃▁▁▁┃▁▁▁┃▁▁▁ ▁▁▁▁┃
       #    ┃  ┃  ┃   ┃   ┃   ┃    ┃
       #   ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
       #    1  2  7   4   8   1    3
       #  --------------------------------
       #  Result : 14  
       #  Get the number of elements
       n = arr.length
       #  Display result
       print(" Result : ", task.maxWater(arr, n) ,"\n")
   end
   
   main()

Output

 Result : 14
   
/*
       Scala program
       Container with most water
   */
   class Capacity()
   {
       def min(a: Int, b: Int): Int = {
           if (a < b)
           {
               return a;
           }
           return b;
       }
       def max(a: Int, b: Int): Int = {
           if (a > b)
           {
               return a;
           }
           return b;
       }
       // Return the size of most water container
       def maxWater(arr: Array[Int], n: Int): Int = {
           var result: Int = 0;
           var i: Int = 0;
           while (i < n - 1)
           {
               var j: Int = i + 1;
               while (j < n)
               {
                   // Find resultant limit of max water container
                   result = max(result, (j - i) * min(arr(i), arr(j)));
                   j += 1;
               }
               i += 1;
           }
           return result;
       }
   }
   object Main
   {
       def main(args: Array[String]): Unit = {
           var task: Capacity = new Capacity();
           var arr: Array[Int] = Array(1, 2, 7, 4, 8, 1, 3);
           /*
                             ┃
                     ┃       ┃
                     ┃       ┃
                     ┃       ┃
                     ┃   ┃   ┃           
                     ┃   ┃   ┃        ┃
                  ┃  ┃   ┃   ┃        ┃
               ┃  ┃  ┃   ┃   ┃   ┃    ┃
              ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
               1  2  7   4   8   1    3
           ----------------------------------     
                        14
                        ↓ 
                      ▁▁▁▁▁▁▁┃
                     ┃       ┃
                  8  ┃       ┃
                    ↘┃▁▁▁▁▁▁▁┃
               10    ┃▁▁▁┃▁▁▁┃▁▁▁▁▁▁▁▁ ↙ 12   
            6    ↘ ▁▁┃▁▁▁┃▁▁▁┃▁▁▁▁▁▁▁▁┃
              ↘ ▁▁┃▁▁┃▁▁▁┃▁▁▁┃▁▁▁ ▁▁▁▁┃
               ┃  ┃  ┃   ┃   ┃   ┃    ┃
              ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
               1  2  7   4   8   1    3
             --------------------------------
             
             Result : 14  
           */
           // Get the number of elements
           var n: Int = arr.length;
           // Display result
           print(" Result : " + task.maxWater(arr, n) + "\n");
       }
   }

Output

 Result : 14
import Foundation;
   /*
       Swift 4 program
       Container with most water
   */
   class Capacity
   {
       func min(_ a: Int, _ b: Int) -> Int
       {
           if (a < b)
           {
               return a;
           }
           return b;
       }
       func max(_ a: Int, _ b: Int) -> Int
       {
           if (a > b)
           {
               return a;
           }
           return b;
       }
       // Return the size of most water container
       func maxWater(_ arr: [Int], _ n: Int) -> Int
       {
           var result: Int = 0;
           var i: Int = 0;
           while (i < n - 1)
           {
               var j: Int = i + 1;
               while (j < n)
               {
                   // Find resultant limit of max water container
                   result = self.max(result, (j - i) * self.min(arr[i], arr[j]));
                   j += 1;
               }
               i += 1;
           }
           return result;
       }
   }
   func main()
   {
       let task: Capacity = Capacity();
       let arr: [Int] = [1, 2, 7, 4, 8, 1, 3];
       /*
                         ┃
                 ┃       ┃
                 ┃       ┃
                 ┃       ┃
                 ┃   ┃   ┃           
                 ┃   ┃   ┃        ┃
              ┃  ┃   ┃   ┃        ┃
           ┃  ┃  ┃   ┃   ┃   ┃    ┃
          ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
           1  2  7   4   8   1    3
       ----------------------------------     
                    14
                    ↓ 
                  ▁▁▁▁▁▁▁┃
                 ┃       ┃
              8  ┃       ┃
                ↘┃▁▁▁▁▁▁▁┃
           10    ┃▁▁▁┃▁▁▁┃▁▁▁▁▁▁▁▁ ↙ 12   
        6    ↘ ▁▁┃▁▁▁┃▁▁▁┃▁▁▁▁▁▁▁▁┃
          ↘ ▁▁┃▁▁┃▁▁▁┃▁▁▁┃▁▁▁ ▁▁▁▁┃
           ┃  ┃  ┃   ┃   ┃   ┃    ┃
          ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
           1  2  7   4   8   1    3
         --------------------------------
         
         Result : 14  
       */
       // Get the number of elements
       let n: Int = arr.count;
       // Display result
       print(" Result : ", task.maxWater(arr, n) );
   }
   main();

Output

 Result :  14
/*
       Kotlin program
       Container with most water
   */
   class Capacity
   {
       fun min(a: Int, b: Int): Int
       {
           if (a < b)
           {
               return a;
           }
           return b;
       }
       fun max(a: Int, b: Int): Int
       {
           if (a > b)
           {
               return a;
           }
           return b;
       }
       // Return the size of most water container
       fun maxWater(arr: Array < Int > , n: Int): Int
       {
           var result: Int = 0;
           var i: Int = 0;
           while (i < n - 1)
           {
               var j: Int = i + 1;
               while (j < n)
               {
                   // Find resultant limit of max water container
                   result = this.max(result, (j - i) * this.min(arr[i], arr[j]));
                   j += 1;
               }
               i += 1;
           }
           return result;
       }
   }
   fun main(args: Array < String > ): Unit
   {
       val task: Capacity = Capacity();
       val arr: Array < Int > = arrayOf(1, 2, 7, 4, 8, 1, 3);
       /*
                         ┃
                 ┃       ┃
                 ┃       ┃
                 ┃       ┃
                 ┃   ┃   ┃           
                 ┃   ┃   ┃        ┃
              ┃  ┃   ┃   ┃        ┃
           ┃  ┃  ┃   ┃   ┃   ┃    ┃
          ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
           1  2  7   4   8   1    3
       ----------------------------------     
                    14
                    ↓ 
                  ▁▁▁▁▁▁▁┃
                 ┃       ┃
              8  ┃       ┃
                ↘┃▁▁▁▁▁▁▁┃
           10    ┃▁▁▁┃▁▁▁┃▁▁▁▁▁▁▁▁ ↙ 12   
        6    ↘ ▁▁┃▁▁▁┃▁▁▁┃▁▁▁▁▁▁▁▁┃
          ↘ ▁▁┃▁▁┃▁▁▁┃▁▁▁┃▁▁▁ ▁▁▁▁┃
           ┃  ┃  ┃   ┃   ┃   ┃    ┃
          ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
           1  2  7   4   8   1    3
         --------------------------------
         
         Result : 14  
       */
       // Get the number of elements
       val n: Int = arr.count();
       // Display result
       print(" Result : " + task.maxWater(arr, n) + "\n");
   }

Output

 Result : 14
package main
   import "fmt"
   /*
       Go program
       Container with most water
   */
   
   func min(a, b int) int {
       if a < b {
           return a
       }
       return b
   }
   func max(a, b int) int {
       if a > b {
           return a
       }
       return b
   }
   // Return the size of most water container
   func maxWater(arr[] int, n int) int {
       var result int = 0
       for i := 0 ; i < n - 1 ; i++ {
           for j := i + 1 ; j < n ; j++ {
               // Find resultant limit of max water container
               result = max(result, (j - i) * min(arr[i], arr[j]))
           }
       }
       return result
   }
   func main() {
       
       var arr = [] int {
           1,
           2,
           7,
           4,
           8,
           1,
           3,
       }
       /*
                         ┃
                 ┃       ┃
                 ┃       ┃
                 ┃       ┃
                 ┃   ┃   ┃           
                 ┃   ┃   ┃        ┃
              ┃  ┃   ┃   ┃        ┃
           ┃  ┃  ┃   ┃   ┃   ┃    ┃
          ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
           1  2  7   4   8   1    3
       ----------------------------------     
                    14
                    ↓ 
                  ▁▁▁▁▁▁▁┃
                 ┃       ┃
              8  ┃       ┃
                ↘┃▁▁▁▁▁▁▁┃
           10    ┃▁▁▁┃▁▁▁┃▁▁▁▁▁▁▁▁ ↙ 12   
        6    ↘ ▁▁┃▁▁▁┃▁▁▁┃▁▁▁▁▁▁▁▁┃
          ↘ ▁▁┃▁▁┃▁▁▁┃▁▁▁┃▁▁▁ ▁▁▁▁┃
           ┃  ┃  ┃   ┃   ┃   ┃    ┃
          ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
           1  2  7   4   8   1    3
         --------------------------------
         
         Result : 14  
       */
       // Get the number of elements
       var n int = len(arr)
       // Display result
       fmt.Print(" Result : ", maxWater(arr, n), "\n")
   }

Output

 Result : 14

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment