๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
9.61K subscribers
5.6K photos
3 videos
95 files
10.4K links
๐ŸšฉMain Group - @SuperExams
๐Ÿ“Job Updates - @FresherEarth

๐Ÿ”ฐAuthentic Coding Solutions(with Outputs)
โš ๏ธDaily Job Updates
โš ๏ธHackathon Updates & Solutions

Buy ads: https://telega.io/c/cs_algo
Download Telegram
public int solution(int[] A) {
        int N = A.length;
        Arrays.sort(A);
        int minMoves = N;
       
        for (int i = 0; i <= N - 1; i++) {
            int end = A[i] + N - 1;
            int r= binarySearch(A, end);
            int balls= r- i + 1;
            int moves = N - balls;
            minMoves = Math.min(minMoves, moves);
        }
       
        return minMoves;
    }
   
    private int binarySearch(int[] A, int target) {
        int low = 0;
        int high = A.length - 1;
       
        while (low <= high) {
            int mid = low + (high - low) / 2;
           
            if (A[mid] <= target) {
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
       
        return high;
    }
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>

using namespace std;
struct Area {
    int startR, endR, startC, endC, size;
    bool horizontal;
    Area(int sr, int er, int sc, int ec, int s, bool h)
        : startR(sr), endR(er), startC(sc), endC(ec), size(s), horizontal(h) {}
};

int maxCultivationArea(const vector<string>& A) {
    int N = A.size();
    int M = A[0].size();
    vector<Area> areas;

    for (int r = 0; r < N; ++r) {
        int startC = 0;
        while (startC < M) {
            if (A[r][startC] == '.') {
                int endC = startC;
                while (endC < M && A[r][endC] == '.') endC++;
                areas.emplace_back(r, r, startC, endC - 1, endC - startC, true);
                startC = endC;
            } else {
                startC++;
            }
        }
    }
    for (int c = 0; c < M; ++c) {
        int startR = 0;
        while (startR < N) {
            if (A[startR][c] == '.') {
                int endR = startR;
                while (endR < N && A[endR][c] == '.') endR++;
                areas.emplace_back(startR, endR - 1, c, c, endR - startR, false);
                startR = endR;
            } else {
                startR++;
            }
        }
    }

    int maxCultivation = 0;

    for (int i = 0; i < areas.size(); ++i) {
        for (int j = i + 1; j < areas.size(); ++j) {
            const Area& a1 = areas[i];
            const Area& a2 = areas[j];
            bool overlap = false;

            if (a1.horizontal && a2.horizontal) {
                overlap = a1.startR == a2.startR && !(a1.endC < a2.startC || a2.endC < a1.startC);
            } else if (!a1.horizontal && !a2.horizontal) {
                overlap = a1.startC == a2.startC && !(a1.endR < a2.startR || a2.endR < a1.startR);
            } else {
                if (a1.horizontal) {
                    overlap = a1.startR <= a2.endR && a1.startR >= a2.startR && a2.startC <= a1.endC && a2.startC >= a1.startC;
                } else {
                    overlap = a2.startR <= a1.endR && a2.startR >= a1.startR && a1.startC <= a2.endC && a1.startC >= a2.startC;
                }
            }

            if (!overlap) {
                maxCultivation = max(maxCultivation, a1.size + a2.size);
            }
        }
    }

    for (const auto& area : areas) {
        maxCultivation = max(maxCultivation, area.size);
    }

    return maxCultivation;
}
๐Ÿ‘1