You are on page 1of 19

EXPERIMENT NUMBER –1.

STUDENT’S NAME – Manish Kumar


STUDENT’S UID – 20BCS7420
CLASS AND GROUP – 20CSE10_B
SEMESTER – 2nd

TOPIC OF EXPERIMENT – Competitive problems covering concepts of conditional


statements.
AIM OF THE EXPERIMENT – Introduction to competitive programming

Problem Statement 1.1.1: Roman to Integer

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

For example, 2 is written as II in Roman numeral, just two one's added together. 12 is
written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V
+ II.

Roman numerals are usually written largest to smallest from left to right. However,
the numeral for four is not IIII. Instead, the number four is written as IV. Because the
one is before the five we subtract it making four. The same principle applies to the
number nine, which is written as IX. There are six instances where subtraction is used:

 I can be placed before V (5) and X (10) to make 4 and 9.


SUBJECT NAME-Computer Workshop SUBJECT CODE-20CSP155
 X can be placed before L (50) and C (100) to make 40 and 90.
 C can be placed before D (500) and M (1000) to make 400 and 900.

Given a roman numeral, convert it to an integer.

Example 1:

Input: s = "III"

Output: 3

Example 2:

Input: s = "IV"

Output: 4

Example 3:

Input: s = "IX"

Output: 9

Example 4:

Input: s = "LVIII"

Output: 58

Explanation: L = 50, V= 5, III = 3.

Example 5:

Input: s = "MCMXCIV"

Output: 1994

Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

Constraints:

SUBJECT NAME-Computer Workshop SUBJECT CODE-20CSP155


 1 <= s.length <= 15
 s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').
 It is guaranteed that s is a valid roman numeral in the range [1, 3999].

PROGRAM CODE:
#include <iostream>
#include <string>
using namespace std;

int value(char r)
{
if (r == 'I')
return 1;
if (r == 'V')
return 5;
if (r == 'X')
return 10;
if (r == 'L')
return 50;
if (r == 'C')
return 100;
if (r == 'D')
return 500;
if (r == 'M')
return 1000;

return -1;
}

int romanToDecimal(string& str)


{

int res = 0;

for (int i = 0; i < str.length(); i++)


{

int s1 = value(str[i]);
SUBJECT NAME-Computer Workshop SUBJECT CODE-20CSP155
if (i + 1 < str.length())
{

int s2 = value(str[i + 1]);

if (s1 >= s2)


{

res = res + s1;


}
else
{

res = res + s2 - s1;


i++;
}
}
else {
res = res + s1;
}
}
return res;
}

int main()
{

string str = "MCMIV";


cout << "Roman Numeral: ";
getline(cin, str);
cout << "Integer form of Roman Numeral is " << romanToDecimal(str) << endl;

system("pause 0");
return 0;
}

ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION


(Kindly jot down the compile time errors encountered)
SUBJECT NAME-Computer Workshop SUBJECT CODE-20CSP155
 N/A
PROGRAMS’ EXPLANATION (in brief):
As I run program the control go to main function in which I declare and defined sting
str. Then program display “Roman Numeral:” on console. Then enter any
Roman number such as “MCMXC”. Control go to romanToDecimal function, which
convert Roman number into decimal number. Display on console.

OUTPUT:

Problem Statement 1.1.2: Longest Common Prefix

Write a function to find the longest common prefix string among an array of strings. If
there is no common prefix, return an empty string "".

Example 1:

Input: strs = ["flower","flow","flight"]

Output: "fl"

SUBJECT NAME-Computer Workshop SUBJECT CODE-20CSP155


Example 2:

Input: strs = ["dog","racecar","car"]

Output: ""

Explanation: There is no common prefix among the input strings.

Constraints:

 0 <= strs.length <= 200


 0 <= strs[i].length <= 200
 strs[i] consists of only lower-case English letters.

PROGRAM CODE:
#include<iostream>
#include<algorithm>

using namespace std;

string longestCommonPrefix(string ar[], int n)


{

if (n == 0)
return "";

if (n == 1)
return ar[0];

sort(ar, ar + n);

int en = min(ar[0].size(),
ar[n - 1].size());

string first = ar[0], last = ar[n - 1];


int i = 0;
while (i < en && first[i] == last[i])
i++;
SUBJECT NAME-Computer Workshop SUBJECT CODE-20CSP155
string pre = first.substr(0, i);
return pre;
}

int main()
{
int n{ 0 };
cout << "Enter the number of word. ";
cin >> n;

string strs[] = { "flower", "flow", "flight" };

for (int i = 0; i < n; i++)


{
cin >> strs[i];
}

cout << "The longest common prefix is: " << longestCommonPrefix(strs, n) << endl;

return 0;
}

ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION


(Kindly jot down the compile time errors encountered)

 N/A

SUBJECT NAME-Computer Workshop SUBJECT CODE-20CSP155


OUTPUT:

Problem Statement 1.1.3: Conditional List

You will be given a list X of length L. You need to find whether there exists an element
which satisfies the following condition. Find y, such that

If there exist any y which satisfied this condition then print YES, otherwise print NO

Let give List X be


SUBJECT NAME-Computer Workshop SUBJECT CODE-20CSP155
1233

X(1)+X(2)=X(4), therefore position 3 satisfies the condition.

Output will be

YES

Input

