Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JEDI#2 Week3 assignment #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions week2/2D Array - DS/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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 {

// Complete the hourglassSum function below.
static int hourglassSum(int[][] arr) {
int max = Integer.MIN_VALUE;

for(int i = 0; i < 6; i++) {
for(int j = 0; j < 6; j++) {
if (i > 1 && j > 1) {
int sum = arr[i][j] +
arr[i][j - 1] +
arr[i][j - 2] +
arr[i - 1][j - 1] +
arr[i - 2][j] +
arr[i - 2][j - 1] +
arr[i - 2][j - 2];

if (sum > max) {
max = sum;
}
}
}
}
return max;
}

private static final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

int[][] arr = new int[6][6];

for (int i = 0; i < 6; i++) {
String[] arrRowItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

for (int j = 0; j < 6; j++) {
int arrItem = Integer.parseInt(arrRowItems[j]);
arr[i][j] = arrItem;
}
}

int result = hourglassSum(arr);

bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();

bufferedWriter.close();

scanner.close();
}
}
42 changes: 42 additions & 0 deletions week2/Array Manipulation/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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 final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) throws IOException {
String[] nm = scanner.nextLine().split(" ");
int n = Integer.parseInt(nm[0]);
int m = Integer.parseInt(nm[1]);

long[] arr = new long[n];
for (int i = 0; i < m; i++) {
int lower = scanner.nextInt();
int upper = scanner.nextInt();
long sum = scanner.nextInt();

arr[lower - 1] += sum;
if (upper < n) {
arr[upper] -= sum;
}
}

long max = 0, temp = 0;
for (int i = 0; i < n; i++) {
temp += arr[i];
if (temp > max) {
max = temp;
}
}

System.out.println(max);

scanner.close();
}
}
58 changes: 58 additions & 0 deletions week2/Left Rotation/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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 final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {
String[] nd = scanner.nextLine().split(" ");

int n = Integer.parseInt(nd[0]);

int d = Integer.parseInt(nd[1]);

int[] a = new int[n];

String[] aItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

for (int i = 0; i < n; i++) {
int aItem = Integer.parseInt(aItems[i]);
a[i] = aItem;
}

scanner.close();

leftRotate(a, d);

printArray(a);
}

public static void leftRotate(int[] arr, int d) {
d = d % arr.length;

int[] temp = Arrays.copyOfRange(arr, 0, d);

for (int i = 0; i < arr.length - d; i++) {
arr[i] = arr[i + d];
}

for (int i = 0; i < d; i++) {
arr[arr.length - d + i] = temp[i];
}
}

public static void printArray(int[] arr) {
for (int ele : arr) {
System.out.print(ele + " ");
}
}
}
61 changes: 61 additions & 0 deletions week2/New Year Chaos/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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 {

// Complete the minimumBribes function below.
static void minimumBribes(int[] q) {
int totalBribes = 0;

int expectedFirst = 1, expectedSecond = 2, expectedThird = 3;

for (int i = 0; i < q.length; i++) {
if (q[i] == expectedFirst) {
expectedFirst = expectedSecond;
expectedSecond = expectedThird++;
} else if (q[i] == expectedSecond) {
totalBribes++;
expectedSecond = expectedThird++;
} else if (q[i] == expectedThird) {
totalBribes += 2;
expectedThird++;
} else {
System.out.println("Too chaotic");
return;
}
}

System.out.println(totalBribes);
}

private static final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {
int t = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

for (int tItr = 0; tItr < t; tItr++) {
int n = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

int[] q = new int[n];

String[] qItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

for (int i = 0; i < n; i++) {
int qItem = Integer.parseInt(qItems[i]);
q[i] = qItem;
}

minimumBribes(q);
}

scanner.close();
}
}
45 changes: 45 additions & 0 deletions week2/The Power Sum/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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 {

// Complete the powerSum function below.
static int powerSum(int X, int N) {
return powerSumHelper(X, N, 1);
}

private static int powerSumHelper(int X, int N, int num) {
int power = (int) Math.pow(num, N);
if (power > X) return 0;
if (power == X) return 1;

return powerSumHelper(X, N, num + 1) +
powerSumHelper(X - power, N, num + 1);
}

private static final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

int X = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

int N = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

int result = powerSum(X, N);

bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();

bufferedWriter.close();

scanner.close();
}
}
Loading