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
    • 100+ Java Programs
    • 100+ C Programs
  • 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

HackerRank Winning Lottery Ticket problem solution

YASH PAL, 31 July 2024

In this HackerRank Winning Lottery Ticket problem solution, Your task is to find the number of winning pairs of distinct tickets, such that concatenation of their ticket IDs (in any order) makes for a winning scenario. Complete the function winningLotteryTicket which takes a string array of ticket IDs as input, and returns the number of winning pairs.

HackerRank Winning Lottery Ticket problem solution

Problem solution in Python.

n = int(input())
p = [input().strip() for _ in range(n)]

fullMask = 2**10-1
cntMask = [0 for _ in range(fullMask+1)]

for s in p:
    mask = 0
    for c in s:
        mask |= 1 << (ord(c) - ord('0'))
    cntMask[mask] += 1

res = 0
for i in range(fullMask+1):
    for j in range(i+1, fullMask+1):
        if i | j == fullMask:
            res += cntMask[i] * cntMask[j]

res += cntMask[fullMask] * (cntMask[fullMask]-1) / 2
print(int(res))

Problem solution in C++.

 
#include "bits/stdc++.h"
using namespace std;
const int N = 1e6 + 6;
int n;
int cnt[1 << 10];


void readInp() {
  ios_base :: sync_with_stdio(false);
  cin.tie(NULL);
  string x;
  cin >> n;
  for(int i = 1; i <= n; ++i) {
    cin >> x;
    int mask = 0;
    for(int j = 0; j < x.size(); ++j) mask |= (1 << (x[j] - '0'));
    ++cnt[mask];
  }
}

long long solve() {
   long long ans = 0;
   for(int m1 = 0; m1 <= 1023; ++m1) 
    for(int m2 = 0; m2 <= 1023; ++m2)
     if((m1 | m2) == 1023) 
        ans += m1 == m2 ? 1LL * cnt[m1] * (cnt[m1] - 1) : 1LL * cnt[m1] * cnt[m2];  
    return ans / 2LL;
}

void out(long long x) {
    cout << x << endl;
}

int main() {
    readInp();
    out(solve());
    return 0;
}

algorithm 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