Skip to content

Commit aa0c3e7

Browse files
committed
Merge branch 'master' of https://github.com/smv1999/CompetitiveProgrammingQuestionBank into Devincept15
2 parents 47c93fc + 6443a09 commit aa0c3e7

File tree

15 files changed

+877
-0
lines changed

15 files changed

+877
-0
lines changed

Arrays/leap.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* Title : Check whether the year entered is leap or not .
2+
If the entered year passes the conditions then it is considered to be as leap
3+
otherwise not .
4+
5+
*/
6+
import java.util.Scanner ;
7+
class leap
8+
{ int leap_year;
9+
10+
leap (int a) // Constructor
11+
{
12+
leap_year=a;
13+
}
14+
void check ()
15+
{
16+
if(leap_year%4==0)
17+
18+
{
19+
if(leap_year%100==0)
20+
{
21+
if(leap_year%400==0)
22+
{ System.out.println( " Leap year .");
23+
System.out.println( " \n 366 days in " +leap_year+ " year . \n 29 days in month of Feb . \n"); }
24+
else
25+
{
26+
System.out.println( " Not Leap year "); }
27+
}
28+
else
29+
{ System.out.println( " Leap year .");
30+
System.out.println( " \n 366 days in " + leap_year+ " year . \n 29 days in month of Feb . \n");}
31+
}
32+
33+
else
34+
{ System.out.println( " Not Leap year "); }
35+
36+
}
37+
38+
public static void main (String[] args)
39+
{
40+
int year ;
41+
Scanner in=new Scanner(System.in);
42+
year=in.nextInt();
43+
leap Y = new leap(year);
44+
Y.check();
45+
46+
}
47+
}
48+
/*
49+
1900
50+
Not Leap year .
51+
52+
2000
53+
Leap year .
54+
55+
366 days in year 2000 .
56+
29 days in month of Feb .
57+
58+
*/

