Skip to content

Commit

Permalink
chore: day 2 puzzles added
Browse files Browse the repository at this point in the history
  • Loading branch information
sanghaibiraj committed Mar 22, 2024
1 parent b247b1d commit 3e5379c
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 0 deletions.
9 changes: 9 additions & 0 deletions easy/day_2/problem.txt
Original file line number Diff line number Diff line change
@@ -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.
17 changes: 17 additions & 0 deletions easy/day_2/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <iostream>

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;
}
21 changes: 21 additions & 0 deletions hard/day_2/problem.txt
Original file line number Diff line number Diff line change
@@ -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.]
25 changes: 25 additions & 0 deletions hard/day_2/sample_test_cases.txt
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions hard/day_2/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//Write your code here.
5 changes: 5 additions & 0 deletions medium/day_2/problem.txt
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 12 additions & 0 deletions medium/day_2/prog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <iostream>

void mysteryFunction() {
int* ptr;
*ptr = 10;
std::cout << *ptr << std::endl;
}

int main() {
mysteryFunction();
return 0;
}

0 comments on commit 3e5379c

Please sign in to comment.