Skip to content

Commit 419ae09

Browse files
committedSep 5, 2021
Unsolved part-1
1 parent 0b730de commit 419ae09

35 files changed

+1995
-3
lines changed
 

‎10X/Python/Array/Array_Leader.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'''
2+
Array_Leader
3+
Given an integer array A containing N distinct integers, you have to find all the leaders in the array A.
4+
5+
An element is leader if it is strictly greater than all the elements to its right side.
6+
7+
NOTE:
8+
The rightmost element is always a leader.
9+
10+
Problem Constraints:
11+
1 <= N <= 10^5
12+
1 <= A[i] <= 10^8
13+
Input
14+
First line denotes n, the size of the array. The next n lines contains the elements of the array.
15+
16+
Output
17+
Return an integer array denoting all the leader elements of the array. (You need to print one element in one line & The sequence of the resultant array should be in the reverse order, in which they appear in the given array.)
18+
19+
Example
20+
Input: 6
21+
22+
16
23+
24+
17
25+
26+
4
27+
28+
3
29+
30+
5
31+
32+
2
33+
34+
Output:
35+
36+
2
37+
38+
5
39+
40+
17
41+
42+
Explanation:
43+
Element 17 is strictly greater than all the elements on the right side to it.
44+
45+
Element 2 is strictly greater than all the elements on the right side to it.
46+
47+
Element 5 is strictly greater than all the elements on the right side to it.
48+
49+
So we will return this three elements i.e [2, 5, 17] as the required ordering.
50+
'''
51+
arr=[int(input()) for _ in range(int(input()))]
52+
j=len(arr)
53+
ma=arr[len(arr)-1]
54+
a=[]
55+
a.append(ma)
56+
while j>0 and len(arr)>1:
57+
j-=1
58+
if arr[j]>ma:
59+
ma=arr[j]
60+
a.append(ma)
61+
[print(i) for i in a]

‎10X/Python/Array/Canteen.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'''
2+
Canteen
3+
You and Ravi, are deciding how to split bill at canteen. Each will only pay for the items they consume. Ravi gets the check and calculates your portion. You must determine if his calculation is correct.
4+
5+
For example, assume the bill has the following prices: bill = [2,4,6]. You declined to eat item k = bill[2] which costs 6. If Ravi calculates the bill correctly, you will pay (2 + 4)/2 = 3. If he includes the cost of bill[2], he will calculate (2 + 4 + 6)/2. In the second case, he should refund 3 to you.
6+
7+
Input Format
8+
The first line contains two space-separated integers n and k, the number of items ordered and the 0-based
9+
index of the item that you did not eat.
10+
11+
The second line contains n space-separated integers where 0 ≤ i < n.
12+
13+
The third line contains an integer, b, the amount of money that Ravi charged You for your share of the
14+
bill.
15+
16+
Note:
17+
The amount of money due to you by Ravi will always be an integer.
18+
Output Format
19+
If Ravi did not overcharge you, print It is Correct! on a new line; otherwise, print the difference
20+
(i.e., bcharged - bactual) that Ravi must refund to you. This will always be an integer.
21+
22+
Sample Input
23+
4 1
24+
3 10 2 9
25+
12
26+
Sample Output
27+
5
28+
Explanation
29+
You didn't eat item bill[1] = 10, but she shared the rest of the items with Brian. The total cost of the
30+
shared items is 3 + 2 + 9 = 14 and, split in half, the cost per person is bactual = 7. Ravi charged you
31+
bcharged = 12 for your portion of the bill. We print the amount you were overcharged,
32+
bcharged - bactual = 12 - 7 = 5, on a new line.
33+
'''
34+
# your code goes here
35+
n,k=map(int,input().split())
36+
ar=[int(x) for x in input().split()]
37+
p=int(input())
38+
s=(sum(ar)-ar[k])//2
39+
if p>s:
40+
print(p-s)
41+
else:
42+
print("It is Correct!")

