Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Xenny had a list of N strings of equal length. He wanted to sort them by the first M characters only. That
# means, while sorting the list of strings, he only wanted to consider the first M characters of each string.
# Help Xenny to find out the Kth string in the list after he sorts them.
#
# Note: Xenny wanted to perform stable sorting.
# Stable sorting algorithms maintain the relative order of records with equal keys (i.e. values). That is, a
# sorting algorithm is stable if whenever there are two records R and S with the same key and with R
# appearing before S in the original list, R will appear before S in the sorted list.
#
# Input
#
# First line contains a single integer - T, which represents the number of testcases.
# T testcases follow.
# Each testcase is of the following format:
# First line contains 3 space-separated integers - N, K and M.
# N is the total number of strings Xenny has.
# K is the index of the string in the list after sorting, which Xenny has to find.
# M is the number of characters based on which sorting will be done by Xenny.
# Then next N lines contain N strings ( each line will contain one string ) .
#
# Output
#
# For each testcase, print the Kth string in the sorted list in a new line.
#
# Constraints
#
# 1 ≤ T ≤ 50
# 1 ≤ N ≤ 103
# 1 ≤ Max Length of each String ≤ 103
# 1 ≤ M ≤ Max Length
# M ≤ Max Length of each String ≤ 103
#
# SAMPLE INPUT
# 1
# 3 1 3
# abcdef
# abcaaa
# aabaaa
#
# SAMPLE OUTPUT
# aabaaa

for _ in range(int(input())):
n, k, m = input().split()
strings = []
for i in range(int(n)):
strings.append(input())

array = sorted(strings, key = lambda x: x[:int(m)])
print(array[int(k) - 1])
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Little Ashish got a lot of strings as his birthday gift. He does not mind getting so many strings for
# free; in fact, he loves them. But, on noticing all the strings he received as a gift, Little Ashish, who's
# also a snob and a bit OCD kind of a guy, realizes that he does not like the way in which the strings are
# arranged.
#
# He likes his strings sorted, in a different kind of a way. He wants his strings to be sorted based on the
# count of characters present in the string. For instance, if the string is: "aaabbc", then the desired
# string would be: cbbaaa. In case where the count of two characters is same, then the lexicographically
# smaller one will be printed first. For instance: "aabbcc" then, the output will be: "aabbcc".
#
# Input:
# First line of input contains number of test cases T. Each test case contains a single string S.
#
# Output:
# For each test cases print the sorted string.
#
# Constraints:
# 1<=T<=100
# 1<=|S|<=100
#
# Note:
# String contains only lowercase characters ['a' to 'z'].
#
# SAMPLE INPUT
# 3
# aabbccdd
# aabcc
# hackerearth
#
# SAMPLE OUTPUT
# aabbccdd
# baacc
# cktaaeehhrr

from collections import Counter

for _ in range(int(input())):
string = Counter(input())
sorted_array = sorted(string.items(), key=lambda x: (x[1], x[0]))
result = ''
for items in sorted_array:
result += items[0] * items[1]
print(result)
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# X and Y are best friends and they love to chat with each other. But their recent concerns about the privacy
# of their messages has distant them. So they decided to encrypt their messages with a key, K, such that the
# character of their messages are now shifted K times towards right of their initial value. Their techniques
# only convert numbers and alphabets while leaving special characters as it is.
#
# Provided the value K you are required to encrypt the messages using their idea of encryption.
#
# INPUT FORMAT
#
# The first line of the input contains, T, the number of messages. The next line contains N, and K, no of
# characters in the message and key for encryption. The next line contains the message.
#
# OUTPUT FORMAT
#
# Output the encrypted messages on a new line for all the test cases.
#
# CONSTRAINS
#
# 1≤T≤100
# 1≤N≤106
# 0≤K≤106
#
# SAMPLE INPUT
# 2
# 12 4
# Hello-World!
# 16 50
# Aarambh@1800-hrs
#
# SAMPLE OUTPUT
# Lipps-Asvph!
# Yypykzf@1800-fpq

myString = 'abcdefghijklmnopqrstuvwxyz'
myStringU = myString.upper()
nums = '0123456789'

def access_char(string, i):
return string[i % len(string)]

for _ in range(int(input())):
n, k = map(int, input().split())
string = input()
result = []

for char in string:
if char.islower() and char.isalpha():
result.append(access_char(myString, myString.find(char) + k))
elif char.isupper() and char.isalpha():
result.append(access_char(myStringU, myStringU.find(char) + k))
elif char.isnumeric():
result.append(access_char(nums, nums.find(str(char)) + k))
else:
result.append(char)

print(''.join([str(i) for i in result]))

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Efficient Python program to check entered number is a perfect square of 2 or not
# Example
# 8
# Its a perfect square of 2



n = int(input("Enter a number"))
if n & (n - 1) == 0:
print("Its a perfect square of 2")
else:
print("Its not perfect square")
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
# Constraints
# 1 <= N <=1000

import math