The first line contains T, the number of test cases. For each test case, the first line
contains L, the number of elements in the list X. The second line for each test case
contains L space-separated integers, denoting the list X.

Output

For each test case print YES if condition exists; otherwise print NO.

Constraints

1≤T≤10

1≤L≤10^5

1≤Xi ≤2×10^4

1≤i≤L

SAMPLE INPUT

123

SUBJECT NAME-Computer Workshop SUBJECT CODE-20CSP155


SAMPLE OUTPUT

NO

PROGRAM CODE:

#include<iostream>

using namespace std;

int main()

int t;

cin >> t;

while (t--)

int n;

cin >> n;

long long sum = 0;

int brr[n + 1];

int arr[n + 1];

arr[0] = 0;

for (int i = 1; i <= n; ++i)

cin >> arr[i];

SUBJECT NAME-Computer Workshop SUBJECT CODE-20CSP155


brr[i] = arr[i];

arr[i] += arr[i - 1];

sum += arr[i];

bool ok = true;

for (int i = 1; i <= n; ++i)

if (arr[i - 1] == (arr[n] - arr[i]))

cout << "YES\n";

ok = false;

continue;

if (ok)

cout << "NO\n";

ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION


(Kindly jot down the compile time errors encountered)

SUBJECT NAME-Computer Workshop SUBJECT CODE-20CSP155


 N/A

OUTPUT:

Problem Statement 1.1.4: Count Primes

Count the number of prime numbers less than a non-negative number, n.

Example 1:

Input: n = 10

Output: 4

Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.

SUBJECT NAME-Computer Workshop SUBJECT CODE-20CSP155


Example 2:

Input: n = 0

Output: 0

Example 3:

Input: n = 1

Output: 0

Constraints:

 0 <= n <= 5 * 106

PROGRAM CODE:
#include <iostream>
using namespace std;

// function check whether a number


// is prime or not
bool isPrime(int n)
{
// Corner case
if (n <= 1)
return false;

// Check from 2 to n-1


for (int i = 2; i < n; i++)
if (n % i == 0)
return false;

return true;
}
// Function to print number of primes
int printNumberPrime(int n)
{
int count{};
for (int i = 2; i <= n; i++) {
if (isPrime(i))
SUBJECT NAME-Computer Workshop SUBJECT CODE-20CSP155
count++;
}

return count;
}
// Driver Code
int main()
{
int n{};
cin >> n;
cout << printNumberPrime(n);

return 0;
}

ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION


(Kindly jot down the compile time errors encountered)

 N/A
OUTPUT:

SUBJECT NAME-Computer Workshop SUBJECT CODE-20CSP155


Problem Statement 1.1.5: Happy Numbers

Write an algorithm to determine if a number n is happy.

A happy number is a number defined by the following process:

Starting with any positive integer, replace the number by the sum of the squares of its
digits.

Repeat the process until the number equals 1 (where it will stay), or it loops endlessly
in a cycle which does not include 1.

Those numbers for which this process ends in 1 are happy.

Return true if n is a happy number, and false if not.

Example 1:

Input: n = 19

Output: true

Explanation:

12 + 92 = 82

82 + 22 = 68

62 + 82 = 100
SUBJECT NAME-Computer Workshop SUBJECT CODE-20CSP155
12 + 02 + 02 = 1

Example 2:

Input: n = 2

Output: false

PROGRAM CODE:
#include <iostream>
using namespace std;

// Returns sum of squares of digits


// of a number n. For example for n = 12
// it returns 1 + 4 = 5
int sumDigitSquare(int n)
{
int sq = 0;
while (n) {
int digit = n % 10;
sq += digit * digit;
n = n / 10;
}
return sq;
}

// Returns true if n is Happy number


// else returns false.
bool isHappy(int n)
{
// Keep replacing n with sum of
// squares of digits until we either
// reach 1 or we end up in a cycle
while (1) {

// Number is Happy if we reach 1


SUBJECT NAME-Computer Workshop SUBJECT CODE-20CSP155
if (n == 1)
return true;

// Replace n with sum of squares


// of digits
n = sumDigitSquare(n);

// Number is not Happy if we reach 4


if (n == 4)
return false;
}

return false;
}

// Driver code
int main()
{
int n = 23;
cin >> n;
if (isHappy(n))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}

ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION


(Kindly jot down the compile time errors encountered)

 N/A

OUTPUT:

SUBJECT NAME-Computer Workshop SUBJECT CODE-20CSP155


LEARNING OUTCOMES
1. Apply coding skills to solve application based problems on competitive platforms
such as Hacker Rank/ Hacker Earth/Code Chef.
2. Understand the basic concept and structure of computer hardware
3. Identify the existing configuration of the computers and peripherals.
4. Installing and uninstalling multiple operating systems on a machine.
5. Apply their knowledge about computer peripherals to identify /rectify problems
on-board.

EVALUATION COLUMN (To be filled by concerned faculty only)

Sr. No. Parameters Maximum Marks


Marks Obtained
1. Worksheet Completion including 10
writing learning objective/ Outcome
2. Post Lab Quiz Result 5

3. Student engagement in Simulation/ 5


SUBJECT NAME-Computer Workshop SUBJECT CODE-20CSP155
Performance/ Pre Lab Questions
4. Total Marks 20

SUBJECT NAME-Computer Workshop SUBJECT CODE-20CSP155

You might also like