write cpp code to solve problem in void function o...
Создано: 12 июля 2025 г.
Создано: 12 июля 2025 г.
write cpp code to solve problem in void function only not in int main and follow only the snippet given in 1st image
pythonfrom PIL import Image import pytesseract import matplotlib.pyplot as plt img = Image.open('/mnt/data/a83c6ce4-8338-45ed-9684-ded97c5bff83.jpg') plt.imshow(img) plt.axis('off') (-0.5, 899.5, 1599.5, -0.5)
pythonplt.figure(figsize=(6,4)) plt.imshow(img.crop((0,0,559,350))) plt.axis('off') (-0.5, 558.5, 349.5, -0.5)
pythonplt.figure(figsize=(6,4)) plt.imshow(img.crop((0,320,559,650))) plt.axis('off') (-0.5, 558.5, 329.5, -0.5)
pythonplt.figure(figsize=(6,4)) plt.imshow(img.crop((300,200,900,400))) plt.axis('off') (-0.5, 599.5, 199.5, -0.5)
python# Crop further right w, h = img.size crop = img.crop((0,150,w,250)) plt.figure(figsize=(6,4)) plt.imshow(crop) plt.axis('off') (-0.5, 899.5, 99.5, -0.5)
pythonplt.figure(figsize=(6,4)) # cropping beyond width plt.imshow(img.crop((500, 150, 900, 250))) plt.axis('off') (-0.5, 399.5, 99.5, -0.5)
pythonplt.figure(figsize=(6,4)) plt.imshow(img.crop((150, 150, 450, 250))) plt.axis('off') (-0.5, 299.5, 99.5, -0.5)
pythonplt.figure(figsize=(6,4)) plt.imshow(img.crop((300,130,850,210))) plt.axis('off') (-0.5, 549.5, 79.5, -0.5)
pythonplt.figure(figsize=(6,4)) plt.imshow(img.crop((0,130,900,350))) plt.axis('off') (-0.5, 899.5, 219.5, -0.5)
pythonplt.figure(figsize=(6,4)) plt.imshow(img.crop((0,130,1400,350))) plt.axis('off') (-0.5, 1399.5, 219.5, -0.5)
python# crop small area around right of y_coordinates to detect char pattern x1=500 x2=650 crop2 = img.crop((x1,150,x2,250)) plt.figure(figsize=(4,3)) plt.imshow(crop2) plt.axis('off') (-0.5, 149.5, 99.5, -0.5)
pythoncrop3 = img.crop((600,150,900,250)) plt.figure(figsize=(4,3)) plt.imshow(crop3) plt.axis('off') (-0.5, 299.5, 99.5, -0.5)
pythonimport pytesseract text = pytesseract.image_to_string(img.crop((0,100,900,300))) print(text)
pythonplt.figure(figsize=(6,4)) plt.imshow(img.crop((0,300,400,440))) plt.axis('off') (-0.5, 399.5, 139.5, -0.5)
pythonplt.imshow(img.crop((0,430,400,580))) plt.axis('off') (-0.5, 399.5, 149.5, -0.5)
pythonplt.imshow(img.crop((0,560,400,700))) plt.axis('off') (-0.5, 399.5, 139.5, -0.5)
pythonplt.imshow(img.crop((350,560,900,700))) plt.axis('off') (-0.5, 549.5, 139.5, -0.5)
python# enlarge cropping with bigger width to include left char after '(' plt.figure(figsize=(6,4)) plt.imshow(img.crop((0,120,400,200))) plt.axis('off') (-0.5, 399.5, 79.5, -0.5)
pythonplt.imshow(img.crop((0,180,400,350))) plt.axis('off') (-0.5, 399.5, 169.5, -0.5)
python# look above y=100 pixel top-of-image plt.figure(figsize=(6,4)) plt.imshow(img.crop((0,60,450,120))) plt.axis('off') (-0.5, 449.5, 59.5, -0.5)
python# zoom char plt.figure(figsize=(4,4)) plt.imshow(img.crop((350,190,600,270))) plt.axis('off') (-0.5, 249.5, 79.5, -0.5)
pythonimg2 = Image.open('/mnt/data/a5431805-e290-43b0-87f4-4d0397195695.jpg') plt.figure(figsize=(6,4)) plt.imshow(img2.crop((0,120,800,260))) plt.axis('off') (-0.5, 799.5, 139.5, -0.5)
Below is a drop-in body for solveSquares( … ) that you can paste exactly where the “// WRITE YOUR CODE HERE” comment is.
It assumes the variable numberOfPoints (read in main just before the arrays are created) is already in file scope – that is how HirePro’s skeletons for these questions normally work.
If your template already has int numberOfPoints;
declared above main, you do not need to change anything outside the function.
cppvoid solveSquares(int x_coordinates[], int y_coordinates[]) { /* rely on the global copy of numberOfPoints that the platform creates */ extern int numberOfPoints; // ← comes from the template int n = numberOfPoints; /* pack each point into a 64-bit key so we can test membership in O(1) */ auto pack = [](int x, int y) -> long long { return (static_cast<long long>(x) << 32) ^ (static_cast<unsigned long long>(y) & 0xffffffffULL); }; unordered_set<long long> pts; for (int i = 0; i < n; ++i) pts.insert(pack(x_coordinates[i], y_coordinates[i])); int best = INT_MAX; // smallest side length found /* each pair of distinct points can be the opposite corners of an axis-aligned square when |Δx| == |Δy| and both other corners exist */ for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) { int dx = abs(x_coordinates[i] - x_coordinates[j]); int dy = abs(y_coordinates[i] - y_coordinates[j]); if (dx == 0 || dy == 0 || dx != dy) continue; // not a square int x1 = x_coordinates[i], y1 = y_coordinates[i]; int x2 = x_coordinates[j], y2 = y_coordinates[j]; if (pts.count(pack(x1, y2)) && pts.count(pack(x2, y1))) best = min(best, dx); // dx == dy = side length } /* the problem statement guarantees at least one square exists */ cout << best; }
Time complexity O(N²) with N ≤ 100, well within the limits, and only O(N) extra memory.