├── .gitignore
├── Day3.java
├── README.md
├── Day5.java
├── Day4.java
├── Day7.java
├── Day8.java
├── Day6.java
├── Day11.java
├── Day15.java
├── Day13.java
├── Day14.java
├── Day2.java
├── Day17.java
├── Day9.java
├── Day16.java
├── Day1.java
├── Day12.java
├── Day10.java
├── Day20.java
├── Day18.java
├── Day19.java
└── Day21.java
/.gitignore:
--------------------------------------------------------------------------------
1 | *.class
2 |
--------------------------------------------------------------------------------
/Day3.java:
--------------------------------------------------------------------------------
1 | /* Number of digits :
2 |
3 | Problem statement ==>
4 | You are given a number 'n'.
5 | Return number of digits in ‘n’.
6 |
7 | Example:
8 | Input: 'n' = 123
9 | Output: 3
10 | Explanation:
11 | The 3 digits in ‘123’ are 1, 2 and 3.
12 | */
13 |
14 |
15 | public class Day3 {
16 | public static int countDigits(int n){
17 |
18 | return (int) Math.log10(n) + 1;
19 | }
20 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # NINJA-SLAYGROUND-2.0-21-Day-Coding-Challenge :
2 |
3 | This is a dynamic coding event aimed at sharpening programming skills through daily tasks. Over the course of 21 days, participants engage in challenges that enhance their coding proficiency and problem-solving techniques.
4 |
--------------------------------------------------------------------------------
/Day5.java:
--------------------------------------------------------------------------------
1 | /* Check Sorted Array:
2 |
3 | Problem statement ==>
4 | You have been given an array ‘a’ of ‘n’ non-negative integers.You have to check whether the given array is sorted in the non-decreasing order or not.
5 | Your task is to return 1 if the given array is sorted. Else, return 0.
6 |
7 | Example :
8 | Input: ‘n’ = 5, ‘a’ = [1, 2, 3, 4, 5]
9 | Output: 1
10 | The given array is sorted in non-decreasing order; hence the answer will be 1.
11 | */
12 |
13 |
14 | public class Day5 {
15 | public static int isSorted(int n, int []a) {
16 |
17 | for(int i=1; i a[i]){
19 | return 0;
20 | }
21 | }
22 | return 1;
23 | }
24 | }
--------------------------------------------------------------------------------
/Day4.java:
--------------------------------------------------------------------------------
1 | /* Prime Number :
2 |
3 | Problem statement ==>
4 | A prime number is a positive integer that is divisible by exactly 2 integers, 1 and the number itself.
5 | You are given a number 'n'.
6 | Find out whether 'n' is prime or not.
7 |
8 | Example :
9 | Input: 'n' = 5
10 | Output: YES
11 | Explanation: 5 is only divisible by 1 and 5. 2, 3 and 4 do not divide 5.
12 | */
13 |
14 |
15 | public class Day4 {
16 | public static boolean isPrime(int n) {
17 |
18 | if(n<=1) return false;
19 | if(n == 2) return true;
20 |
21 | for(int i=2; i*i <= n; i++){
22 | if(n%i == 0){
23 | return false;
24 | }
25 | }
26 | return true;
27 | }
28 | }
--------------------------------------------------------------------------------
/Day7.java:
--------------------------------------------------------------------------------
1 | /* Reverse an Array :
2 |
3 | Problem statement ==>
4 | Given an array 'arr' of size 'n'.
5 | Return an array with all the elements placed in reverse order.
6 |
7 | Note:
8 | You don’t need to print anything. Just implement the given function.
9 | Example:
10 | Input: n = 6, arr = [5, 7, 8, 1, 6, 3]
11 | Output: [3, 6, 1, 8, 7, 5]
12 | Explanation: After reversing the array, it looks like this [3, 6, 1, 8, 7, 5].
13 | */
14 |
15 | public class Day7 {
16 | public static int[] reverseArray(int n, int []nums) {
17 |
18 | int[] reverseArray = new int[n];
19 | for(int i=0;i
4 | You are given two integers 'n', and 'm'.
5 | Calculate 'gcd(n,m)', without using library functions.
6 |
7 | Note:
8 | The greatest common divisor (gcd) of two numbers 'n' and 'm' is the largest positive number that divides both 'n' and 'm' without leaving a remainder.
9 | Example:
10 | Input: 'n' = 6, 'm' = 4
11 | Output: 2
12 | Explanation:
13 | Here, gcd(4,6) = 2, because 2 is the largest positive integer that divides both 4 and 6.
14 | */
15 |
16 | public class Day8 {
17 | public static int calcGCD(int n, int m){
18 |
19 | while (m != 0) {
20 | int temp = m;
21 | m = n % m;
22 | n = temp;
23 | }
24 | return n;
25 | }
26 | }
--------------------------------------------------------------------------------
/Day6.java:
--------------------------------------------------------------------------------
1 | /* Reverse a Number :
2 |
3 | Problem statement ==>
4 | You are given a number 'n'.
5 | Return an integer that is the reverse of ‘n’.
6 | Note:
7 | Reverse of ‘n’ means an integer where, the most significant digit of ‘n’ is the least significant digit of the number, the second most significant digit of ‘n’ is the second least significant digit of the number and so on.
8 |
9 | Example:
10 | Input: 'n' = 123
11 | Output: 321
12 | Explanation:
13 | Reverse of 'n' = 123 is 321.
14 | */
15 |
16 |
17 | public class Day6 {
18 | public static int reverseNumber(int n) {
19 | int reversed =0;
20 |
21 | while(n>0){
22 | int digit = n%10;
23 | reversed = reversed*10 + digit;
24 | n=n/10;
25 | }
26 | return reversed;
27 | }
28 | }
--------------------------------------------------------------------------------
/Day11.java:
--------------------------------------------------------------------------------
1 | /* Two Sum :
2 |
3 | Problem statement ==>
4 | Sam want to read exactly ‘TARGET’ number of pages.
5 | He has an array ‘BOOK’ containing the number of pages for ‘N’ books.
6 | Return YES/NO, if it is possible for him to read any 2 books and he can meet his ‘TARGET’ number of pages.
7 |
8 | Example:
9 | Input: ‘N’ = 5, ‘TARGET’ = 5
10 | ‘BOOK’ = [4, 1, 2, 3, 1]
11 | Output: YES
12 | Explanation:
13 | Sam can buy 4 pages book and 1 page book
14 | */
15 |
16 | import java.util.HashSet;
17 | public class Day11 {
18 | public static String read(int n, int []book, int target){
19 |
20 | HashSet set =new HashSet<>();
21 |
22 | for(int pages:book)
23 | {
24 | int diff = target - pages;
25 | if(set.contains(diff)){
26 | return "YES";
27 | }
28 | set.add(pages);
29 | }
30 | return "NO";
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/Day15.java:
--------------------------------------------------------------------------------
1 | /* Implement Lower Bound:
2 |
3 | Problem statement==>
4 | You are given an array 'arr' sorted in non-decreasing order and a number 'x'. You must return the index of the lower bound of 'x'.
5 |
6 | Note:
7 | 1. For a sorted array 'arr', 'lower_bound' of a number 'x' is defined as the smallest index 'idx' such that the value 'arr[idx]' is not less than 'x'.If all numbers are smaller than 'x', then 'n' should be the 'lower_bound' of 'x', where 'n' is the size of array.
8 | 2. Try to do this in O(log(n)).
9 |
10 | Example:
11 | Input: ‘arr’ = [1, 2, 2, 3] and 'x' = 0
12 | Output: 0
13 | */
14 |
15 |
16 | public class Day15 {
17 | public static int lowerBound(int []arr, int n, int x) {
18 |
19 | int low=0, high=n;
20 |
21 | while(low
4 | You are given an integer array 'A' of size 'N', sorted in non-decreasing order. You are also given an integer 'target'. Your task is to write a function to search for 'target' in the array 'A'. If it exists, return its index in 0-based indexing. If 'target' is not present in the array 'A', return -1.
5 |
6 | Note:
7 | You must write an algorithm whose time complexity is O(LogN)
8 | */
9 |
10 | public class Day13 {
11 | public static int search(int []nums, int target) {
12 | // Write your code here.
13 | int left = 0;
14 | int right = nums.length - 1;
15 |
16 | while (left <= right) {
17 | int mid = left + (right - left) / 2;
18 |
19 | if (nums[mid] == target) {
20 | return mid;
21 | } else if (nums[mid] > target) {
22 | right = mid - 1;
23 | } else {
24 | left = mid + 1;
25 | }
26 | }
27 |
28 | return -1;
29 | }
30 | }
--------------------------------------------------------------------------------
/Day14.java:
--------------------------------------------------------------------------------
1 | /* Sort An Array of 0s, 1s and 2s:
2 |
3 | Problem statement==>
4 | You have been given an array/list 'arr' consisting of 'n' elements.
5 | Each element in the array is either 0, 1 or 2.
6 | Sort this array/list in increasing order.
7 | Do not make a new array/list. Make changes in the given array/list.
8 |
9 | Example :
10 | Input: 'arr' = [2, 2, 2, 2, 0, 0, 1, 0]
11 | Output: Final 'arr' = [0, 0, 0, 1, 2, 2, 2, 2]
12 | */
13 |
14 | import java.util.* ;
15 | import java.io.*;
16 | public class Day14 {
17 | public static void sortArray(ArrayList arr, int n) {
18 | // Write your code here.
19 | int low = 0, mid = 0, high = n - 1;
20 |
21 | while (mid <= high) {
22 | if (arr.get(mid) == 0) {
23 | Collections.swap(arr, low, mid);
24 | low++;
25 | mid++;
26 | } else if (arr.get(mid) == 1) {
27 | mid++;
28 | } else {
29 | Collections.swap(arr, mid, high);
30 | high--;
31 | }
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/Day2.java:
--------------------------------------------------------------------------------
1 | /* Nth Fibonacci Number :
2 |
3 | Problem statement==>
4 | The n-th term of Fibonacci series F(n), where F(n) is a function, is calculated using the following formula -
5 | F(n) = F(n - 1) + F(n - 2),
6 | Where, F(1) = 1, F(2) = 1
7 | Provided 'n' you have to find out the n-th Fibonacci Number. Handle edges cases like when 'n' = 1 or 'n' = 2 by using conditionals like if else and return what's expected.
8 | "Indexing is start from 1"
9 | */
10 |
11 | import java.util.Scanner;
12 | public class Day2 {
13 | public static void main(String[] args) {
14 |
15 | Scanner scanner = new Scanner(System.in);
16 | int n = scanner.nextInt();
17 | scanner.close();
18 |
19 | if (n == 1 || n == 2) {
20 | System.out.println(1);
21 | return;
22 | }
23 |
24 | int prev1 = 1, prev2 = 1;
25 | int current = 0;
26 |
27 | for (int i = 3; i <= n; i++) {
28 | current = prev1 + prev2;
29 | prev1 = prev2;
30 | prev2 = current;
31 | }
32 | System.out.println(current);
33 | }
34 | }
--------------------------------------------------------------------------------
/Day17.java:
--------------------------------------------------------------------------------
1 | /* Implement Upper Bound:
2 |
3 | Problem statement==>
4 | You are given a sorted array ‘arr’ containing ‘n’ integers and an integer ‘x’.Implement the ‘upper bound’ function to find the index of the upper bound of 'x' in the array.
5 |
6 | Note:
7 | 1. The upper bound in a sorted array is the index of the first value that is greater than a given value.
8 | 2. If the greater value does not exist then the answer is 'n', Where 'n' is the size of the array.
9 | 3. Try to write a solution that runs in log(n) time complexity.
10 |
11 | Example:
12 | Input : ‘arr’ = {2,4,6,7} and ‘x’ = 5,
13 | Output: 2
14 | Explanation: The upper bound of 5 is 6 in the given array, which is at index 2 (0-indexed).
15 | */
16 |
17 |
18 | public class Day17 {
19 | public static int upperBound(int []arr, int x, int n){
20 | // Write your code here.
21 | int low=0;
22 | int high=n;
23 | while(low x){
26 | high=mid;
27 | }else{
28 | low=mid +1;
29 | }
30 | }
31 | return low;
32 | }
33 | }
--------------------------------------------------------------------------------
/Day9.java:
--------------------------------------------------------------------------------
1 | /* Second Largest Number :
2 |
3 | Problem statement ==>
4 | You have been given an array ‘a’ of ‘n’ unique non-negative integers.
5 | Find the second largest and second smallest element from the array.
6 | Return the two elements (second largest and second smallest) as another array of size 2
7 |
8 | Example :
9 | Input: ‘n’ = 5, ‘a’ = [1, 2, 3, 4, 5]
10 | Output: [4, 2]
11 | The second largest element after 5 is 4, and the second smallest element after 1 is 2.
12 | */
13 |
14 |
15 | public class Day9 {
16 | public static int[] getSecondOrderElements(int n, int[] a) {
17 | int largest = Integer.MIN_VALUE, secondLargest = Integer.MIN_VALUE;
18 | int smallest = Integer.MAX_VALUE, secondSmallest = Integer.MAX_VALUE;
19 |
20 | for (int num : a) {
21 | if (num > largest) largest = num;
22 | if (num < smallest) smallest = num;
23 | }
24 |
25 | for (int num : a) {
26 | if (num > secondLargest && num < largest) secondLargest = num;
27 | if (num < secondSmallest && num > smallest) secondSmallest = num;
28 | }
29 |
30 | return new int[] {secondLargest, secondSmallest};
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/Day16.java:
--------------------------------------------------------------------------------
1 | /* Valid Parentheses:
2 |
3 | Problem statement==>
4 | You're given a string 'S' consisting of "{", "}", "(", ")", "[" and "]" .
5 | Return true if the given string 'S' is balanced, else return false.
6 |
7 | For example:
8 | 'S' = "{}()".
9 | There is always an opening brace before a closing brace i.e. '{' before '}', '(' before ').
10 | So the 'S' is Balanced.
11 | */
12 |
13 |
14 | import java.util.Stack;
15 |
16 | public class Day16 {
17 | public static boolean isValidParenthesis(String s) {
18 | Stack stack = new Stack<>();
19 |
20 | for (char ch : s.toCharArray()) {
21 | if (ch == '(' || ch == '{' || ch == '[') {
22 | stack.push(ch);
23 | } else if (ch == ')' || ch == '}' || ch == ']') {
24 | if (stack.isEmpty()) return false;
25 |
26 | char top = stack.pop();
27 | if ((ch == ')' && top != '(') ||
28 | (ch == '}' && top != '{') ||
29 | (ch == ']' && top != '[')) {
30 | return false;
31 | }
32 | }
33 | }
34 | return stack.isEmpty();
35 | }
36 | }
37 |
38 |
--------------------------------------------------------------------------------
/Day1.java:
--------------------------------------------------------------------------------
1 | /* Switch Case statement:
2 |
3 | Problem statement ==>
4 | Programming languages have some conditional / decision-making statements that execute when some specific condition is fulfilled.
5 | Switch-case is one of the ways to implement them.
6 | In a menu-driven program, the user is given a set of choices of things to do (the menu) and then is asked to select a menu item.
7 | There are 2 choices in the menu:
8 | Choice 1 is to find the area of a circle having radius 'r'.
9 | Choice 2 is to find the area of a rectangle having dimensions 'l' and 'b'.
10 | You are given the choice 'ch' and an array 'a'.
11 | If ‘ch’ is 1, ‘a’ contains a single number ‘r’. If ‘ch’ is 2, ‘a’ contains 2 numbers, ‘l’ and ‘b’.
12 | Consider the choice and print the appropriate area.
13 | */
14 |
15 |
16 | public class Day1 {
17 | public static double areaSwitchCase(int ch, double []a) {
18 |
19 | double area = 0.0;
20 | switch (ch) {
21 | case 1:
22 | if (a.length == 1) {
23 | double r = a[0];
24 | area = Math.PI * r * r;
25 | }
26 | break;
27 | case 2:
28 | if (a.length == 2) {
29 | double l = a[0];
30 | double b = a[1];
31 | area = l * b;
32 | }
33 | break;
34 | }
35 | return Math.round(area * 100000.0) / 100000.0;
36 | }
37 | }
--------------------------------------------------------------------------------
/Day12.java:
--------------------------------------------------------------------------------
1 | /* Add One To Number:
2 |
3 | Problem statement ==>
4 | Given a non-negative number represented as an array of digits, you have to add 1 to the number, i.e, increment the given number by one.
5 | The digits are stored such that the most significant digit is at the starting of the array and the least significant digit is at the end of the array.
6 |
7 | Example:
8 | If the given array is {1,5,2}, the returned array should be {1,5,3}.
9 | Note:
10 | Input array can contain leading zeros, but the output array should not contain any leading zeros (even if the input array contains leading zeroes).
11 | For Example:
12 | If the given array is {0,2}, the returned array should be {3}.
13 |
14 | */
15 |
16 |
17 | import java.util.* ;
18 | import java.io.*;
19 | public class Day12
20 | {
21 | public static ArrayList addOneToNumber(ArrayList arr)
22 | {
23 | int n = arr.size();
24 | int carry = 1;
25 | ArrayList result = new ArrayList<>();
26 |
27 | for (int i = n - 1; i >= 0; i--) {
28 | int sum = arr.get(i) + carry;
29 | result.add(sum % 10);
30 | carry = sum / 10;
31 | }
32 |
33 | if(carry > 0 ){
34 | result.add(carry);
35 | }
36 | Collections.reverse(result);
37 | while (result.size() > 1 && result.get(0) == 0) {
38 | result.remove(0);
39 | }
40 | return result;
41 | }
42 | }
--------------------------------------------------------------------------------
/Day10.java:
--------------------------------------------------------------------------------
1 | /* Highest / Lowest Frequency Elements:
2 |
3 | Problem statement ==>
4 | Given an array 'v' of 'n' numbers.
5 | Your task is to find and return the highest and lowest frequency elements.
6 | If there are multiple elements that have the highest frequency or lowest frequency, pick the smallest element.
7 |
8 | Example:
9 | Input: ‘n' = 6, 'v' = [1, 2, 3, 1, 1, 4]
10 | Output: 1 2
11 | */
12 |
13 |
14 | import java.util.*;
15 |
16 | public class Day10 {
17 | public static int[] getFrequencies(int[] v) {
18 | HashMap freqMap = new HashMap<>();
19 | for (int num : v) {
20 | freqMap.put(num, freqMap.getOrDefault(num, 0) + 1);
21 | }
22 |
23 | int maxFreq = Integer.MIN_VALUE, minFreq = Integer.MAX_VALUE;
24 | int maxFreqElement = Integer.MAX_VALUE, minFreqElement = Integer.MAX_VALUE;
25 |
26 | for (Map.Entry entry : freqMap.entrySet()) {
27 | int element = entry.getKey();
28 | int frequency = entry.getValue();
29 |
30 | if (frequency > maxFreq || (frequency == maxFreq && element < maxFreqElement)) {
31 | maxFreq = frequency;
32 | maxFreqElement = element;
33 | }
34 |
35 | if (frequency < minFreq || (frequency == minFreq && element < minFreqElement)) {
36 | minFreq = frequency;
37 | minFreqElement = element;
38 | }
39 | }
40 |
41 | return new int[]{maxFreqElement, minFreqElement};
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/Day20.java:
--------------------------------------------------------------------------------
1 | /* Pascal's Triangle :
2 |
3 | Problem statement==>
4 | You are given an integer N. Your task is to return a 2-D ArrayList containing the pascal’s triangle till the row N.
5 | A Pascal's triangle is a triangular array constructed by summing adjacent elements in preceding rows. Pascal's triangle contains the values of the binomial coefficient.
6 |
7 | For example, given integer N= 4 then you have to print.
8 | 1
9 | 1 1
10 | 1 2 1
11 | 1 3 3 1
12 |
13 | Here for the third row, you will see that the second element is the summation of the above two-row elements i.e. 2=1+1, and similarly for row three 3 = 1+2 and 3 = 1+2.
14 | */
15 |
16 | import java.io.*;
17 | import java.util.* ;
18 |
19 | import java.util.ArrayList;
20 |
21 | public class Day20 {
22 | public static ArrayList> printPascal(int n) {
23 | // Write your code here.
24 | ArrayList> pascalTriangle = new ArrayList<>();
25 | for (int i = 0; i < n; i++) {
26 | ArrayList row = new ArrayList<>();
27 | for (int j = 0; j <= i; j++) {
28 | if (j == 0 || j == i) {
29 | row.add(1L);
30 | } else {
31 | long value = pascalTriangle.get(i - 1).get(j - 1) + pascalTriangle.get(i - 1).get(j);
32 | row.add(value);
33 | }
34 | }
35 | pascalTriangle.add(row);
36 | }
37 | return pascalTriangle;
38 | }
39 |
40 | public static void main(String[] args) {
41 | int n = 5;
42 | ArrayList> result = printPascal(n);
43 | for (ArrayList row : result) {
44 | for (Long num : row) {
45 | System.out.print(num + " ");
46 | }
47 | System.out.println();
48 | }
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/Day18.java:
--------------------------------------------------------------------------------
1 | /* Spiral Matrix :
2 |
3 | Problem statement==>
4 | You are given a 2D matrix ‘MATRIX’ of ‘N’*’M’ dimension. You have to return the spiral traversal of the matrix.
5 |
6 | Example:
7 | Input:
8 | MATRIX = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 60] ]
9 | Output:
10 | 1 3 5 7 20 60 34 30 23 10 11 16
11 | Explanation: Starting from the element in the first row and the first column, traverse from left to right (1 3 5 7), then top to bottom (20 60), then right to left (34 30 23), then bottom to up (10) and then left to right (11 16).
12 | */
13 |
14 |
15 | import java.util.*;
16 | public class Day18 {
17 | public static int[] spiralMatrix(int [][]MATRIX) {
18 | // Write your code here.
19 | int n = MATRIX.length;
20 | int m = MATRIX[0].length;
21 |
22 | int[] result = new int[n * m];
23 | int index = 0;
24 |
25 | int top = 0, bottom = n - 1, left = 0, right = m - 1;
26 |
27 | while (top <= bottom && left <= right) {
28 | for (int i = left; i <= right; i++) {
29 | result[index++] = MATRIX[top][i];
30 | }
31 | top++;
32 |
33 | for (int i = top; i <= bottom; i++) {
34 | result[index++] = MATRIX[i][right];
35 | }
36 | right--;
37 |
38 | if (top <= bottom) {
39 | for (int i = right; i >= left; i--) {
40 | result[index++] = MATRIX[bottom][i];
41 | }
42 | bottom--;
43 | }
44 |
45 | if (left <= right) {
46 | for (int i = bottom; i >= top; i--) {
47 | result[index++] = MATRIX[i][left];
48 | }
49 | left++;
50 | }
51 | }
52 |
53 | return result;
54 | }
55 | }
--------------------------------------------------------------------------------
/Day19.java:
--------------------------------------------------------------------------------
1 | /* Zero Matrix:
2 |
3 | Problem statement==>
4 | You are given a matrix 'MATRIX' of dimension 'N' x 'M'. Your task is to make all the elements of row 'i' and column 'j' equal to 0 if any element in the ith row or jth column of the matrix is 0.
5 | Note:
6 | 1) The number of rows should be at least 1.
7 | 2) The number of columns should be at least 1.
8 | */
9 |
10 |
11 | import java.util.* ;
12 | import java.io.*;
13 |
14 | public class Day19 {
15 | public static ArrayList> zeroMatrix(ArrayList> matrix, Integer n, Integer m) {
16 | boolean firstRowHasZero = false;
17 | boolean firstColHasZero = false;
18 |
19 | for (int i = 0; i < n; i++) {
20 | if (matrix.get(i).get(0) == 0) {
21 | firstColHasZero = true;
22 | break;
23 | }
24 | }
25 |
26 | for (int j = 0; j < m; j++) {
27 | if (matrix.get(0).get(j) == 0) {
28 | firstRowHasZero = true;
29 | break;
30 | }
31 | }
32 |
33 | for (int i = 1; i < n; i++) {
34 | for (int j = 1; j < m; j++) {
35 | if (matrix.get(i).get(j) == 0) {
36 | matrix.get(i).set(0, 0);
37 | matrix.get(0).set(j, 0);
38 | }
39 | }
40 | }
41 |
42 | for (int i = 1; i < n; i++) {
43 | if (matrix.get(i).get(0) == 0) {
44 | for (int j = 1; j < m; j++) {
45 | matrix.get(i).set(j, 0);
46 | }
47 | }
48 | }
49 |
50 | for (int j = 1; j < m; j++) {
51 | if (matrix.get(0).get(j) == 0) {
52 | for (int i = 1; i < n; i++) {
53 | matrix.get(i).set(j, 0);
54 | }
55 | }
56 | }
57 |
58 | if (firstRowHasZero) {
59 | for (int j = 0; j < m; j++) {
60 | matrix.get(0).set(j, 0);
61 | }
62 | }
63 |
64 | if (firstColHasZero) {
65 | for (int i = 0; i < n; i++) {
66 | matrix.get(i).set(0, 0);
67 | }
68 | }
69 |
70 | return matrix;
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/Day21.java:
--------------------------------------------------------------------------------
1 | /* Number of Inversions:
2 |
3 | Problem statement==>
4 | There is an integer array ‘A’ of size ‘N’.
5 | Number of inversions in an array can be defined as the number of pairs of ‘i’, ‘j’ such that ‘i’ < ‘j’ and ‘A[i]’ > ‘A[j]’.
6 | You must return the number of inversions in the array.
7 |
8 | For example,
9 | Input:
10 | A = [5, 3, 2, 1, 4], N = 5
11 | Output:
12 | 7
13 | */
14 |
15 | public class Day21 {
16 | public static int numberOfInversions(int[] a, int n) {
17 | return mergeSortAndCount(a, 0, n - 1);
18 | }
19 |
20 | private static int mergeSortAndCount(int[] arr, int left, int right) {
21 | int count = 0;
22 | if (left < right) {
23 | int mid = left + (right - left) / 2;
24 |
25 | // Count inversions in the left half
26 | count += mergeSortAndCount(arr, left, mid);
27 |
28 | // Count inversions in the right half
29 | count += mergeSortAndCount(arr, mid + 1, right);
30 |
31 | // Count and merge inversions across the two halves
32 | count += mergeAndCount(arr, left, mid, right);
33 | }
34 | return count;
35 | }
36 |
37 | private static int mergeAndCount(int[] arr, int left, int mid, int right) {
38 | int[] leftArray = new int[mid - left + 1];
39 | int[] rightArray = new int[right - mid];
40 |
41 | for (int i = 0; i < leftArray.length; i++)
42 | leftArray[i] = arr[left + i];
43 | for (int i = 0; i < rightArray.length; i++)
44 | rightArray[i] = arr[mid + 1 + i];
45 |
46 | int i = 0, j = 0, k = left, inversions = 0;
47 |
48 | while (i < leftArray.length && j < rightArray.length) {
49 | if (leftArray[i] <= rightArray[j]) {
50 | arr[k++] = leftArray[i++];
51 | } else {
52 | arr[k++] = rightArray[j++];
53 | // All remaining elements in leftArray form an inversion
54 | inversions += (leftArray.length - i);
55 | }
56 | }
57 |
58 | // Copy remaining elements of leftArray (if any)
59 | while (i < leftArray.length)
60 | arr[k++] = leftArray[i++];
61 |
62 | // Copy remaining elements of rightArray (if any)
63 | while (j < rightArray.length)
64 | arr[k++] = rightArray[j++];
65 |
66 | return inversions;
67 | }
68 | }
69 |
--------------------------------------------------------------------------------