Question link: CodeChef: Practical coding for everyone
APPROACH: I found the differences between every possible horizontal lines and vertical lines . I added a new horizontal line and found differences. I checked if there is same difference in vertical lines so that the square forms.
#include<iostream>
#include<bits/stdc++.h>
#include <cmath>
#include<vector>
using namespace std;
int main()
{
long w,h,n,m;
cin >> w >> h >> n >> m;
long ar[n],br[n];
for(int i=0;i<n;i++)
cin >> ar[i];
for(int i=0;i<m;i++)
cin >> br[i];
long long max=0;
long long vertical[100000];
long long horizontal[100000];
for(int i=0;i<100000;i++)
{
vertical[i]=0;
horizontal[i]=0;
}
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
vertical[(abs(ar[i]-ar[j]))]++;
}
}
for(int i=0;i<m-1;i++)
{
for(int j=i+1;j<m;j++)
{
horizontal[(abs(br[i]-br[j]))]++;
}
}
for(int k=0;k<=h;k++)
{
long long count=0;
for(int i=0;i<m;i++)
{
horizontal[abs(k-br[i])]++;
}
for(int i=1;i<100000;i++)
{
if(horizontal[i]*vertical[i] !=0)
count++;
}
if(count>=max)
max=count;
for(int i=0;i<m;i++)
{
horizontal[abs(k-br[i])]--;
}
}
cout << max << endl;
}
Solution link: CodeChef: Practical coding for everyone
THANK YOU.