Skip to content

Commit a33de39

Browse files
Merge pull request #637 from arpitghura/main
Refactorised Java Folder
2 parents f2988b7 + 40a773a commit a33de39

37 files changed

+280
-337
lines changed

Java/.idea/.gitignore

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,60 @@
1-
package com.company;
2-
3-
public class LeftRotateArrayByD {
4-
5-
//Reversal Algorithm-->
6-
static void reverse(int[] arr, int low, int high){
7-
int temp=0;
8-
while (low<high) {
9-
temp = arr[low];
10-
arr[low] = arr[high];
11-
arr[high] = temp;
12-
low++;
13-
high--;
14-
}
15-
}
16-
17-
static void leftRotate(int[] arr, int d){
18-
if (d==0)
19-
return;
20-
21-
int n=arr.length;
22-
reverse(arr,0,d-1);
23-
reverse(arr, d,n-1);
24-
reverse(arr,0,n-1);
25-
}
26-
27-
// Better Method-->
28-
29-
// static void leftRotate(int[] arr, int d){
30-
// int[] temp = new int[d];
31-
// for (int i=0; i<d; i++)
32-
// temp[i]=arr[i];
33-
// for (int i=d; i< arr.length; i++)
34-
// arr[i-d]=arr[i];
35-
// for (int i=0; i<d; i++)
36-
// arr[arr.length-d+1]=temp[i];
37-
// }
38-
39-
// Naive Method-->
40-
41-
// static void leftRotateOne(int[] arr){
42-
// int n = arr.length;
43-
// int temp = arr[0];
44-
// for (int i=1; i<arr.length; i++)
45-
// arr[i-1]=arr[i];
46-
// arr[n-1]=temp;
47-
// }
48-
// static void LeftRotate(int[] arr, int d){
49-
// for (int i=0; i<d; i++)
50-
// leftRotateOne(arr);
51-
// }
52-
public static void main(String[] args) {
53-
int[] arr = new int[]{1,2,3,4,5};
54-
int d = 2;
55-
int n = arr.length;
56-
leftRotate(arr, 2);
57-
for (int i=0; i<n; i++)
58-
System.out.print(arr[i] + " ");
59-
}
60-
}
1+
package com.company;
2+
3+
public class LeftRotateArrayByD {
4+
5+
//Reversal Algorithm-->
6+
static void reverse(int[] arr, int low, int high){
7+
int temp=0;
8+
while (low<high) {
9+
temp = arr[low];
10+
arr[low] = arr[high];
11+
arr[high] = temp;
12+
low++;
13+
high--;
14+
}
15+
}
16+
17+
static void leftRotate(int[] arr, int d){
18+
if (d==0)
19+
return;
20+
21+
int n=arr.length;
22+
reverse(arr,0,d-1);
23+
reverse(arr, d,n-1);
24+
reverse(arr,0,n-1);
25+
}
26+
27+
// Better Method-->
28+
29+
// static void leftRotate(int[] arr, int d){
30+
// int[] temp = new int[d];
31+
// for (int i=0; i<d; i++)
32+
// temp[i]=arr[i];
33+
// for (int i=d; i< arr.length; i++)
34+
// arr[i-d]=arr[i];
35+
// for (int i=0; i<d; i++)
36+
// arr[arr.length-d+1]=temp[i];
37+
// }
38+
39+
// Naive Method-->
40+
41+
// static void leftRotateOne(int[] arr){
42+
// int n = arr.length;
43+
// int temp = arr[0];
44+
// for (int i=1; i<arr.length; i++)
45+
// arr[i-1]=arr[i];
46+
// arr[n-1]=temp;
47+
// }
48+
// static void LeftRotate(int[] arr, int d){
49+
// for (int i=0; i<d; i++)
50+
// leftRotateOne(arr);
51+
// }
52+
public static void main(String[] args) {
53+
int[] arr = new int[]{1,2,3,4,5};
54+
int d = 2;
55+
int n = arr.length;
56+
leftRotate(arr, 2);
57+
for (int i=0; i<n; i++)
58+
System.out.print(arr[i] + " ");
59+
}
60+
}
File renamed without changes.
Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
1-
package XtraQuestions.StringsArrayMixed;
2-
// You are given two numbers n and k. You are required to rotate n k times to the right. If kis positive, rotate to the right l.e. remove rightmost digit and make it leftmost. Do the reverse for negative value of k. Also k can have an absolute value larger than number of digits in n. 2. Take as input n and k. 3. Print the rotated number. 4.
3-
// Note - Assume that the number of rotations will not cause leading O's in the result, e.g. such an input will not be given
4-
// n = 12340056 k= 3 r = 05612340
5-
// Constraints 1 <= n < 10^9 - 10^9 < k < 10^9
6-
// Format
7-
// Input
8-
// "n" where n is any integer. "K" where k is any integer.
9-
// Output
10-
// "r", the rotated number
11-
// SAMPLE INPUT
12-
// 562984
13-
// 2
14-
// OUTPUT
15-
// 845629
16-
17-
import java.util.*;
18-
19-
public class RoatateNumberInRightKTimes {
20-
public static void main(String[] args) {
21-
// int n = 12340056; // This type of number is not allowed
22-
int n = 124452;
23-
System.out.println(n);
24-
25-
int k = 2;
26-
27-
int frontnum = 0;
28-
for(int i=0; i<k; i++){
29-
int rem = n%10;
30-
int remaining = n/10;
31-
32-
// for 0's in between we have to use this
33-
// if(rem==0){
34-
// String padded = String.format("%08d" , remaining);
35-
// // System.out.println("padded: "+ padded);
36-
// n = Integer.parseInt(padded);
37-
// // n = padded;
38-
// System.out.println("the n is : " + n);
39-
// continue;
40-
// }
41-
int count = 0;
42-
int temp = remaining;
43-
while(temp!=0){
44-
count++;
45-
temp = temp/10;
46-
}
47-
// System.out.println("count : " + count);
48-
frontnum = rem*(int)(Math.pow(10,count)) + remaining;
49-
n = frontnum;
50-
// System.out.println("frontnum : " + frontnum);
51-
}
52-
53-
System.out.println("The number after " + k + " rotations: " + frontnum);
54-
}
55-
}
1+
package XtraQuestions.StringsArrayMixed;
2+
// You are given two numbers n and k. You are required to rotate n k times to the right. If kis positive, rotate to the right l.e. remove rightmost digit and make it leftmost. Do the reverse for negative value of k. Also k can have an absolute value larger than number of digits in n. 2. Take as input n and k. 3. Print the rotated number. 4.
3+
// Note - Assume that the number of rotations will not cause leading O's in the result, e.g. such an input will not be given
4+
// n = 12340056 k= 3 r = 05612340
5+
// Constraints 1 <= n < 10^9 - 10^9 < k < 10^9
6+
// Format
7+
// Input
8+
// "n" where n is any integer. "K" where k is any integer.
9+
// Output
10+
// "r", the rotated number
11+
// SAMPLE INPUT
12+
// 562984
13+
// 2
14+
// OUTPUT
15+
// 845629
16+
17+
import java.util.*;
18+
19+
public class RoatateNumberInRightKTimes {
20+
public static void main(String[] args) {
21+
// int n = 12340056; // This type of number is not allowed
22+
int n = 124452;
23+
System.out.println(n);
24+
25+
int k = 2;
26+
27+
int frontnum = 0;
28+
for(int i=0; i<k; i++){
29+
int rem = n%10;
30+
int remaining = n/10;
31+
32+
// for 0's in between we have to use this
33+
// if(rem==0){
34+
// String padded = String.format("%08d" , remaining);
35+
// // System.out.println("padded: "+ padded);
36+
// n = Integer.parseInt(padded);
37+
// // n = padded;
38+
// System.out.println("the n is : " + n);
39+
// continue;
40+
// }
41+
int count = 0;
42+
int temp = remaining;
43+
while(temp!=0){
44+
count++;
45+
temp = temp/10;
46+
}
47+
// System.out.println("count : " + count);
48+
frontnum = rem*(int)(Math.pow(10,count)) + remaining;
49+
n = frontnum;
50+
// System.out.println("frontnum : " + frontnum);
51+
}
52+
53+
System.out.println("The number after " + k + " rotations: " + frontnum);
54+
}
55+
}
File renamed without changes.

