forked from adityabisoi/ds-algo-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request adityabisoi#567 from Aarushi11H/main
Algorithms solns
- Loading branch information
Showing
4 changed files
with
168 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters