Skip to content

Commit

Permalink
Merge pull request adityabisoi#567 from Aarushi11H/main
Browse files Browse the repository at this point in the history
Algorithms solns
  • Loading branch information
adityabisoi authored Dec 22, 2020
2 parents e01d82f + fff837c commit cfaf75d
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 3 deletions.
75 changes: 75 additions & 0 deletions Algorithms/Sorting/Lily's Homework/solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Here, We can compare a sorted array to the original
and just keep track of the differences. As we
go we will need to perform the swaps, becasue we
re not guranteed 1 swap will fix both elements.
We will do this on both a ascending sort and a
descending sort as both meet the minimal
requirement
Time Complexity: O(n log(n)) //We must sort the input
Space Complexity: O(n) //We store the input in an array
*/

import java.io.*;
import java.util.*;

public class solution {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();

int sortedSwaps = 0;
int[] homework = new int[n];
Integer[] homeworkSorted = new Integer[n];
Map<Integer,Integer> original = new HashMap<>();

int sortedReverseSwaps = 0;
int[] homework2ndCopy = new int[n];
Map<Integer,Integer> original2ndCopy = new HashMap<>();

//Initialize our arrays and maps
for(int i = 0; i < n; i++)
{
homeworkSorted[i] = input.nextInt();
homework[i] = homeworkSorted[i];
homework2ndCopy[i] = homeworkSorted[i];
original.put(homework[i],i);
original2ndCopy.put(homework2ndCopy[i],i);
}

Arrays.sort(homeworkSorted);//Sort the input ascending

for(int i = 0; i < n; i++)
{
if(homework[i] != homeworkSorted[i])
{
//swap the element from homework to the right position
int tmp = homework[i];
homework[i] = homework[original.get(homeworkSorted[i])];
homework[original.get(homeworkSorted[i])] = tmp;
//Update index after swap
original.put(tmp,original.get(homeworkSorted[i]));
sortedSwaps++;
}
}

Arrays.sort(homeworkSorted, Collections.reverseOrder());//Sort the input descending

for(int i = 0; i < n; i++)
{
if(homework2ndCopy[i] != homeworkSorted[i])
{
//swap the element from homework to the right position
int tmp = homework2ndCopy[i];
homework2ndCopy[i] = homework2ndCopy[original.get(homeworkSorted[i])];
homework2ndCopy[original2ndCopy.get(homeworkSorted[i])] = tmp;
//Update index after swap
original2ndCopy.put(tmp, original2ndCopy.get(homeworkSorted[i]));
sortedReverseSwaps++;
}
}
System.out.println(Math.min(sortedSwaps,sortedReverseSwaps));//Choose the smallest of the two possible smallest
}
}
71 changes: 71 additions & 0 deletions Algorithms/Strings/Highest Value Palindrome/solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class solution {

private static String largestPalindrome(String number, int k) {
char[] chars = number.toCharArray();
int minChange = 0;
for (int i = 0, j = chars.length - 1; i < j; i++, j--) {
if (chars[i] != chars[j]) {
minChange++;
}
}
if (minChange > k) {
return "-1";
}
int changeBoth = k - minChange;

// Iinitialize l and r by leftmost and
// rightmost ends
int i = 0;
int j = chars.length - 1;
for (; i <= j; i++, j--) {
if (chars[i] != chars[j]) {
// Replace left and right character by
// maximum of both
char maxChar = (char) Math.max(chars[i], chars[j]);
if (maxChar != '9' && changeBoth - 1 >= 0) {
/* If characters at ith or rth (one of them) is changed in the previous
loop then subtract 1 from K (1 more is
subtracted already) and make them 9 */
chars[i] = '9';
chars[j] = '9';
changeBoth--;
} else {
chars[i] = maxChar;
chars[j] = maxChar;
minChange--;
}
} else {
char maxChar = (char) Math.max(chars[i], chars[j]);
/* If none of them is changed in the
previous loop then subtract 2 from changeboth
and convert both to 9 */
if (maxChar != '9' && changeBoth - 2 >= 0) {
chars[i] = '9';
chars[j] = '9';
changeBoth -= 2;
}
}
}
if (changeBoth != 0 && i - 1 == j + 1) {
chars[i - 1] = '9';
}
String palindrome = new String(chars);
return palindrome;
}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt();
String number = in.next();
System.out.println(largestPalindrome(number, k));
}
}
19 changes: 19 additions & 0 deletions Algorithms/Strings/String Construction/solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for(int a0 = 0; a0 < n; a0++){
String s = in.next(); //input the string
System.out.println(s.chars().distinct().count()); //count the distinct characters in the string as copying
// distinct characters will cost 1 dollar each
}
}
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ Please read [contribution guidelines](CONTRIBUTING.md) for contributing to the p
||[Knapsack](https://www.hackerrank.com/challenges/unbounded-knapsack/problem)|Medium|[View](/Algorithms/Dynamic%20Programming/Knapsack/solution.cpp)|[View](/Algorithms/Dynamic%20Programming/Knapsack/solution.java)|[View](/Algorithms/Dynamic%20Programming/Knapsack/solution.py)|
||[The Bomberman Game](https://www.hackerrank.com/challenges/bomber-man/problem)|Medium|[View](/Algorithms/Implementation/The%20Bomberman%20Game/solution.cpp)|[View](/Algorithms/Implementation/The%20Bomberman%20Game/solution.java)|[View](/Algorithms/Implementation/The%20Bomberman%20Game/solution.py)|
||[Organizing Containers of Balls](https://www.hackerrank.com/challenges/organizing-containers-of-balls/problem)|Easy|[View](Algorithms/Organizing%20Containers%20of%20Balls/solution.cpp)|[View](Algorithms/Organizing%20Containers%20of%20Balls/solution.java)|[View](/Algorithms/Organizing%20Containers%20of%20Balls/solution.py)|
||[String Construction](https://www.hackerrank.com/challenges/string-construction/problem)|Easy|||[View](/Algorithms/Strings/String%20Construction/solution.py)|
||[Highest Value Palindrome](https://www.hackerrank.com/challenges/richie-rich/problem)|Medium|||[View](/Algorithms/Strings/Highest%20Value%20Palindrome/solution.py)|
||[Lily's Homework](https://www.hackerrank.com/challenges/lilys-homework/problem)|Medium|||[View](/Algorithms/Sorting/Lily's%20Homework/solution.py)|
||[String Construction](https://www.hackerrank.com/challenges/string-construction/problem)|Easy||[View](/Algorithms/Strings/String%20Construction/solution.java)|[View](/Algorithms/Strings/String%20Construction/solution.py)|
||[Highest Value Palindrome](https://www.hackerrank.com/challenges/richie-rich/problem)|Medium|||[View](/Algorithms/Strings/Highest%20Value%20Palindrome/solution.java)|[View](/Algorithms/Strings/Highest%20Value%20Palindrome/solution.py)|
||[Lily's Homework](https://www.hackerrank.com/challenges/lilys-homework/problem)|Medium||[View](/Algorithms/Sorting/Lily's%20Homework/solution.java)|[View](/Algorithms/Sorting/Lily's%20Homework/solution.py)|
| Contest Problems |
||[Squares of a Sorted Array](https://www.hackerrank.com/contests/coding-gym-mo0419/challenges/squares-of-a-sorted-array)|Medium|[View](/Contest%20Problems/Squares%20of%20a%20Sorted%20Array/solution.cpp)|[View](/Contest%20Problems/Squares%20of%20a%20Sorted%20Array/solution.java)|[View](/Contest%20Problems/Squares%20of%20a%20Sorted%20Array/solution.py)|
| Disjoint Sets |
Expand Down

0 comments on commit cfaf75d

Please sign in to comment.