userInput = int(input())
for i in range(2, userInput):
for i in range(2, userInput + 1):
check = 0
for j in range(2, i):
for j in range(2, int(math.sqrt(i))+ 1):
if i % j == 0:
check = 1
break
Expand Down
44 changes: 44 additions & 0 deletions CompetitiveProgramming/HackerEarth/Bit_Manipulation/P04_Mystery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# In the world of dragon ball, Goku has been the greatest rival of Vegeta. Vegeta wants to surpass goku but never succeeds. Now that he
# knows he cant beat goku in physical strength, he wants to be satisfied by beating goku in mental strength. He gives certain inputs and
# outputs , Goku needs to find the logic and predict the output for the next inputs. Goku is struggling with the challenge, your task is
# to find the logic and and help him win the challenge.

# INPUT :

# Given a series of numbers(inputs) and each number(N) on a newline.

# OUTPUT :

# For the given input , Output the required ans.

# NOTE :

# No. of test cases are unknown.

# Use Faster I/O Techniques.

# CONSTRAINTS :

# 0<= N <= 10^18

# SAMPLE INPUT
# 0
# 1
# 5
# 12
# 22
# 1424
# SAMPLE OUTPUT
# 0
# 1
# 2
# 2
# 3
# 4

while(1):
try:
r=bin(int(input()))
print(r.count('1'))
except:
break
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Hihi is the grandfather of all geeks in IIITA. He and his crazy ideas.....Huh..... Currently, hihi is working on his most famous project
# named 21 Lane, but he is stuck at a tricky segment of his code.

# Hihi wants to assign some random IP addresses to users, but he won't use rand(). He wants to change the current IP of the user's computer
# to the IP such that its hash is next hash greater than the hash of original IP and differs by only 1 bit from the hash of original IP.

# Smart Hihi already hashed the IP to some integer using his personal hash function. What he wants from you is to convert the given hashed
# IP to the required IP X as mentioned above.

# OK, just find the find the smallest number greater than n with exactly 1 bit different from n in binary form

# Input :

# First line contains single integer T ( 1 <= T <= 10^6)- number of test cases. Second line contains hashed IP N ( 1 <= N <= 10^18)

# Output :

# Print T lines, each containing an integer X, the required IP.(don't worry Hihi will decode X to obtain final IP address)

# SAMPLE INPUT
# 5
# 6
# 4
# 10
# 12
# 14
# SAMPLE OUTPUT
# 7
# 5
# 11
# 13
# 15


for _ in range(int(input())):
a=int(input())
print(a|a+1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Given an array of numbers of size (2*n+1).Raja is unable to find the number which is present odd number of times.It is guaranteed that only one such number exists.Can you help Raja in finding the number which is present odd number of times?

# Input
# First line contains value of n.
# Second line contains (2*n+1) array elements.
# Output
# Print the required number.
# Constraints
# 1≤ n ≤
# 1≤ a[i] ≤

# SAMPLE INPUT
# 2
# 1 2 3 2 1
# SAMPLE OUTPUT
# 3
# Explanation
# For first input only 3 is the number which is present odd number of times.

a=int(input())
b=list(map(int,input().split()))
d=b[0]
for i in range(1,len(b)):
d=d^b[i]
print(d)
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# You are given an array A1,A2...AN. You have to tell how many pairs (i, j) exist such that 1 ≤ i < j ≤ N and Ai XOR Aj is odd.

# Input and Output
# First line T, the number of testcases. Each testcase: first line N, followed by N integers in next line. For each testcase, print the
# required answer in one line.

# Constraints
# 1 ≤ T ≤ 10
# 1 ≤ N ≤ 105
# 0 ≤ Ai ≤ 109

# SAMPLE INPUT
# 2
# 3
# 1 2 3
# 4
# 1 2 3 4
# SAMPLE OUTPUT
# 2
# 4
# Explanation
# For first testcase: 1 XOR 2 is 3 and 2 XOR 3 is 1. So, 2 valid pairs. For second testcase: 1 XOR 2 is 3 and 2 XOR 3 is 1 and 1 XOR 4 is 5
# and 3 XOR 4 is 7. So, 4 valid pairs.

for _ in range (int(input())):
a=int(input())
b=list(map(int,input().split()))
ans=0
a2=0
for i in range(0,a):
if(b[i]&1):
ans+=1
else:
a2+=1

print(ans*a2)
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# A project was going on related to image processing and to perform experiments and get desired result the image needs to be converted to
# Gray-Scale using a parameter 'x' and the function P(x) represented the Gray-Code and calculated via x xor (x div 2) where xor stands for
# bitwise exclusive OR (bitwise modulo 2 addition), and div means integer division.

# It is interesting to note that function P(x) is invertible, which means it is always possible to uniquely restore x given the value of
# P(x).

# So the group working on the project forgot to keep the original data related to parameter 'x'. Write a program to restore number x from
# the given value of P(x).

# INPUT:
# The input file contains an integer number y, the value of G(x).

# OUTPUT:
# The output file should contain a single integer x such that G(x) = y.

# CONSTRAINTS:
# 0 ≤ x,P(x) ≤ .

# SAMPLE INPUT
# 15
# SAMPLE OUTPUT
# 10

a=int(input())
a=bin(a).replace("0b","")
c=[int(i) for i in str(a)]
d=[]
e=c[0]
for i in range(0,len(a)):
if(i==0):
d.append(c[i])
else:
f=c[i]^e
d.append(f)
e=f
e=1
g=0
for i in range(0,len(a)):
g=g+d[len(a)-i-1]*e
e=e*2
print(g)
Loading