Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerEarth Little Shino and the tournament problem solution

YASH PAL, 31 July 2024
In this HackerEarth Little Shino and the tournament problem solution, Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and ith fighter will be represented by i. Each fighter has some distinct strength. The rules of the tournament are:
Each fight will have 2 fighters.
In a fight, a fighter with more strength will win.
In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is an odd number of fighters, the last one will qualify to the next round without fighting.
HackerEarth Little Shino and the tournament problem solution

HackerEarth Little Shino and the tournament problem solution.

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;
bool B[30];

int main(int argc, char* argv[])
{
if(argc == 2 or argc == 3)
freopen(argv[1], "r", stdin);
if(argc == 3)
freopen(argv[2], "w", stdout);
string s;
int ans, count, k;
cin >> k >> s;
ans = 0;
for(int i = 0;i < s.length();++i)
{
count = 0;
memset(B, false, sizeof(B));
for(int j = i;j < s.length();++j)
{
if(B[s[j] - 'a'] == false) count++;
B[s[j] - 'a'] = true;
if(count == k)
ans++;
}
}
cout << ans << endl;
return 0;
}

Second solution

#include<bits/stdc++.h>

using namespace std;

int k;
string st;
int cnt[1000];
int ans;

int main(){

cin >> k;
cin >> st;

for (int l = 0; l < st.size(); l++)
{
for (int i = 'a'; i <= 'z'; i++)
{
cnt[i] = 0;
}

int dif = 0;

for (int r = l; r < st.size(); r++)
{
cnt[st[r]]++;
if (cnt[st[r]] == 1)
++dif;
if (dif == k)
++ans;
}
}

cout << ans << endl;

return 0;
}
coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes