Skip to content

Commit 5f8c0c8

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 68cd8c1 + 382eb2f commit 5f8c0c8

File tree

4 files changed

+157
-31
lines changed

4 files changed

+157
-31
lines changed

Conversions/DecimalToHexaDecimal.java

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,30 @@
1-
import java.lang.StringBuilder;
2-
import java.util.Scanner;
31

4-
class Test {
5-
private static final int sizeOfIntInHalfBytes = 8;
6-
private static final int numberOfBitsInAHalfByte = 4;
7-
private static final int halfByte = 0x0F;
8-
private static final char[] hexDigits = {
9-
'0', '1', '2', '3', '4', '5', '6', '7',
10-
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
11-
};
2+
class DecimalToHexaDecimal {
3+
private static final int sizeOfIntInHalfBytes = 8;
4+
private static final int numberOfBitsInAHalfByte = 4;
5+
private static final int halfByte = 0x0F;
6+
private static final char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
7+
'F' };
128

13-
public static String decToHex(int dec) {
14-
StringBuilder hexBuilder = new StringBuilder(sizeOfIntInHalfBytes);
15-
hexBuilder.setLength(sizeOfIntInHalfBytes);
16-
for (int i = sizeOfIntInHalfBytes - 1; i >= 0; --i)
17-
{
18-
int j = dec & halfByte;
19-
hexBuilder.setCharAt(i, hexDigits[j]);
20-
dec >>= numberOfBitsInAHalfByte;
21-
}
22-
return hexBuilder.toString();
23-
}
9+
// Returns the hex value of the dec entered in the parameter.
10+
public static String decToHex(int dec) {
11+
StringBuilder hexBuilder = new StringBuilder(sizeOfIntInHalfBytes);
12+
hexBuilder.setLength(sizeOfIntInHalfBytes);
13+
for (int i = sizeOfIntInHalfBytes - 1; i >= 0; --i) {
14+
int j = dec & halfByte;
15+
hexBuilder.setCharAt(i, hexDigits[j]);
16+
dec >>= numberOfBitsInAHalfByte;
17+
}
18+
return hexBuilder.toString().toLowerCase();
19+
}
2420

25-
public static void main(String[] args) {
26-
Scanner sc = new Scanner(System.in);
27-
System.out.println("Write your Number to convert into HexaDecimal: ")
28-
int dec = 305445566;
29-
String hex = Integer.toHexString(dec);
30-
String hex = decToHex(dec);
31-
System.out.println(hex);
32-
}
21+
// Test above function.
22+
public static void main(String[] args) {
23+
System.out.println("Test...");
24+
int dec = 305445566;
25+
String libraryDecToHex = Integer.toHexString(dec);
26+
String decToHex = decToHex(dec);
27+
System.out.println("Result from the library : " + libraryDecToHex);
28+
System.out.println("Result decToHex method : " + decToHex);
29+
}
3330
}

Conversions/HexToOct.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
+ * Converts any Hexadecimal Number to Octal
3+
+ *
4+
+ * @author Tanmay Joshi
5+
+ *
6+
+ */
7+
import java.util.Scanner;
8+
9+
public class HexToOct
10+
{
11+
/**
12+
+ * This method converts a Hexadecimal number to
13+
+ * a decimal number
14+
+ *
15+
+ * @param The Hexadecimal Number
16+
+ * @return The Decimal number
17+
+ */
18+
public static int hex2decimal(String s)
19+
{
20+
String str = "0123456789ABCDEF";
21+
s = s.toUpperCase();
22+
int val = 0;
23+
for (int i = 0; i < s.length(); i++)
24+
{
25+
char a = s.charAt(i);
26+
int n = str.indexOf(a);
27+
val = 16*val + n;
28+
}
29+
return val;
30+
}
31+
32+
/**
33+
+ * This method converts a Decimal number to
34+
+ * a octal number
35+
+ *
36+
+ * @param The Decimal Number
37+
+ * @return The Octal number
38+
+ */
39+
public static int decimal2octal(int q)
40+
{
41+
int now;
42+
int i=1;
43+
int octnum=0;
44+
while(q>0)
45+
{
46+
now=q%8;
47+
octnum=(now*(int)(Math.pow(10,i)))+octnum;
48+
q/=8;
49+
i++;
50+
}
51+
octnum/=10;
52+
return octnum;
53+
}
54+
// Main method that gets the hex input from user and converts it into octal.
55+
public static void main(String args[])
56+
{
57+
String hexadecnum;
58+
int decnum,octalnum;
59+
Scanner scan = new Scanner(System.in);
60+
61+
System.out.print("Enter Hexadecimal Number : ");
62+
hexadecnum = scan.nextLine();
63+
64+
// first convert hexadecimal to decimal
65+
66+
decnum = hex2decimal(hexadecnum); //Pass the string to the hex2decimal function and get the decimal form in variable decnum
67+
68+
// convert decimal to octal
69+
octalnum=decimal2octal(decnum);
70+
System.out.println("Number in octal: "+octalnum);
71+
72+
73+
}
74+
}

Data Structures/Graphs/MatrixGraphs.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public boolean edgeDoesExist(int from, int to) {
8080

8181
/**
8282
* This method adds an edge to the graph between two specified
83-
* verticies
83+
* vertices
8484
*
8585
* @param from the data of the vertex the edge is from
8686
* @param to the data of the vertex the edge is going to
@@ -101,7 +101,7 @@ public boolean addEdge(int from, int to) {
101101

102102
/**
103103
* this method removes an edge from the graph between two specified
104-
* verticies
104+
* vertices
105105
*
106106
* @param from the data of the vertex the edge is from
107107
* @param to the data of the vertex the edge is going to
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import java.util.Scanner;
2+
3+
/**
4+
* Program to implement Kadane’s Algorithm to
5+
* calculate maximum contiguous subarray sum of an array
6+
* Time Complexity: O(n)
7+
*
8+
* @author Nishita Aggarwal
9+
*
10+
*/
11+
12+
public class KadaneAlgorithm {
13+
14+
/**
15+
* This method implements Kadane's Algorithm
16+
*
17+
* @param arr The input array
18+
* @return The maximum contiguous subarray sum of the array
19+
*
20+
*/
21+
static int largestContiguousSum(int arr[]){
22+
int i,len=arr.length,cursum=0,maxsum=Integer.MIN_VALUE;
23+
if(len==0) //empty array
24+
return 0;
25+
for(i=0;i<len;i++){
26+
cursum+=arr[i];
27+
if(cursum>maxsum){
28+
maxsum=cursum;
29+
}
30+
if(cursum<=0){
31+
cursum=0;
32+
}
33+
}
34+
return maxsum;
35+
}
36+
37+
/**
38+
* Main method
39+
*
40+
* @param args Command line arguments
41+
*/
42+
public static void main(String[] args) {
43+
Scanner sc=new Scanner(System.in);
44+
int n,arr[],i;
45+
n=sc.nextInt();
46+
arr=new int[n];
47+
for(i=0;i<n;i++){
48+
arr[i]=sc.nextInt();
49+
}
50+
int maxContSum=largestContiguousSum(arr);
51+
System.out.println(maxContSum);
52+
sc.close();
53+
}
54+
55+
}

0 commit comments

Comments
 (0)