Skip to content

Commit

Permalink
Merge pull request #6 from terodea/pepcoding
Browse files Browse the repository at this point in the history
Array problems java and scala
  • Loading branch information
terodea authored Jan 21, 2022
2 parents e554542 + 2aa3f82 commit bee5a66
Show file tree
Hide file tree
Showing 10 changed files with 273 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.metals
*.vscode
34 changes: 34 additions & 0 deletions pepcoding/java/L2/ArrayStrings/maxChunksToMakeArraySortedII.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.util.*;
public class maxChunksToMakeArraySortedII {

// ~~~~~~~~~~~~~~~User Section~~~~~~~~~~~~~~~~~
public static int maxChunksToSorted2(int[] arr) {
int count = 0;
int[] rightMin = new int[arr.length+1];
int n = arr.length;
rightMin[n] = Integer.MAX_VALUE;

for(int i= n-1; i>=0; i--){
rightMin[i] = Math.min(rightMin[i+1], arr[i]);
}

int leftMax = Integer.MIN_VALUE;

for(int i=0; i<n; i++){

leftMax = Integer.max(arr[i], leftMax);

if(leftMax <= rightMin[i+1]) {
count++;
}
}
return count;
}

// ~~~~~~~~~~~~~~~Input Management~~~~~~~~~~~~~~~~~
public static void main(String[] args) {
int[] arr = {4,3,6,5,7,12,11,10};
int res = maxChunksToSorted2(arr);
System.out.println(res);
}
}
33 changes: 33 additions & 0 deletions pepcoding/java/L2/ArrayStrings/noOfSubArraysWithBoundedMax.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
public class noOfSubArraysWithBoundedMax{
public static int numSubarrayBoundedMax(int[] arr, int left, int right) {
int prev_count = 0;
int overall_count = 0;
int i=0;
int j=0;
while(j < arr.length){

if(left <= arr[j] && arr[j] <= right){

prev_count = j - i + 1;
overall_count += prev_count;

}else if(arr[j] < left){

overall_count += prev_count;

}else{
prev_count = 0;
i = j + 1;
}
j++;
}
return overall_count;
}

public static void main(String[] args){
int[] arr = {2,1,4,3};
int left = 2;
int right= 3;
System.out.println(numSubarrayBoundedMax(arr, left, right));
}
}
38 changes: 38 additions & 0 deletions pepcoding/java/L2/ArrayStrings/reverseVowelsOfString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
public class reverseVowelsOfString{
private static boolean isVowel(char ch){
String vowels = "aeiouAEIOU";
return vowels.contains(ch + "");
}

public static String reverseVowels(String s){

char[] arr = s.toCharArray();
int left = 0;
int right = arr.length - 1;

while(left < right){

while(left < right && isVowel(arr[left]) == false){
left++;
}

while(left < right && isVowel(arr[right]) == false){
right--;
}

char temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;

left++;
right--;
}

return String.valueOf(arr);
}

public static void main(String[] args){
String str = "hello";
System.out.println(reverseVowels(str));
}
}
27 changes: 27 additions & 0 deletions pepcoding/java/L2/ArrayStrings/wiggleSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
public class wiggleSort{
public static void wiggleSort(int[] arr) {
for(int i=0; i<arr.length - 1; i++){
if(i % 2 == 0){
if(arr[i] > arr[i+1]){
swap(arr, i, i+1);
}
}
else{
if(arr[i] < arr[i+1]){
swap(arr, i, i+1);
}
}
}
}

private static void swap(int[] arr, int i, int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

public static void main(String[] args){
int[] arr = {3,5,2,1,6,4};
wiggleSort(arr).toString()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ object maxChunksToMakeArraySorted{

def main(args: Array[String]): Unit{
var data = Array(4,3,2,1,0)
maxChunksToMakeArraySorted(arr=data)
}
}
26 changes: 26 additions & 0 deletions pepcoding/scala/L2/ArrayStrings/maxChunksToMakeArraySortedII.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
object Solution {
def maxChunksToSorted(arr: Array[Int]): Int = {
var count=0;
var rightMin:Array[Int] = new Array[Int](arr.size+1)
var n = arr.size
rightMin(n) = Int.MaxValue
for (i <- (1 to n-1).reverse){
rightMin(i) = Math.min(rightMin(i+1), arr(i))
}

var leftMax = -1

for(i <- 0 to n-1){
leftMax = (arr(i)).max(leftMax)
if(leftMax <= rightMin(i+1)){
count = count + 1;
}
}
count
}

def main(args: Array[String]): Int{
var data: Array[Int] = Array(4, 3, 2, 1, 0)
maxChunksToSorted(arr=data)
}
}
27 changes: 27 additions & 0 deletions pepcoding/scala/L2/ArrayStrings/noOfSubArraysWithBoundedMax.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
object noOfSubArraysWithBoundedMax{
def numSubarrayBoundedMax(arr: Array[Int], left: Int, right: Int): Int = {
var prev_count=0
var overall_count=0
var i=0
var j=0

while(j < arr.size){
if(left <= arr(j) && arr(j) <= right){
prev_count = j-i+1
overall_count += prev_count
}else if(arr(j) < left){
overall_count += prev_count
}else{
prev_count=0
i=j+1
}
j+=1
}
overall_count
}

def main(args: Array[String]): Unit = {
var arr = Array(2,1,4,3)
print(numSubarrayBoundedMax(arr, 2, 3))
}
}
58 changes: 58 additions & 0 deletions pepcoding/scala/L2/ArrayStrings/reverseVowelsOfString.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
object reverseVowelsOfString{

val isVowel2 = Set('a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U')

def reverseVowels2(s: String): String = {
val a = s.toArray
@tailrec
def reverse(i: Int = 0, j: Int = a.length - 1): Unit = {
if (i < j) {
if (!isVowel2(a(i))) reverse(i+1, j)
else if (!isVowel2(a(j))) reverse(i, j-1)
else {
val tmp = a(i)
a(i) = a(j)
a(j) = tmp
reverse(i+1, j-1)
}
}
}
reverse()
a.mkString
}


def isVowel(ch: Char): Boolean = {
String vowels = "aeiouAEIOU"
return vowels.contains(ch + "")
}

def reverseVowels(s: String): String = {
var arr = s.toCharArray()
var left = 0
var right = arr.size - 1

while(left < right){

while(left < right && isVowel(arr(left) == false)){
left += 1
}
while(left < right && isVowel(arr(right) == false)){
right -= 1
}

var temp = arr(left)
arr(left) = arr(right)
arr(right) = temp

left += 1
right -=1
}
String.valueOf(arr)
}

def main(args: Array[String]): Unit = {
var s = "hello"
print(reverseVowels(s))
}
}
27 changes: 27 additions & 0 deletions pepcoding/scala/L2/ArrayStrings/wiggleSort.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
object wiggleSort{
def wiggleSort(arr: Array[Int]): Unit = {
for(i <- 0 to arr.size - 1){
if(i % 2 == 0){
if(arr(i) > arr(i+1)){
swap(arr, i, i+1)
}
}
else{
if(arr(i) < arr(i+1)){
swap(arr, i, i+1)
}
}
}
}

def swap(arr: Array[Int], i:Int, j:Int): Unit = {
var temp = arr(i)
arr(i) = arr(j)
arr(j) = temp
}

def main(args: Array[String]): Unit={
var arr = Array(3,5,2,1,6,4)
print(wiggleSort(arr))
}
}

0 comments on commit bee5a66

Please sign in to comment.