Advertisement
Placido_GDD

Maximums Of Array

Jun 6th, 2022
1,004
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.77 KB | None | 0 0
  1. public class Kata {
  2.   public static int MaxProduct(int[] arr, int size) {
  3.     // sort integers from largest to smallest
  4.     // iterate through the array/list multiplying integers until the counter is equal to size value.
  5.     // return result
  6.     int temp = 0;
  7.     int result = 0;
  8.     for(int a = 0;a < arr.Length;a++)
  9.     {
  10.        for(int b = a+1;b < arr.Length;b++)
  11.        {
  12.            if(arr[a] < arr[b])
  13.            {
  14.               temp = arr[a];
  15.               arr[a] = arr[b];
  16.               arr[b] = temp;
  17.            }
  18.        }
  19.     }
  20.    
  21.     for(int x = 0;x < size;x++)
  22.     {
  23.        if(result == 0)
  24.        {
  25.           result = arr[x];
  26.        }
  27.  
  28.        else
  29.        {
  30.           result = result * arr[x];  
  31.        }
  32.     }
  33.    
  34.     return result;
  35.   }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement