Skip to content

Commit 26d2d57

Browse files
committed
this is repo to be commited
1 parent 877cdc3 commit 26d2d57

44 files changed

Lines changed: 1380 additions & 35 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package arrays;
2+
3+
public class Maximum_subarray_problem {
4+
// Java program to print largest
5+
// contiguous array sum
6+
7+
static void maxSubArraySum(int a[], int size)
8+
{
9+
int max_so_far = Integer.MIN_VALUE,
10+
max_ending_here = 0,start = 0,
11+
end = 0, s = 0;
12+
13+
for (int i = 0; i < size; i++)
14+
{
15+
max_ending_here += a[i];
16+
17+
if (max_so_far < max_ending_here)
18+
{
19+
max_so_far = max_ending_here;
20+
start = s;
21+
end = i;
22+
}
23+
24+
if (max_ending_here < 0)
25+
{
26+
max_ending_here = 0;
27+
s = i + 1;
28+
}
29+
}
30+
System.out.println("Maximum contiguous sum is "
31+
+ max_so_far);
32+
System.out.println("Starting index " + start);
33+
System.out.println("Ending index " + end);
34+
}
35+
36+
// Driver code
37+
public static void main(String[] args)
38+
{
39+
int a[] = { -2, -3, 4, -1, -2, 1, 5, -3 };
40+
int n = a.length;
41+
maxSubArraySum(a, n);
42+
}
43+
}
44+

arrays/RotateArray.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package arrays;
2+
3+
public class RotateArray {
4+
5+
//function to rotate the elements of an array
6+
7+
void rotateL(int a[], int d, int n) {
8+
for(int i= 0;i<d;i++)
9+
rotateLByOne(a,n);
10+
}
11+
12+
// rotate the array by left by one
13+
14+
void rotateLByOne(int a[],int n) {
15+
int i,temp;
16+
temp = a[0];
17+
for(i =0;i<n-1;i++)
18+
a[i] = a[i+1];
19+
a[i] = temp;
20+
}
21+
22+
// a utility function to display the elements of array
23+
24+
void printArray(int a[],int n) {
25+
for(int i=0;i<n;i++)
26+
System.out.print(a[i]+" ");
27+
}
28+
29+
// driver method of the program
30+
public static void main(String[] args) {
31+
32+
RotateArray rotate = new RotateArray();
33+
34+
int a[] = {2,4,6,8,10,12,14,16,18,20};
35+
rotate.rotateL(a, 3, 10);
36+
rotate.printArray(a , 10);
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package arrays;
2+
import java.util.Arrays;
3+
4+
public class Sort_an_array_of_0s_1s_2s_without_using_extra_space_or_sorting_algo {
5+
static int arr1[] = new int[]{1, 5, 9, 10, 15, 20};
6+
static int arr2[] = new int[]{2, 3, 8, 13};
7+
8+
static void merge(int m, int n)
9+
{
10+
// Iterate through all elements of ar2[] starting from to the last element
11+
for (int i=n-1; i>=0; i--)
12+
{
13+
/* Find the smallest element greater than ar2[i]. Move all elements one position ahead till the smallest greater
14+
element is not found */
15+
int j, last = arr1[m-1];
16+
17+
for (j=m-2; j >= 0 && arr1[j] > arr2[i]; j--)
18+
arr1[j+1] = arr1[j];
19+
20+
// If there was a greater element
21+
if (j != m-2 || last > arr2[i]) {
22+
arr1[j+1] = arr2[i];
23+
arr2[i] = last;
24+
25+
}
26+
}
27+
}
28+
29+
// Driver method to test the above function
30+
public static void main1(String[] args)
31+
{
32+
merge(arr1.length,arr2.length);
33+
System.out.print("After Merging nFirst Array: ");
34+
System.out.println(Arrays.toString(arr1));
35+
System.out.print("Second Array: ");
36+
System.out.println(Arrays.toString(arr2));
37+
}
38+
}

basicProblems/Factorial.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package basicProblems;
2+
import java.util.*;
3+
public class Factorial {
4+
5+
public static void main(String[] args) {
6+
int fact=1;
7+
@SuppressWarnings("resource")
8+
Scanner sc = new Scanner(System.in);
9+
int n = sc.nextInt();
10+
11+
for(int i = n ; i>=1;i--) {
12+
fact = fact*i;
13+
}
14+
15+
System.out.println("fact of " + n + " is " + fact );
16+
}
17+
}
18+
19+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package basicProblems;
2+
import java.util.*;
3+
public class Fibonacci_Series {
4+
5+
public static void main(String[] args) {
6+
7+
@SuppressWarnings("resource")
8+
Scanner sc = new Scanner(System.in);
9+
int n = sc.nextInt();
10+
int a=0,b=1;
11+
12+
System.out.println(a + " ");
13+
System.out.println(b + " ");
14+
15+
for(int i=0;i<n-2;i++) {
16+
int c = a+b;
17+
System.out.println(c + " ");
18+
a=b;
19+
b=c;
20+
}
21+
22+
}
23+
24+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package basicProblems;
2+
3+
public class Multiplicative_Table_till_20 {
4+
5+
public static void main(String[] args) {
6+
7+
for(int i=1;i<=20;i++) {
8+
9+
int tableOf = i;
10+
11+
for(int j =1;j<=10;j++) {
12+
System.out.print(tableOf*i + " ");
13+
}
14+
15+
System.out.println();
16+
}
17+
18+
}
19+
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package basicProblems;
2+
3+
public class Palindrome_Number {
4+
5+
public static void main(String[] args) {
6+
7+
8+
9+
10+
11+
}
12+
13+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package basicProblems;
2+
import java.util.*;
3+
public class Prime_Number_Or_Not {
4+
5+
public static void main(String[] args) {
6+
@SuppressWarnings("resource")
7+
Scanner sc = new Scanner(System.in);
8+
int n = sc.nextInt();
9+
boolean isPrime = true;
10+
for(int i=2;i<n;i++) {
11+
if(n % i == 0) {
12+
isPrime = false;
13+
break;
14+
}
15+
}
16+
System.out.print(" is Prime " + isPrime);
17+
18+
19+
}
20+
21+
}

basicProblems/Series_Sum_1.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package basicProblems;
2+
import java.util.*;
3+
public class Series_Sum_1 {
4+
5+
public static void main(String[] args) {
6+
7+
@SuppressWarnings("resource")
8+
Scanner sc = new Scanner(System.in);
9+
int n = sc.nextInt();
10+
11+
float result = 0;
12+
13+
for(float i=1;i<=n;i++) {
14+
result +=1/i;
15+
}
16+
System.out.println(result);
17+
18+
}
19+
20+
}

basicProblems/Series_Sum_2.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package basicProblems;
2+
import java.util.Scanner;
3+
4+
/* This series is also known as TAYLOR series*/
5+
// program to demonstrate series sum of "1 + 1/2 + 1/3 + 1/4 + 1/5 + ...1 /n" this before mentioned series
6+
7+
public class Series_Sum_2 {
8+
9+
public static void main(String[] args) {
10+
11+
@SuppressWarnings("resource")
12+
Scanner sc = new Scanner(System.in);
13+
int n = sc.nextInt();
14+
15+
float result = 0;
16+
17+
for(float i=1;i<=n;i++) {
18+
19+
if(i%2==0) {
20+
result -= 1/i;
21+
}
22+
23+
else {
24+
result += 1/i;
25+
}
26+
27+
System.out.println(result);
28+
}
29+
30+
}
31+
32+
}

0 commit comments

Comments
 (0)