diff --git a/easy/day_2/problem.txt b/easy/day_2/problem.txt new file mode 100644 index 0000000..2e8f01b --- /dev/null +++ b/easy/day_2/problem.txt @@ -0,0 +1,9 @@ +Write a c++ program to take |n| numbers from user a1, a2, ..., an and find their sum. +|x| implies absolute of x, if x=-5 |x|=5, if x=5 |x|=5. + +Constraints: +-10^5<=n<=10^5 +-10^9<=ai<=10^9 + +Solution to the code is provided in solution.cpp file, but the code is failing in some test cases, DEBUG it. +Note: Printing the strings is not causing any error, the sum is coming wrong. \ No newline at end of file diff --git a/easy/day_2/solution.cpp b/easy/day_2/solution.cpp new file mode 100644 index 0000000..94372c9 --- /dev/null +++ b/easy/day_2/solution.cpp @@ -0,0 +1,17 @@ +#include + +int main() { + int n; + int sum = 0; + std::cout << "Enter the number of elements: "; + std::cin >> n; + std::cout << "Enter " << n << " numbers:\n"; + for (int i = 0; i < n; ++i) { + int num; + std::cout << "Enter number " << i + 1 << ": "; + std::cin >> num; + sum += num; + } + std::cout << "Sum of the " << n << " numbers is: " << sum << std::endl; + return 0; +} diff --git a/hard/day_2/problem.txt b/hard/day_2/problem.txt new file mode 100644 index 0000000..8e04d87 --- /dev/null +++ b/hard/day_2/problem.txt @@ -0,0 +1,21 @@ +Consider an integer sequence of length 2N, denoted as A = (A1, A2, ..., A2N), where each integer from 1 to N appears exactly twice. Our goal is to transform A into a specific type of sequence known as a "bright sequence" by performing a series of operations. In a bright sequence, for every index i ranging from 1 to N, the value of the element at index 2i-1 is equal to the value of the element at index 2i. + +To achieve this, we are allowed to swap the values of two adjacent terms within the sequence A any number of times. Let's denote K as the minimum number of operations required to transform A into a bright sequence. Our task is to determine the lexicographically smallest bright sequence that can be obtained after performing these K operations on A. + +Constraints +1≤N≤2×10^5 +1≤A[i]≤N +A contains each integer i=1,2,…,N exactly twice. +All input values are integers. + +Input +The input is given from Standard Input in the following format: +N +A1 A2 … A2N +​ +Output +Print the lexicographically smallest good sequence that can be obtained by performing the operations K times on A, with spaces in between. + +TASK: +Write a solution for this code in solution.cpp in the best time and memory complexity you can think of. +[Check the sample_test_case.txt file for sample test cases.] \ No newline at end of file diff --git a/hard/day_2/sample_test_cases.txt b/hard/day_2/sample_test_cases.txt new file mode 100644 index 0000000..05acf8c --- /dev/null +++ b/hard/day_2/sample_test_cases.txt @@ -0,0 +1,25 @@ +Sample Input 1 +3 +3 2 1 2 3 1 + +Sample Output 1 +2 2 3 3 1 1 + +For example, we can perform the operation four times as (3,2,1,2,3,1)→(3,2,1,3,2,1)→(3,2,3,1,2,1)→(3,3,2,1,2,1)→(3,3,2,2,1,1) to make A a good sequence. This number is the minimum needed. With four operations, we can also make A=(2,2,3,3,1,1), and the answer is (2,2,3,3,1,1). + + + +Sample Input 2 +3 +1 1 2 2 3 3 + +Sample Output 2 +1 1 2 2 3 3 + + + +Sample Input 3 +15 +15 12 11 10 5 11 13 2 6 14 3 6 5 14 10 15 1 2 13 9 7 4 9 1 3 8 12 4 8 7 +Sample Output 3 +11 11 5 5 6 6 10 10 14 14 15 15 2 2 12 12 13 13 1 1 3 3 9 9 4 4 7 7 8 8 \ No newline at end of file diff --git a/hard/day_2/solution.cpp b/hard/day_2/solution.cpp new file mode 100644 index 0000000..d0233ef --- /dev/null +++ b/hard/day_2/solution.cpp @@ -0,0 +1 @@ +//Write your code here. \ No newline at end of file diff --git a/medium/day_2/problem.txt b/medium/day_2/problem.txt new file mode 100644 index 0000000..bc05536 --- /dev/null +++ b/medium/day_2/problem.txt @@ -0,0 +1,5 @@ +You are given a C++ program in prog.cpp file that compiles and runs without error, but it prints out garbage data when executed. Your task is to find and fix the issue causing the incorrect output without changing anything outside of a single function. + +Write in comments what was causing the error in this code and how did you fix it. + +Also, don't just remove the pointer to solve the problem, THINK. \ No newline at end of file diff --git a/medium/day_2/prog.cpp b/medium/day_2/prog.cpp new file mode 100644 index 0000000..99c0935 --- /dev/null +++ b/medium/day_2/prog.cpp @@ -0,0 +1,12 @@ +#include + +void mysteryFunction() { + int* ptr; + *ptr = 10; + std::cout << *ptr << std::endl; +} + +int main() { + mysteryFunction(); + return 0; +}