You are on page 1of 3

SNo.

biweekly-contest-83 (Problem Statement)


1. Easy Level-Best Poker Hand.
You are given an integer array ranks and a character array suits. You have 5 cards
where the ith card has a rank of ranks[i] and a suit of suits[i].

The following are the types of poker hands you can make from best to worst:

1. "Flush": Five cards of the same suit.


2. "Three of a Kind": Three cards of the same rank.
3. "Pair": Two cards of the same rank.
4. "High Card": Any single card.

Return a string representing the best type of poker hand you can make with the given
cards.

Note that the return values are case-sensitive.

Example 1:

Input: ranks = [13,2,3,1,9], suits = ["a","a","a","a","a"]

Output: "Flush"

Explanation: The hand with all the cards consists of 5 cards with the
same suit, so we have a "Flush".

Approach:

You return output on the basis of above four condition, like


according to count of char in suits or rank basis return given
string.
So I read the condition and without second thought got idea of
map because I want count of char and rank so on that basis I give
output.
So I took two map, one for rank and second for suits and after
insert the rank and suits in both map then I iterate in map and
check for given conditions.

I show you How I implement it in code-

Code:
string bestHand(vector<int>& ranks, vector<char>& suits) {

unordered_map<int,int>r;
unordered_map<char,int>s;
for(int i=0;i<ranks.size();i++)
{
r[ranks[i]]++;
}for(int i=0;i<suits.size();i++)
{
s[suits[i]]++;
}
for(auto it: s)
{
if(it.second==5)
return "Flush";
}
for(auto it:r)
{
if(it.second>=3)
return "Three of a Kind";
}
for(auto it:r)
{
if(it.second==2)
return "Pair";
}
return "High Card";
}
2. Medium Level: Number of Zero-Filled Subarrays.
Code:
Input: nums = [1,3,0,0,2,0,0,4]

Output: 6

Explanation:

There are 4 occurrences of [0] as a subarray.

There are 2 occurrences of [0,0] as a subarray.

There is no occurrence of a subarray with a size more than 2 filled


with 0. Therefore, we return 6.

Approach:
After reading problem statement, we return count of zero
subarray.
So for that I iterate in vector and check if nums[i]==0 then
increment cnt value, and then in else condition I make an equation
For counting subarray that is total+=(c*(c+1)/2)
Like in {1,3,0,0,2,0,0,4}
At index 2 and 3 we have zero so cnt=2 then total = 0+(6/2)
Total=3; and do cnt=0;
Then again got zero at 5 and 6 index again tot=3
Then total is 3+3 =6
So return 6;

Code:
long long zeroFilledSubarray(vector<int>& nums) {

long long int c=0;


long long int t=0;
for(int i=0;i<nums.size();i++)
{
if(nums[i]==0)
{
c++;
}
else
{
t = t+(c*(c+1)/2);
c=0;
}
}
t = t+(c*(c+1)/2);
return t;
}

You might also like