‎10X/Python/Array/Counting_Stars.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'''
2+
Counting Stars
3+
One day Professor Xavier saw alphabets appearing on the stars. He was so fascinated by them that he started making a list of numbers and for every new star, he inserted a number X into the list. This X is equal to the no. of times the alphabet on the current star has previously appeared.
4+
5+
After seeing N stars, he decided to stop and calculate the sum of all the N numbers.
6+
7+
Professor Xavier has spent all his life watching stars and do not know how to add. Can you help him in finding the sum?
8+
9+
Input
10+
The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
11+
12+
The first line contains a single integer – N, denoting the number of stars.
13+
14+
The next line contains a string S of length N, describing the characters that appeared on the N stars sequentially.
15+
16+
Output
17+
For each query, print a single line containing one integer – the sum of all the N numbers.
18+
19+
Example
20+
Input: 1
21+
22+
5
23+
24+
abaab
25+
26+
Output:
27+
28+
4
29+
30+
Explanation
31+
There are 5 stars.
32+
33+
The list (say L) is initially empty. L = {}.
34+
35+
The 1st star had alphabet ‘a’ on it. Since ‘a’ has not appeared previously, 0 is inserted in the list. L = {0}.
36+
37+
The 2nd star had alphabet ‘b’ on it. Since ‘b’ has not appeared previously, 0 is inserted in the list. L = {0, 0}.
38+
39+
The 3rd star had alphabet ‘a’ on it. Since ‘a’ has previously appeared 1 time, 1 is inserted in the list. L = {0, 0, 1}.
40+
41+
The 4th star had alphabet ‘a’ on it. Since ‘a’ has previously appeared 2 times, 2 is inserted in the list. L = {0, 0, 1, 2}.
42+
43+
The 5th star had alphabet ‘b’ on it. Since ‘b’ has previously appeared 1 time, 1 is inserted in the list. L = {0, 0, 1, 2, 1}.
44+
45+
Sum of all elements in L = 4.
46+
'''
47+
def countStar(s,n):
48+
d={}
49+
l=[]
50+
for i in range(n):
51+
if s[i] in d:
52+
d[s[i]]+=1
53+
l.append(d[s[i]])
54+
else:
55+
d[s[i]]=0
56+
l.append(d[s[i]])
57+
58+
return sum(l)
59+
60+
tc=int(input())
61+
for _ in range(tc):
62+
n=int(input())
63+
s=input()
64+
print(countStar(s,n))
65+
66+

‎10X/Python/Array/Distance_Covering.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'''
2+
Distance covering
3+
Description
4+
Given a distance n, find the number of ways to cover it with either 1 or 2 steps.
5+
6+
Input format
7+
First line contains a positive integer n, the number of test cases.
8+
This is followed by n lines, each line containing an integer p, 1 <= p <= 20.
9+
10+
Output format
11+
n lines, each line containing a positive integer, denoting the number of ways to cover the corresponding distance.
12+
13+
Sample input
14+
2
15+
3
16+
2
17+
Sample output
18+
3
19+
2
20+
Explanation
21+
First line of input is 2, meaning there are 2 test cases.
22+
Next line contains 3, indicating that the distance to be covered is 3 units.
23+
We can cover a distance of 3 in the following ways:
24+
25+
Path 1: 1-1-1
26+
Path 2: 1-2
27+
Path 3: 2-1
28+
So, the first line of output is 3.
29+
Next line contains 2, indicating that the distance to be covered is 3 units.
30+
We can cover a distance of 2 in the following ways:
31+
32+
Path 1: 1-1
33+
Path 2: 2
34+
So, the second line of output is 2.
35+
36+
'''
37+
def dist(n):
38+
if n<=2:
39+
return n
40+
return dist(n-1)+dist(n-2)
41+
for _ in range(int(input())):
42+
print(dist(int(input())))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'''
2+
Find the Runner-Up Score!
3+
link- https://www.hackerrank.com/contests/10x-02-09-2021-1/challenges/find-second-maximum-number-in-a-list/problem
4+
'''
5+
if __name__ == '__main__':
6+
n = int(input())
7+
arr =[int(x) for x in input().split()]
8+
ma=-10000
9+
sec=ma
10+
a=[]
11+
for i in range(n):
12+
if arr[i]>sec and arr[i]!=ma:
13+
sec=arr[i]
14+
if sec>ma:
15+
ma,sec=sec,ma
16+
17+
print(sec)

‎10X/Python/Array/Hack Money.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'''
2+
Hack Money
3+
Description
4+
You are a bank account hacker. Initially you have 1 rupee in your account, and you want exactly N rupees
5+
in your account. You wrote two hacks, First hack can multiply the amount of money you own by 10, while the
6+
second can multiply it by 20. These hacks can be used any number of time . Can you achieve the desired
7+
amount N using these hacks.
8+
9+
Input
10+
The first line of the input contains a single integer T denoting the number of test cases.
11+
12+
The description of T test cases follows.The first and only line of each test case contains a single integer N.
13+
14+
Output
15+
For each test case, print a single line containing the string "Yes" if you can make exactly N rupees or "No" otherwise.
16+
17+
Example
18+
Input:
19+
20+
5
21+
22+
1
23+
24+
2
25+
26+
10
27+
28+
25
29+
30+
200
31+
32+
Output:
33+
34+
Yes
35+
36+
No
37+
38+
Yes
39+
40+
No
41+
42+
Yes
43+
44+
Explanation
45+
In the 5th case hacker can get Rs. 200 by first using 10x hack and using 20x hack.
46+
47+
'''
48+
def hackm(n):
49+
if n==0:
50+
return False
51+
if n==1:
52+
return True
53+
if n%10==0 and n%20==0:
54+
return hackm(n//10) or hackm(n//20)
55+
elif n%10==0:
56+
return hackm(n//10)
57+
elif n%20==0:
58+
return hackm(n//20)
59+
else:
60+
return False
61+
for i in range (int(input())):
62+
print("Yes"if hackm(int(input()))==True else "No")
63+

0 commit comments

Comments
 (0)
Please sign in to comment.