Majority Vote | GfG Problem of the Day | Arrays | Moore's Voting Algorithm

แชร์
ฝัง
  • เผยแพร่เมื่อ 7 ต.ค. 2024

ความคิดเห็น • 1

  • @codetips-byRochak
    @codetips-byRochak  5 วันที่ผ่านมา

    Let's Connect 🤝 :
    LinkedIn : www.linkedin.com/in/rochak-vyas17/
    Code :
    class Solution {
    public:
    // Function to find the majority elements in the array
    vector findMajority(vector& nums) {
    int candidate1=nums[0];
    int candidate2=nums[1];
    int vote1=0;
    int vote2=0;
    int n=nums.size();
    vectorans;
    for(int i=0;in/3){
    ans.push_back(candidate2);
    flag=1;
    }
    if(!flag){
    return {-1};
    }
    return ans;
    }
    };