Skip to content

Commit 7477374

Browse files
committed
Added more stuff
1 parent a2735f0 commit 7477374

File tree

7 files changed

+464
-5
lines changed

7 files changed

+464
-5
lines changed

Bit Manipulation/Unset.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ int turnOffIthBit(int n, int i){
3636
return (n & ~(1 << (i)));
3737

3838
}
39+
40+
3941
int main( int argc , char ** argv )
4042
{
4143
ios_base::sync_with_stdio(false) ;
@@ -49,9 +51,4 @@ int main( int argc , char ** argv )
4951

5052
return 0;
5153

52-
53-
54-
55-
56-
5754
}

Greedy/Problemdiscussion.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
3+
Name: Mehul Chaturvedi
4+
IIT-Guwahati
5+
6+
*/
7+
8+
/*
9+
PROBLEM STATEMENT
10+
Harshit gave Aahad an array of size N and asked to minimize the difference between the maximum
11+
value and minimum value by modifying the array under the condition that each array element either
12+
increase or decrease by k(only once).
13+
It seems difficult for Aahad so he asked for your help
14+
Input Format
15+
The First line contains two space-separated integers: N,K
16+
Next lines contain N space-separated integers denoting elements of the array
17+
Output Format
18+
The output contains a single integer denoting the minimum difference between maximum value and
19+
the minimum value in the array
20+
Constraints
21+
1 =< N <= 10^5
22+
1 <= Ai,K <= 10^9
23+
Sample Input1:
24+
3 6
25+
1 15 10
26+
Sample Output1:
27+
5
28+
Explaination
29+
We change from 1 to 6, 15 to 9 and 10 to 4. Maximum difference is 5 (between 4 and 9). We can't
30+
get a lower difference.
31+
32+
*/
33+
34+
35+
#include <bits/stdc++.h>
36+
37+
using namespace std;
38+
39+
int getMinDiff(int arr[], int n, int k)
40+
{
41+
if (n == 1)
42+
return 0;
43+
44+
// Sort all elements
45+
sort(arr, arr+n);
46+
47+
// Initialize result
48+
int ans = arr[n-1] - arr[0];
49+
50+
// Handle corner elements
51+
int small = arr[0] + k;
52+
int big = arr[n-1] - k;
53+
if (small > big)
54+
swap(small, big);
55+
56+
// Traverse middle elements
57+
for (int i = 1; i < n-1; i ++)
58+
{
59+
int subtract = arr[i] - k;
60+
int add = arr[i] + k;
61+
62+
// If both subtraction and addition
63+
// do not change diff
64+
if (subtract >= small || add <= big)
65+
continue;
66+
67+
// Either subtraction causes a smaller
68+
// number or addition causes a greater
69+
// number. Update small or big using
70+
// greedy approach (If big - subtract
71+
// causes smaller diff, update small
72+
// Else update big)
73+
if (big - subtract <= add - small)
74+
small = subtract;
75+
else
76+
big = add;
77+
}
78+
79+
return min(ans, big - small);
80+
}
81+
82+
int main( int argc , char ** argv )
83+
{
84+
ios_base::sync_with_stdio(false) ;
85+
cin.tie(NULL) ;
86+
87+
int n,k;
88+
cin>>n>>k;
89+
90+
int* arr = new int[n];
91+
92+
for (int i = 0; i < n; ++i)
93+
{
94+
cin>>arr[i];
95+
}
96+
97+
cout << getMinDiff(arr, n, k) << '\n';
98+
99+
100+
return 0 ;
101+
102+
103+
104+
}

