Skip to content

Commit 361dde6

Browse files
Merge branch 'kunal-kushwaha:main' into pattern
2 parents 57107c2 + 03e0ccd commit 361dde6

19 files changed

+341
-2
lines changed

assignments/conditionals-loops.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
16. Volume Of Cylinder
2121
17. Volume Of Sphere
2222
18. Volume Of Pyramid
23-
19. Curved Surface Area Of Cube
24-
20. Total Surface Area Of Cylinder
23+
19. Curved Surface Area Of Cylinder
24+
20. Total Surface Area Of Cube
2525
21. Fibonacci Series In Java Programs
2626
22. [Subtract the Product and Sum of Digits of an Integer](https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/)
2727

assignments/searching.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Videos:
2+
- [Linear Search](https://youtu.be/_HRA37X8N_Q)
3+
4+
Problems:
5+
- [Linear Search](https://www.hackerearth.com/practice/algorithms/searching/linear-search/practice-problems/)
Binary file not shown.
979 KB
Binary file not shown.

lectures/9-linear search/code/.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lectures/9-linear search/code/.idea/description.html

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lectures/9-linear search/code/.idea/encodings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lectures/9-linear search/code/.idea/misc.xml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lectures/9-linear search/code/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lectures/9-linear search/code/.idea/project-template.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lectures/9-linear search/code/.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.kunal;
2+
// https://leetcode.com/problems/find-numbers-with-even-number-of-digits/
3+
public class EvenDigits {
4+
public static void main(String[] args) {
5+
int[] nums = {12,345,2,6,7896};
6+
// System.out.println(findNumbers(nums));
7+
8+
System.out.println(digits2(-345678));
9+
}
10+
static int findNumbers(int[] nums) {
11+
int count = 0;
12+
for(int num : nums) {
13+
if (even(num)) {
14+
count++;
15+
}
16+
}
17+
return count;
18+
}
19+
20+
// function to check whether a number contains even digits or not
21+
static boolean even(int num) {
22+
int numberOfDigits = digits(num);
23+
/*
24+
if (numberOfDigits % 2 == 0) {
25+
return true;
26+
}
27+
return false;
28+
*/
29+
return numberOfDigits % 2 == 0;
30+
}
31+
32+
static int digits2(int num) {
33+
if (num < 0) {
34+
num = num * -1;
35+
}
36+
return (int)(Math.log10(num)) + 1;
37+
}
38+
39+
// count number of digits in a number
40+
static int digits(int num) {
41+
42+
if (num < 0) {
43+
num = num * -1;
44+
}
45+
46+
if (num == 0) {
47+
return 1;
48+
}
49+
50+
int count = 0;
51+
while (num > 0) {
52+
count++;
53+
num = num / 10; // num /= 10
54+
}
55+
56+
return count;
57+
}
58+
59+
60+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.kunal;
2+
3+
public class FindMin {
4+
public static void main(String[] args) {
5+
int[] arr = {18, 12, 7, 3, 14, 28};
6+
System.out.println(min(arr));
7+
}
8+
9+
// assume arr.length != 0
10+
// return the minimum value in the array
11+
static int min(int[] arr) {
12+
int ans = arr[0];
13+
for (int i = 1; i < arr.length; i++) {
14+
if (arr[i] < ans) {
15+
ans = arr[i];
16+
}
17+
}
18+
return ans;
19+
}
20+
21+
22+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.kunal;
2+
3+
public class Main {
4+
5+
public static void main(String[] args) {
6+
int[] nums = {23, 45, 1, 2, 8, 19, -3, 16, -11, 28};
7+
int target = 19;
8+
int ans = linearSearch(nums, target);
9+
System.out.println(ans);
10+
}
11+
12+
// search the target and return true or false
13+
static boolean linearSearch3(int[] arr, int target) {
14+
if (arr.length == 0) {
15+
return false;
16+
}
17+
18+
// run a for loop
19+
for (int element : arr) {
20+
if (element == target) {
21+
return true;
22+
}
23+
}
24+
// this line will execute if none of the return statements above have executed
25+
// hence the target not found
26+
return false;
27+
}
28+
29+
// search the target and return the element
30+
static int linearSearch2(int[] arr, int target) {
31+
if (arr.length == 0) {
32+
return -1;
33+
}
34+
35+
// run a for loop
36+
for (int element : arr) {
37+
if (element == target) {
38+
return element;
39+
}
40+
}
41+
// this line will execute if none of the return statements above have executed
42+
// hence the target not found
43+
return Integer.MAX_VALUE;
44+
}
45+
46+
// search in the array: return the index if item found
47+
// otherwise if item not found return -1
48+
static int linearSearch(int[] arr, int target) {
49+
if (arr.length == 0) {
50+
return -1;
51+
}
52+
53+
// run a for loop
54+
for (int index = 0; index < arr.length; index++) {
55+
// check for element at every index if it is = target
56+
int element = arr[index];
57+
if (element == target) {
58+
return index;
59+
}
60+
}
61+
// this line will execute if none of the return statements above have executed
62+
// hence the target not found
63+
return -1;
64+
}
65+
66+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.kunal;
2+
// https://leetcode.com/problems/richest-customer-wealth/
3+
public class MaxWealth {
4+
public static void main(String[] args) {
5+
6+
}
7+
public int maximumWealth(int[][] accounts) {
8+
// person = rol
9+
// account = col
10+
int ans = Integer.MIN_VALUE;
11+
for (int[] ints : accounts) {
12+
// when you start a new col, take a new sum for that row
13+
int sum = 0;
14+
for (int anInt : ints) {
15+
sum += anInt;
16+
}
17+
// now we have sum of accounts of person
18+
// check with overall ans
19+
if (sum > ans) {
20+
ans = sum;
21+
}
22+
}
23+
return ans;
24+
}
25+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.kunal;
2+
3+
import java.util.Arrays;
4+
5+
public class SearchIn2DArray {
6+
public static void main(String[] args) {
7+
int[][] arr = {
8+
{23, 4, 1},
9+
{18, 12, 3, 9},
10+
{78, 99, 34, 56},
11+
{18, 12}
12+
};
13+
int target = 56;
14+
int[] ans = search(arr,target); // format of return value {row, col}
15+
System.out.println(Arrays.toString(ans));
16+
17+
System.out.println(max(arr));
18+
19+
System.out.println(Integer.MIN_VALUE);
20+
}
21+
22+
static int[] search(int[][] arr, int target) {
23+
for (int row = 0; row < arr.length; row++) {
24+
for (int col = 0; col < arr[row].length; col++) {
25+
if (arr[row][col] == target) {
26+
return new int[]{row, col};
27+
}
28+
}
29+
}
30+
return new int[]{-1, -1};
31+
}
32+
33+
static int max(int[][] arr) {
34+
int max = Integer.MIN_VALUE;
35+
for (int[] ints : arr) {
36+
for (int element : ints) {
37+
if (element > max) {
38+
max = element;
39+
}
40+
}
41+
}
42+
return max;
43+
}
44+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.kunal;
2+
3+
public class SearchInRange {
4+
public static void main(String[] args) {
5+
int[] arr = {18, 12, -7, 3, 14, 28};
6+
int target = 3456;
7+
System.out.println(linearSearch(arr, target, 1, 4));
8+
}
9+
10+
static int linearSearch(int[] arr, int target, int start, int end) {
11+
if (arr.length == 0) {
12+
return -1;
13+
}
14+
15+
// run a for loop
16+
for (int index = start; index <= end; index++) {
17+
// check for element at every index if it is = target
18+
int element = arr[index];
19+
if (element == target) {
20+
return index;
21+
}
22+
}
23+
// this line will execute if none of the return statements above have executed
24+
// hence the target not found
25+
return -1;
26+
}
27+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.kunal;
2+
3+
import java.util.Arrays;
4+
5+
public class SearchInStrings {
6+
public static void main(String[] args) {
7+
String name = "Kunal";
8+
char target = 'u';
9+
// System.out.println(search(name, target));
10+
11+
System.out.println(Arrays.toString(name.toCharArray()));
12+
}
13+
14+
15+
static boolean search2(String str, char target) {
16+
if (str.length() == 0) {
17+
return false;
18+
}
19+
20+
for(char ch : str.toCharArray()) {
21+
if (ch == target) {
22+
return true;
23+
}
24+
}
25+
return false;
26+
}
27+
28+
static boolean search(String str, char target) {
29+
if (str.length() == 0) {
30+
return false;
31+
}
32+
33+
for (int i = 0; i < str.length(); i++) {
34+
if (target == str.charAt(i)) {
35+
return true;
36+
}
37+
}
38+
return false;
39+
}
40+
}

0 commit comments

Comments
 (0)