Java/BinarySearch.java

Lines changed: 0 additions & 19 deletions
This file was deleted.
-1.08 KB
Binary file not shown.
-555 Bytes
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
1-
package Searching;
2-
3-
4-
public class BinarySearchSQRT {
5-
public static void main(String[] args) {
6-
int n = 40;
7-
int p = 3;
8-
System.out.printf("%.3f",sqrt(n,p));
9-
10-
}
11-
//time complexity: O(log n)
12-
static double sqrt(int n, int p)
13-
{
14-
int start = 0;
15-
int end = n;
16-
double root = 0.0;
17-
18-
while(start <= end)
19-
{
20-
int m = start + (end - start) / 2;
21-
22-
if(m * m == n)
23-
return m;
24-
if(m * m > n)
25-
{
26-
end = m - 1;
27-
}
28-
else
29-
start = m + 1;
30-
}
31-
double incr = 0.1;
32-
for(int i = 0; i < p; i++)
33-
{
34-
while(root * root <= n)
35-
root += incr;
36-
root -= incr;
37-
incr /= 10;
38-
39-
}
40-
return root;
41-
}
42-
}
43-
1+
package Searching;
2+
3+
4+
public class BinarySearchSQRT {
5+
public static void main(String[] args) {
6+
int n = 40;
7+
int p = 3;
8+
System.out.printf("%.3f",sqrt(n,p));
9+
10+
}
11+
//time complexity: O(log n)
12+
static double sqrt(int n, int p)
13+
{
14+
int start = 0;
15+
int end = n;
16+
double root = 0.0;
17+
18+
while(start <= end)
19+
{
20+
int m = start + (end - start) / 2;
21+
22+
if(m * m == n)
23+
return m;
24+
if(m * m > n)
25+
{
26+
end = m - 1;
27+
}
28+
else
29+
start = m + 1;
30+
}
31+
double incr = 0.1;
32+
for(int i = 0; i < p; i++)
33+
{
34+
while(root * root <= n)
35+
root += incr;
36+
root -= incr;
37+
incr /= 10;
38+
39+
}
40+
return root;
41+
}
42+
}
43+
File renamed without changes.

0 commit comments

Comments
 (0)