adhoc problems/Circularlist.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
3+
Name: Mehul Chaturvedi
4+
IIT-Guwahati
5+
6+
*/
7+
8+
/*
9+
PROBLEM STATEMENT
10+
You are given a circular list of students as follows:
11+
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 11
12+
This list is circular, means that 11 will follow 0 again. You will be given the student number ‘i’ and
13+
some position ‘p’. You will have to tell that if the list will start from (i+1)th student,
14+
then which student will be at pth position.
15+
Input Format:
16+
First line will have an integer ‘t’, denoting the number of test cases.
17+
Next line will have two space separated integers denoting the value of ‘i’ and ‘p’ respectively.
18+
Output Format:
19+
Print ‘t’ lines containing single integer denoting the student number.
20+
Constraints:
21+
1 <= t <= 10^5
22+
0 <= i <= 11
23+
1 <= p <= 12
24+
Sample Input:
25+
2
26+
2 3
27+
5 8
28+
Sample Output:
29+
5
30+
1
31+
Explanation:
32+
First, list will start at 3. 3 -> 4 -> 5. Hence, 5 will be at third position.
33+
Second, list will start at 6. 6 -> 7 -> 8 -> 9 -> 10 -> 11 -> 0 -> 1. Hence, 1 will be at 8th
34+
position.
35+
*/
36+
37+
38+
#include <bits/stdc++.h>
39+
40+
using namespace std;
41+
42+
int go(int i, int p){
43+
int ans = (i+p)%12
44+
return ans;
45+
}
46+
47+
48+
int main( int argc , char ** argv )
49+
{
50+
ios_base::sync_with_stdio(false) ;
51+
cin.tie(NULL) ;
52+
53+
int t;
54+
cin>>t;
55+
56+
while(t--){
57+
int i, p;
58+
cin>>i>>p;
59+
60+
cout << go(i, p) << '\n';
61+
}
62+
63+
64+
return 0 ;
65+
66+
67+
68+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
3+
Name: Mehul Chaturvedi
4+
IIT-Guwahati
5+
6+
*/
7+
8+
/*
9+
PROBLEM STATEMENT
10+
Professor Jain has a class full of notorious students. To get anything done from them is a herculean task. Prof Jain wanted to organize a test. He gave this responsibility to Aahad. Aahad did an excellent job of organizing the test. As a reward, the professor gave him an sequence of numbers to play with. But Aahad likes playing with "interesting" sequence of numbers, which are sequences that have equal elements.
11+
Now, the problem is - Prof Jain has a sequence with elements, and that sequence isn't always "interesting”. To ensure sequence has equal elements, Prof Jain has 2 options:
12+
1) Choose two elements of sequence . DECREASE the first element by 1 and INCREASE the second element by 1. This operation costs 'k' coins.
13+
2) Choose one element of array and INCREASE it by 1. This operation costs 'l' coins.
14+
What’s the minimum number of coins Prof Jain needs to turn his sequence into a “interesting" sequence for Aahad?
15+
Input Format
16+
The first line of input contains three space-separated integers: n, k, l . Integer n is the size of array . Integer k is the number of coins needed to perform the first operation. Integer l is the number of coins needed to perform the second operation.
17+
18+
The second line contains n integers: (a1, a2, a3... an) representing sequence.
19+
Constraints:
20+
1 <= n, k, l <= 1000
21+
1 <= ai <= 1000
22+
Time Limit: 1 second
23+
Output Format
24+
In single line, print one integer number: the minimum number of coins required to make "interesting" sequence.
25+
Sample Test Cases:
26+
Sample Input 1:
27+
4 1 2
28+
3 4 2 2
29+
Sample Output 1:
30+
3
31+
Explanation Output 1 :
32+
The professor has a sequence with 4 elements. To perform the first operation, they must pay 1 coin and to perform the second operation, they must pay 2 coins. The optimal strategy is:
33+
34+
-Perform the second operation on the fourth element. Now the sequence is {3, 4, 2, 3}. This costs 2 coins.
35+
36+
-Perform the first operation on the second and third element. The sequence is now "interesting", and it looks like {3, 3, 3, 3}. This costs 1 coin.
37+
38+
The total amount of coins needed is 2 + 1 = 3.
39+
Sample Input 2:
40+
3 2 1
41+
5 5 5
42+
Sample Output 2:
43+
0
44+
Explanation Output 2 :
45+
The given sequence is already "interesting". The professor would spend 0 coins.
46+
Sample Input 3:
47+
5 2 1
48+
1 2 3 4 5
49+
Sample Output 3:
50+
6
51+
Explanation Output 3 :
52+
The professor has a sequence with 5 elements. To perform the first operation, they must pay 2 coin and to perform the second operation, they must pay 1 coin. The optimal strategy is:
53+
54+
-Perform the first operation on the first and last element. Now the sequence is {2, 2, 3, 4, 4}. This costs 2 coins.
55+
56+
-Perform the first operation again on the first and last element. Now the sequence is {3, 2, 3, 4, 3}. This costs 2 coins.
57+
58+
-Perform the first operation on the second and second last element. Now the sequence is {3, 3, 3, 3, 3}. This costs 2 coins.
59+
60+
The total amount of coins needed is 2 + 2 + 2 = 6.
61+
*/
62+
63+
64+
#include <bits/stdc++.h>
65+
66+
using namespace std;
67+
68+
int go(int* arr, int n, int k, int l){
69+
int maximum = arr[0];
70+
int minimum = arr[0];
71+
for (int i = 0; i < n; ++i)
72+
{
73+
maximum = max(arr[i], maximum);
74+
minimum = min(arr[i], minimum);
75+
}
76+
// cout << maximum << '\n';
77+
// cout << minimum << '\n';
78+
79+
int ans = INT_MAX;
80+
81+
for (int number = minimum; number <= maximum; ++number)
82+
{
83+
//cout << "here" << '\n';
84+
int increase = 0;
85+
int decrease = 0;
86+
for (int i = 0; i < n; ++i)
87+
{
88+
if (arr[i]>number)
89+
{
90+
decrease += arr[i]-number;
91+
}else if (arr[i]<number)
92+
{
93+
increase += number-arr[i];
94+
}
95+
}
96+
cout << "Decrease: "<<decrease << '\n';
97+
cout << "Increase: "<<increase << '\n';
98+
99+
if (decrease>increase)
100+
{
101+
continue;
102+
}
103+
int cost = 0;
104+
105+
cost = decrease*k + (increase-decrease)*l;
106+
// cout << "cost: "<<cost << '\n';
107+
ans = min(ans, cost);
108+
109+
110+
}
111+
112+
return ans;
113+
114+
}
115+
116+
117+
int main( int argc , char ** argv )
118+
{
119+
ios_base::sync_with_stdio(false) ;
120+
cin.tie(NULL) ;
121+
122+
int n, k, l;
123+
124+
cin>>n>>k>>l;
125+
int* arr = new int[n];
126+
127+
for (int i = 0; i < n; ++i)
128+
{
129+
cin>>arr[i];
130+
}
131+
132+
cout << go(arr, n, k, l) << '\n';
133+
134+
delete [] arr;
135+
136+
return 0 ;
137+
138+
139+
140+
}

adhoc problems/Test.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
4 1000 0
2+
3 4 2 2

adhoc problems/a.out

20.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)