Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to find the greatest product of 3 adjacent numbers in the same (any) direction in a grid with Pascal

1 Answer

0 votes
program GreatestProduct;

type
  TIntGrid = array[0..6, 0..6] of SmallInt;

function FindGreatestProduct(const grid: TIntGrid): LongInt;
var
  maxProduct, product: LongInt;
  rows, cols, i, j: Integer;
  maxnumber1, maxnumber2, maxnumber3: Integer;
begin
  maxProduct := 0;
  rows := 7;
  cols := 7;

  for i := 0 to rows - 1 do
    for j := 0 to cols - 1 do
    begin
      // Horizontal
      if j + 2 < cols then
      begin
        product := grid[i, j] * grid[i, j + 1] * grid[i, j + 2];
        if product > maxProduct then
        begin
          maxProduct := product;
          maxnumber1 := grid[i, j];
          maxnumber2 := grid[i, j + 1];
          maxnumber3 := grid[i, j + 2];
        end;
      end;

      // Vertical
      if i + 2 < rows then
      begin
        product := grid[i, j] * grid[i + 1, j] * grid[i + 2, j];
        if product > maxProduct then
        begin
          maxProduct := product;
          maxnumber1 := grid[i, j];
          maxnumber2 := grid[i + 1, j];
          maxnumber3 := grid[i + 2, j];
        end;
      end;

      // Diagonal down-right
      if (i + 2 < rows) and (j + 2 < cols) then
      begin
        product := grid[i, j] * grid[i + 1, j + 1] * grid[i + 2, j + 2];
        if product > maxProduct then
        begin
          maxProduct := product;
          maxnumber1 := grid[i, j];
          maxnumber2 := grid[i + 1, j + 1];
          maxnumber3 := grid[i + 2, j + 2];
        end;
      end;

      // Diagonal down-left
      if (i + 2 < rows) and (j - 2 >= 0) then
      begin
        product := grid[i, j] * grid[i + 1, j - 1] * grid[i + 2, j - 2];
        if product > maxProduct then
        begin
          maxProduct := product;
          maxnumber1 := grid[i, j];
          maxnumber2 := grid[i + 1, j - 1];
          maxnumber3 := grid[i + 2, j - 2];
        end;
      end;
    end;

  WriteLn('maxnumber1: ', maxnumber1);
  WriteLn('maxnumber2: ', maxnumber2);
  WriteLn('maxnumber3: ', maxnumber3);

  FindGreatestProduct := maxProduct;
end;

var
  grid: TIntGrid;
  result: LongInt;

begin
  grid[0,0] := 1;  grid[0,1] := 2;  grid[0,2] := 3;  grid[0,3] := 4;  grid[0,4] := 5;  grid[0,5] := 6;  grid[0,6] := 7;
  grid[1,0] := 8;  grid[1,1] := 9;  grid[1,2] := 10; grid[1,3] := 11; grid[1,4] := 12; grid[1,5] := 13; grid[1,6] := 14;
  grid[2,0] := 49; grid[2,1] := 49; grid[2,2] := 99; grid[2,3] := 40; grid[2,4] := 17; grid[2,5] := 81; grid[2,6] := 18;
  grid[3,0] := 44; grid[3,1] := 20; grid[3,2] := 45; grid[3,3] := 35; grid[3,4] := 14; grid[3,5] := 0;  grid[3,6] := 61;
  grid[4,0] := 26; grid[4,1] := 97; grid[4,2] := 17; grid[4,3] := 78; grid[4,4] := 80; grid[4,5] := 96; grid[4,6] := 83;
  grid[5,0] := 16; grid[5,1] := 7;  grid[5,2] := 97; grid[5,3] := 57; grid[5,4] := 32; grid[5,5] := 16; grid[5,6] := 27;
  grid[6,0] := 60; grid[6,1] := 74; grid[6,2] := 31; grid[6,3] := 49; grid[6,4] := 71; grid[6,5] := 48; grid[6,6] := 86;

  result := FindGreatestProduct(grid);
  WriteLn('Greatest product of 3 adjacent numbers: ', result);
end.




(*
run:
  
maxnumber1: 80
maxnumber2: 96
maxnumber3: 83
Greatest product of 3 adjacent numbers: 637440
  
*)


 



answered Jul 26, 2025 by avibootz
...