Codility:Solution MaxProductOfThree

02:58 0 Comments A + a -

Images
 
 1: using System;
 2: using System.Collections.Generic;
 3: using System.Linq;
 4: // you can also use other imports, for example:
 5: // using System.Collections.Generic;
 6:  
 7: // you can write to stdout for debugging purposes, e.g.
 8: // Console.WriteLine("this is a debug message");
 9:  
 10: class Solution {
 11:     public int solution(int[] A) {
 12:         // write your code in C# 6.0 with .NET 4.5 (Mono)
 13:         
 14:         
 15:            if (A.Length<3)
 16:                 throw new ArgumentException();
 17:                 
 18:     List<int> AA = A.ToList();
 19:     AA.Sort();
 20:     long first = AA[0]*AA[1]*AA[A.Length - 1];
 21:     long second = AA[A.Length - 3]*AA[A.Length - 2]*AA[A.Length - 1];
 22:     long max = System.Math.Max(first, second);
 23:     return Convert.ToInt32(max);   
 24:     }
 25: }




 

 

 

 

 

 

 

 

 

 

 
.