Codechef Problems/TheLeadGame.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
The game of billiards involves two players knocking 3 balls around on a green baize table. Well, there is more to it, but for our purposes this is sufficient.
3+
4+
The game consists of several rounds and in each round both players obtain a score, based on how well they played. Once all the rounds have been played, the total score of each player is determined by adding up the scores in all the rounds and the player with the higher total score is declared the winner.
5+
6+
The Siruseri Sports Club organises an annual billiards game where the top two players of Siruseri play against each other. The Manager of Siruseri Sports Club decided to add his own twist to the game by changing the rules for determining the winner. In his version, at the end of each round, the cumulative score for each player is calculated, and the leader and her current lead are found. Once all the rounds are over the player who had the maximum lead at the end of any round in the game is declared the winner.
7+
8+
Consider the following score sheet for a game with 5 rounds:
9+
10+
Round Player 1 Player 2
11+
1 140 82
12+
2 89 134
13+
3 90 110
14+
4 112 106
15+
5 88 90
16+
The total scores of both players, the leader and the lead after each round for this game is given below:
17+
18+
Round Player 1 Player 2 Leader Lead
19+
1 140 82 Player 1 58
20+
2 229 216 Player 1 13
21+
3 319 326 Player 2 7
22+
4 431 432 Player 2 1
23+
5 519 522 Player 2 3
24+
Note that the above table contains the cumulative scores.
25+
26+
The winner of this game is Player 1 as he had the maximum lead (58 at the end of round 1) during the game.
27+
28+
Your task is to help the Manager find the winner and the winning lead. You may assume that the scores will be such that there will always be a single winner. That is, there are no ties.
29+
30+
Input
31+
The first line of the input will contain a single integer N (N ≤ 10000) indicating the number of rounds in the game. Lines 2,3,...,N+1 describe the scores of the two players in the N rounds. Line i+1 contains two integer Si and Ti, the scores of the Player 1 and 2 respectively, in round i. You may assume that 1 ≤ Si ≤ 1000 and 1 ≤ Ti ≤ 1000.
32+
Output
33+
Your output must consist of a single line containing two integers W and L, where W is 1 or 2 and indicates the winner and L is the maximum lead attained by the winner.
34+
Example
35+
Input:
36+
5
37+
140 82
38+
89 134
39+
90 110
40+
112 106
41+
88 90
42+
Output:
43+
1 58
44+
*/
45+
#include <bits/stdc++.h>
46+
using namespace std;
47+
int main()
48+
{
49+
int n, lead=INT_MIN,p1=0,p2=0,winner=0;
50+
cin >> n;
51+
for (int i = 0; i < n; i++)
52+
{
53+
int a,b;
54+
cin>>a>>b;
55+
//Adding the scores of player1 and player2 so that we can store there cumulative score sum in p1 and p2
56+
p1+=a;
57+
p2+=b;
58+
//If the cumulative sum of p1 is greater than p2
59+
if(p1>p2){
60+
//and their difference is greater than lead than the winner will be player 1
61+
if(p1-p2>lead){
62+
lead=p1-p2;
63+
winner=1;
64+
}
65+
}
66+
67+
else{
68+
//if their difference is greater than lead than the winner will be player 2
69+
if(p2-p1>lead){
70+
lead=p2-p1;
71+
winner=2;
72+
}
73+
}
74+
}
75+
cout<<winner<<" "<<lead;
76+
return 0;
77+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Class for creating nodes in linkedlist
2+
class SinglyLinkedListNode:
3+
def __init__(self, node_data):
4+
self.data = node_data
5+
self.next = None
6+
7+
#Class for building linkedlist
8+
class SinglyLinkedList:
9+
def __init__(self):
10+
self.head = None
11+
self.tail = None
12+
13+
#Function for inserting node
14+
def insert_node(self, node_data):
15+
node = SinglyLinkedListNode(node_data)
16+
17+
if not self.head:
18+
self.head = node
19+
else:
20+
self.tail.next = node
21+
22+
self.tail = node
23+
24+
#Function to print the linked list
25+
def print_singly_linked_list(node, sep):
26+
while node:
27+
print(str(node.data), end=' ')
28+
29+
node = node.next
30+
31+
# Function for getting elements in first and second linked list in array
32+
def printt(headd):
33+
itr = headd
34+
llstr = []
35+
while itr:
36+
llstr.append(itr.data)
37+
itr = itr.next
38+
return llstr
39+
40+
41+
# This function takes two linked list and compares it and merge two list
42+
def mergeLists(llist1, llist2):
43+
ll1 = printt(llist1)
44+
ll2 = printt(llist2)
45+
ll3 = (ll1 + ll2) #comparing and merging two linked list
46+
ll3.sort()
47+
lll = SinglyLinkedList() #creating new linkedlist
48+
for ii in ll3:
49+
lll.insert_node(ii) #Adding merged element to new linked list
50+
return lll.head
51+
52+
# Main funcation
53+
if __name__ == '__main__':
54+
55+
llist1_count = int(
56+
input("Enter the number of element to be in linked list 1: "))
57+
58+
llist1 = SinglyLinkedList()
59+
60+
print("Enter the elements to be added in list1 line by line")
61+
for _ in range(llist1_count):
62+
llist1_item = int(input())
63+
llist1.insert_node(llist1_item)
64+
65+
print("\n")
66+
llist2_count = int(
67+
input("Enter the number of element to be in linked list 2: "))
68+
69+
llist2 = SinglyLinkedList()
70+
71+
print("Enter the elements to be added in list2 line by line")
72+
for _ in range(llist2_count):
73+
llist2_item = int(input())
74+
llist2.insert_node(llist2_item)
75+
76+
llist3 = mergeLists(llist1.head, llist2.head)
77+
78+
print("\n")
79+
print("The merged linked list value: ")
80+
print_singly_linked_list(llist3, ' ')
81+
82+
'''
83+
Output window:
84+
85+
Enter the number of element to be in linked list 1: 3
86+
Enter the elements to be added in list1 line by line
87+
1
88+
2
89+
3
90+
91+
92+
Enter the number of element to be in linked list 2: 2
93+
Enter the elements to be added in list2 line by line
94+
3
95+
4
96+
97+
98+
The merged linked list value:
99+
1 2 3 3 4
100+
101+
Complexity:
102+
Time complexity: O(n)
103+
Space complexity: O(n)
104+
'''
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'''
2+
Aim: Given a polynomial P of a single indeterminate (or variable), x. Also,
3+
given the values of x and k. The task is to verify if P(x) = k.
4+
5+
'''
6+
7+
# taking the space separated input
8+
x_and_k = input().split()
9+
# the very first number denotes 'x'
10+
x = int(x_and_k[0])
11+
# eval is used to evaluate any expression
12+
# here if the evaluated expression results in the value of k, it would mean that it is valid
13+
# the value of k is second in the entered string x_and_k, we have selected it using indicing
14+
print(eval(input()) == int(x_and_k[1]))
15+
16+
'''
17+
18+
COMPLEXITY:
19+
20+
Time Complexity -> O(1)
21+
Space Complexity -> O(1)
22+
23+
Sample Input:
24+
1 4
25+
x**3 + x**2 + x + 1
26+
27+
Sample Output:
28+
True
29+
30+
Explanation:
31+
x = 1
32+
k = 4
33+
P(x) = x**3 + x**2 + x + 1
34+
P(1) = 1**3 + 1**2 + 1 + 1
35+
= 1 + 1 + 1 + 1
36+
= 4
37+
= k
38+
Hence, the polynomial is a valid one.
39+
40+
'''
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/* C++ program to print a given number in words. The program
2+
handles numbers from 0 to 9999
3+
Just take an example if the user privide the number
4+
INPUT-
5+
1-No of test cases
6+
1234-the given number
7+
OUTPUT-
8+
One thousand Two hundred and twenty four */
9+
#include<bits/stdc++.h>
10+
using namespace std;
11+
string one[] = { "", "one ", "two ", "three ", "four ",
12+
"five ", "six ", "seven ", "eight ",
13+
"nine ", "ten ", "eleven ", "twelve ",
14+
"thirteen ", "fourteen ", "fifteen ",
15+
"sixteen ", "seventeen ", "eighteen ",
16+
"nineteen " };
17+
18+
// strings at index 0 and 1 are not used, they is to
19+
// make array indexing simple
20+
string ten[] = { "", "", "twenty ", "thirty ", "forty ",
21+
"fifty ", "sixty ", "seventy ", "eighty ",
22+
"ninety " };
23+
/* ******* Your Functions Below ******** */
24+
25+
26+
// n is 1- or 2-digit number
27+
string numToWords(int n, string s)
28+
{
29+
string str = "";
30+
// if n is more than 19, divide it
31+
if (n > 19)
32+
str += ten[n / 10] + one[n % 10];
33+
else
34+
str += one[n];
35+
36+
// if n is non-zero
37+
if (n)
38+
str += s;
39+
40+
return str;
41+
}
42+
43+
// Function to print a given number in words
44+
string convertToWords(long n)
45+
{
46+
// stores word representation of given number n
47+
string out;
48+
49+
// handles digits at ten millions and hundred
50+
// millions places (if any)
51+
out += numToWords((n / 10000000), "crore ");
52+
53+
// handles digits at hundred thousands and one
54+
// millions places (if any)
55+
out += numToWords(((n / 100000) % 100), "lakh ");
56+
57+
// handles digits at thousands and tens thousands
58+
// places (if any)
59+
out += numToWords(((n / 1000) % 100), "thousand ");
60+
61+
// handles digit at hundreds places (if any)
62+
out += numToWords(((n / 100) % 10), "hundred ");
63+
64+
if (n > 100 && n % 100)
65+
out += "and ";
66+
67+
// handles digits at ones and tens places (if any)
68+
out += numToWords((n % 100), "");
69+
70+
return out;
71+
}
72+
int main()
73+
{
74+
int t;
75+
cin>>t;
76+
77+
while(t--)
78+
{
79+
long n ;
80+
cin>>n;
81+
82+
// convert given number in words
83+
cout << convertToWords(n) << endl;
84+
85+
86+
}
87+
}
88+
89+
/*Time complexity:
90+
Time complexity: O(1).
91+
The loop runs for a constant amount of time.
92+
Auxiliary space: O(1).
93+
As no extra space is required.
94+
Time complexity: O(1). */
95+
96+
97+
98+

0 commit comments

Comments
 (0)