Skip to content

Commit a8effa0

Browse files
2 parents 751567e + cfaf75d commit a8effa0

File tree

16 files changed

+691
-6
lines changed

16 files changed

+691
-6
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.io.*;
2+
import java.util.*;
3+
import java.text.*;
4+
import java.math.*;
5+
import java.util.regex.*;
6+
7+
public class Solution {
8+
9+
public static void main(String[] args) {
10+
Scanner input = new Scanner(System.in);
11+
///////Variables//////
12+
int n = input.nextInt();
13+
int k = input.nextInt();
14+
int maxLuck = 0;
15+
ArrayList<Integer> importantContests = new ArrayList<>();
16+
//////////////////////
17+
18+
//Build list of important contests
19+
for(int i = 0; i < n; i++){
20+
int luck = input.nextInt();
21+
int important = input.nextInt();
22+
23+
if(important != 1)
24+
{
25+
maxLuck += luck;
26+
}
27+
28+
else
29+
importantContests.add(luck);
30+
}
31+
32+
//Sort the important contests in descending order
33+
Collections.sort(importantContests, Collections.reverseOrder());
34+
35+
//Lose the k largest contests and win the rest
36+
for(int i = 0; i < importantContests.size(); i++){
37+
if(i < k)
38+
maxLuck += importantContests.get(i);
39+
else
40+
maxLuck -= importantContests.get(i);
41+
}
42+
43+
System.out.println(maxLuck);
44+
}
45+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
Here, We can compare a sorted array to the original
3+
and just keep track of the differences. As we
4+
go we will need to perform the swaps, becasue we
5+
re not guranteed 1 swap will fix both elements.
6+
We will do this on both a ascending sort and a
7+
descending sort as both meet the minimal
8+
requirement
9+
10+
Time Complexity: O(n log(n)) //We must sort the input
11+
Space Complexity: O(n) //We store the input in an array
12+
*/
13+
14+
import java.io.*;
15+
import java.util.*;
16+
17+
public class solution {
18+
19+
public static void main(String[] args) {
20+
Scanner input = new Scanner(System.in);
21+
int n = input.nextInt();
22+
23+
int sortedSwaps = 0;
24+
int[] homework = new int[n];
25+
Integer[] homeworkSorted = new Integer[n];
26+
Map<Integer,Integer> original = new HashMap<>();
27+
28+
int sortedReverseSwaps = 0;
29+
int[] homework2ndCopy = new int[n];
30+
Map<Integer,Integer> original2ndCopy = new HashMap<>();
31+
32+
//Initialize our arrays and maps
33+
for(int i = 0; i < n; i++)
34+
{
35+
homeworkSorted[i] = input.nextInt();
36+
homework[i] = homeworkSorted[i];
37+
homework2ndCopy[i] = homeworkSorted[i];
38+
original.put(homework[i],i);
39+
original2ndCopy.put(homework2ndCopy[i],i);
40+
}
41+
42+
Arrays.sort(homeworkSorted);//Sort the input ascending
43+
44+
for(int i = 0; i < n; i++)
45+
{
46+
if(homework[i] != homeworkSorted[i])
47+
{
48+
//swap the element from homework to the right position
49+
int tmp = homework[i];
50+
homework[i] = homework[original.get(homeworkSorted[i])];
51+
homework[original.get(homeworkSorted[i])] = tmp;
52+
//Update index after swap
53+
original.put(tmp,original.get(homeworkSorted[i]));
54+
sortedSwaps++;
55+
}
56+
}
57+
58+
Arrays.sort(homeworkSorted, Collections.reverseOrder());//Sort the input descending
59+
60+
for(int i = 0; i < n; i++)
61+
{
62+
if(homework2ndCopy[i] != homeworkSorted[i])
63+
{
64+
//swap the element from homework to the right position
65+
int tmp = homework2ndCopy[i];
66+
homework2ndCopy[i] = homework2ndCopy[original.get(homeworkSorted[i])];
67+
homework2ndCopy[original2ndCopy.get(homeworkSorted[i])] = tmp;
68+
//Update index after swap
69+
original2ndCopy.put(tmp, original2ndCopy.get(homeworkSorted[i]));
70+
sortedReverseSwaps++;
71+
}
72+
}
73+
System.out.println(Math.min(sortedSwaps,sortedReverseSwaps));//Choose the smallest of the two possible smallest
74+
}
75+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
def homework(n, arr):
3+
4+
pos = dict()
5+
for i, num in enumerate(arr):
6+
pos[num] = i
7+
8+
cnt = 0
9+
sorted_arr = sorted(arr)
10+
11+
for i in range(len(arr)):
12+
if arr[i] != sorted_arr[i]:
13+
cnt += 1
14+
15+
new_idx = pos[sorted_arr[i]]
16+
pos[arr[i]] = new_idx
17+
arr[i], arr[new_idx] = arr[new_idx], arr[i]
18+
19+
return cnt
20+
21+
# input the number of elements of the array
22+
n = int(input().strip())
23+
# input elements of array
24+
arr = list(map(int, input().strip().split()))
25+
26+
asc = homework(n, arr)
27+
desc = homework(n, list(reversed(arr)))
28+
# print the minimum number of swaps needed to make the array beautiful
29+
print(min(asc, desc))
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
// Implementation Part
6+
int beautifulBinaryString(string b) {
7+
8+
int count = 0;
9+
/*
10+
* if we found 010 then if will be efficient,
11+
* to change last 0 ---> 1
12+
* so that it can prevent 010
13+
*/
14+
15+
// traversing string b
16+
for(int i = 2; i < b.size(); ){
17+
18+
// checking whether substring 010 is present or not
19+
// based on that increment index i
20+
if(b[i - 2] == '0' && b[i - 1] == '1' && b[i] == '0'){
21+
count++;
22+
i = i + 3;
23+
}else{
24+
i++;
25+
}
26+
}
27+
return count;
28+
}
29+
30+
int main()
31+
{
32+
ofstream fout(getenv("OUTPUT_PATH"));
33+
34+
int n;
35+
cin >> n;
36+
cin.ignore(numeric_limits<streamsize>::max(), '\n');
37+
38+
string b;
39+
getline(cin, b);
40+
41+
int result = beautifulBinaryString(b);
42+
43+
fout << result << "\n";
44+
45+
fout.close();
46+
47+
return 0;
48+
}
49+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
We could use a greedy approach and starting from the
3+
left everytime we see a 010 replace the last 0 with a 1
4+
and continue
5+
*/
6+
7+
import java.io.*;
8+
import java.util.*;
9+
10+
public class Solution {
11+
12+
public static void main(String[] args) {
13+
// Read input from STDIN and Print output to STDOUT
14+
Scanner input = new Scanner(System.in);
15+
int n = input.nextInt(); // length of binary string
16+
input.nextLine();
17+
String s = input.nextLine(); // single binary string of length n
18+
int switches = 0;
19+
20+
for(int i = 0; i < s.length()-2; i++) // left to right character of binary string
21+
{
22+
if(s.charAt(i) == '0' && s.charAt(i+1) == '1' && s.charAt(i+2) == '0')
23+
{
24+
switches++;
25+
i += 2;
26+
}
27+
}
28+
// minimum number of steps needed to make the string beautiful
29+
System.out.println(switches);
30+
}
31+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import java.io.*;
2+
import java.math.*;
3+
import java.security.*;
4+
import java.text.*;
5+
import java.util.*;
6+
import java.util.concurrent.*;
7+
import java.util.regex.*;
8+
9+
public class solution {
10+
11+
private static String largestPalindrome(String number, int k) {
12+
char[] chars = number.toCharArray();
13+
int minChange = 0;
14+
for (int i = 0, j = chars.length - 1; i < j; i++, j--) {
15+
if (chars[i] != chars[j]) {
16+
minChange++;
17+
}
18+
}
19+
if (minChange > k) {
20+
return "-1";
21+
}
22+
int changeBoth = k - minChange;
23+
24+
// Iinitialize l and r by leftmost and
25+
// rightmost ends
26+
int i = 0;
27+
int j = chars.length - 1;
28+
for (; i <= j; i++, j--) {
29+
if (chars[i] != chars[j]) {
30+
// Replace left and right character by
31+
// maximum of both
32+
char maxChar = (char) Math.max(chars[i], chars[j]);
33+
if (maxChar != '9' && changeBoth - 1 >= 0) {
34+
/* If characters at ith or rth (one of them) is changed in the previous
35+
loop then subtract 1 from K (1 more is
36+
subtracted already) and make them 9 */
37+
chars[i] = '9';
38+
chars[j] = '9';
39+
changeBoth--;
40+
} else {
41+
chars[i] = maxChar;
42+
chars[j] = maxChar;
43+
minChange--;
44+
}
45+
} else {
46+
char maxChar = (char) Math.max(chars[i], chars[j]);
47+
/* If none of them is changed in the
48+
previous loop then subtract 2 from changeboth
49+
and convert both to 9 */
50+
if (maxChar != '9' && changeBoth - 2 >= 0) {
51+
chars[i] = '9';
52+
chars[j] = '9';
53+
changeBoth -= 2;
54+
}
55+
}
56+
}
57+
if (changeBoth != 0 && i - 1 == j + 1) {
58+
chars[i - 1] = '9';
59+
}
60+
String palindrome = new String(chars);
61+
return palindrome;
62+
}
63+
64+
public static void main(String[] args) {
65+
Scanner in = new Scanner(System.in);
66+
int n = in.nextInt();
67+
int k = in.nextInt();
68+
String number = in.next();
69+
System.out.println(largestPalindrome(number, k));
70+
}
71+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# input the number of digits and the maximum number of changes allowed
2+
n,k=map(int,input().split(' '))
3+
# input the n-digit string of numbers
4+
num=input()
5+
ss=list(num)
6+
ch=[False for i in range(n)]
7+
offset=0
8+
cc=0
9+
# print a string representation of the highest value achievable or -1
10+
for i in range(n>>1):
11+
if ord(ss[i])>ord(ss[n-1-i]):
12+
ss[n-1-i]=ss[i]
13+
ch[n-1-i]=True
14+
cc+=1
15+
elif ord(ss[i])<ord(ss[n-1-i]):
16+
ss[i] = ss[n-1-i]
17+
ch[i]=True
18+
cc+=1
19+
if cc>k:
20+
print(-1)
21+
else:
22+
k-=cc
23+
i=0
24+
while k and i<(n>>1):
25+
if ss[i]!='9':
26+
if ch[i] or ch[n-i-1]:
27+
ss[i]=ss[n-1-i]='9'
28+
k-=1
29+
elif k>=2:
30+
ss[i] = ss[n - 1 - i] = '9'
31+
k -= 2
32+
i+=1
33+
if n&1 and k:
34+
ss[(n>>1)]='9'
35+
print(''.join(ss))
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
// Implementation Part
6+
int makingAnagrams(string s1, string s2) {
7+
8+
// creating vector for finding frequency of letters
9+
vector<int> freq(26, 0);
10+
11+
// Get the frequency of each letter in s1
12+
for(int i = 0; i < s1.size(); i++){
13+
int index = (int)s1[i] - 97;
14+
freq[index]++;
15+
}
16+
17+
// getting difference of frequency of letters in s1 and s2
18+
for(int i = 0; i < s2.size(); i++){
19+
int index = (int)s2[i] - 97;
20+
freq[index]--;
21+
}
22+
23+
/*
24+
* So if frequency is 0 thet means letter has same frequency in s1 and s2
25+
* no need to delete
26+
* if frequency is not zero...we need to delete that element
27+
*/
28+
int count = 0;
29+
for(int i = 0; i < 26; i++)
30+
count += abs(freq[i]);
31+
32+
return count;
33+
}
34+
35+
int main()
36+
{
37+
ofstream fout(getenv("OUTPUT_PATH"));
38+
39+
string s1;
40+
getline(cin, s1);
41+
42+
string s2;
43+
getline(cin, s2);
44+
45+
int result = makingAnagrams(s1, s2);
46+
47+
fout << result << "\n";
48+
49+
fout.close();
50+
51+
return 0;
52+
}
53+

0 commit comments

Comments
 (0)