দুটি পয়েন্ট দেওয়া আছে। তাদের মধ্যকার ইন্টিজার পয়েন্ট (integral points) কতগুলো আছে, তা বের করতে হবে।
ফর্মুলা: gcd(|x2-x1|, |y2-y1|) - 1। যদি x-এক্সিসের সমান্তরাল হয় (y1==y2): |x1-x2|-1। y-এক্সিসের সমান্তরাল হলে (x1==x2): |y1-y2|-1।
উদাহরণ: pointOne=[1,5], pointTwo=[1,3] হলে আউটপুট ১।
অ্যালগরিদম
- দুটি পয়েন্ট ইনপুট নিন
- যদি x1==x2: return |y1-y2|-1
- যদি y1==y2: return |x1-x2|-1
- অন্যথায়: return gcd(|x1-x2|, |y1-y2|) - 1
ইমপ্লিমেন্টেশন
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int getCount(int pointOne[], int pointTwo[]) { if (pointOne[0] == pointTwo[0]) return abs(pointOne[1] - pointTwo[1]) - 1; if (pointOne[1] == pointTwo[1]) return abs(pointOne[0] - pointTwo[0]) - 1; return gcd(abs(pointOne[0] - pointTwo[0]), abs(pointOne[1] - pointTwo[1])) - 1; } int main() { int pointOne[] = {1, 3}, pointTwo[] = {10, 12}; cout << getCount(pointOne, pointTwo) << endl; return 0; }আউটপুট
8