Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fc32efc

Browse files
committedJun 19, 2021
New algorithms and problems added
1 parent c057811 commit fc32efc

File tree

5 files changed

+111
-0
lines changed

5 files changed

+111
-0
lines changed
 

‎src/algorithm/CalcExponential.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package algorithm;
2+
//To calculate exponential
3+
public class CalcExponential {
4+
public static void main(String[] args) {
5+
int factor=10,expo=3;
6+
int result = 1;
7+
while (expo!=0){
8+
if(expo % 2 == 1){
9+
result *= factor;
10+
}
11+
expo /= 2;
12+
factor *= factor;
13+
}
14+
System.out.println(result);
15+
}
16+
}

‎src/algorithm/Euclid_GCD.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package algorithm;
2+
3+
public class Euclid_GCD {
4+
public static void main(String[] args) {
5+
int a = 105, b=350;
6+
System.out.println(GCD(a, b));
7+
8+
}
9+
10+
public static int GCD(int a, int b) {
11+
while( b !=0){
12+
int reminder = a % b;
13+
a = b;
14+
b =reminder;
15+
}
16+
17+
return a;
18+
}
19+
}

‎src/algorithm/info.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Algorithm:
2+
* Calculate GCD
3+
* Calculate exponential

‎src/array/RotationArray.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package array;
2+
3+
import algorithm.Euclid_GCD;
4+
5+
import java.util.Arrays;
6+
import java.util.Scanner;
7+
8+
public class RotationArray {
9+
public static void printArray(int[] arr){
10+
System.out.println(Arrays.toString(arr));
11+
}
12+
public static void simpleApproach(int[] input,int d){
13+
for (int NumberOfInteration = 1; NumberOfInteration <=d; NumberOfInteration++) {
14+
int value = input[0];
15+
for (int movePosition = 1; movePosition <input.length ; movePosition++) {
16+
input[movePosition-1] = input[movePosition];
17+
}
18+
input[input.length-1] = value;
19+
}
20+
printArray(input);
21+
}
22+
public static void jugglingAlgorithm(int[] input, int d){
23+
d = d%(input.length);
24+
int gcd = Euclid_GCD.GCD(input.length,d);
25+
for (int set = 0; set <gcd ; set++) {
26+
int temp = input[set];
27+
int k = set;
28+
while (true){
29+
int position = (d+k)%(input.length);
30+
if (position==set){
31+
input[position]= temp;
32+
break;
33+
}else{
34+
input[k] = input[position];
35+
}
36+
k = position;
37+
}
38+
}
39+
printArray(input);
40+
}
41+
public static void main(String[] args) {
42+
int[] arr = {1,2,3,4,5,6,7};
43+
Scanner input = new Scanner(System.in);
44+
int d = input.nextInt();
45+
printArray(arr);
46+
// simpleApproach(arr,d);
47+
jugglingAlgorithm(arr,d);
48+
49+
}
50+
}

‎src/problem/MaxSubArray.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package problem;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class MaxSubArray {
7+
public static void main(String[] args) {
8+
List<Integer> A = new ArrayList<>();
9+
A.add(1);
10+
A.add(2);
11+
A.add(-4);
12+
A.add(2);
13+
A.add(2);
14+
15+
int maxEndingHere = A.get(0);
16+
int maxSoFar = A.get(0);
17+
for (int i = 1; i < A.size(); i++) {
18+
maxEndingHere = Math.max(A.get(i), A.get(i) + maxEndingHere);
19+
maxSoFar = Math.max(maxSoFar, maxEndingHere);
20+
}
21+
System.out.println(maxSoFar);
22+
}
23+
}

0 commit comments

Comments
 (0)
Please sign in to comment.