Skip to main content

Container with most water

Here given code implementation process.

/*
   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