Merge remote-tracking branch 'upstream/master'

This commit is contained in:
sahilb2 2017-12-08 14:23:20 -06:00
commit 5f8c0c8b3e
4 changed files with 157 additions and 31 deletions

View File

@ -1,33 +1,30 @@
import java.lang.StringBuilder;
import java.util.Scanner;
class Test {
private static final int sizeOfIntInHalfBytes = 8;
private static final int numberOfBitsInAHalfByte = 4;
private static final int halfByte = 0x0F;
private static final char[] hexDigits = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
class DecimalToHexaDecimal {
private static final int sizeOfIntInHalfBytes = 8;
private static final int numberOfBitsInAHalfByte = 4;
private static final int halfByte = 0x0F;
private static final char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
'F' };
public static String decToHex(int dec) {
StringBuilder hexBuilder = new StringBuilder(sizeOfIntInHalfBytes);
hexBuilder.setLength(sizeOfIntInHalfBytes);
for (int i = sizeOfIntInHalfBytes - 1; i >= 0; --i)
{
int j = dec & halfByte;
hexBuilder.setCharAt(i, hexDigits[j]);
dec >>= numberOfBitsInAHalfByte;
}
return hexBuilder.toString();
}
// Returns the hex value of the dec entered in the parameter.
public static String decToHex(int dec) {
StringBuilder hexBuilder = new StringBuilder(sizeOfIntInHalfBytes);
hexBuilder.setLength(sizeOfIntInHalfBytes);
for (int i = sizeOfIntInHalfBytes - 1; i >= 0; --i) {
int j = dec & halfByte;
hexBuilder.setCharAt(i, hexDigits[j]);
dec >>= numberOfBitsInAHalfByte;
}
return hexBuilder.toString().toLowerCase();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Write your Number to convert into HexaDecimal: ")
int dec = 305445566;
String hex = Integer.toHexString(dec);
String hex = decToHex(dec);
System.out.println(hex);
}
// Test above function.
public static void main(String[] args) {
System.out.println("Test...");
int dec = 305445566;
String libraryDecToHex = Integer.toHexString(dec);
String decToHex = decToHex(dec);
System.out.println("Result from the library : " + libraryDecToHex);
System.out.println("Result decToHex method : " + decToHex);
}
}

74
Conversions/HexToOct.java Normal file
View File

@ -0,0 +1,74 @@
/**
+ * Converts any Hexadecimal Number to Octal
+ *
+ * @author Tanmay Joshi
+ *
+ */
import java.util.Scanner;
public class HexToOct
{
/**
+ * This method converts a Hexadecimal number to
+ * a decimal number
+ *
+ * @param The Hexadecimal Number
+ * @return The Decimal number
+ */
public static int hex2decimal(String s)
{
String str = "0123456789ABCDEF";
s = s.toUpperCase();
int val = 0;
for (int i = 0; i < s.length(); i++)
{
char a = s.charAt(i);
int n = str.indexOf(a);
val = 16*val + n;
}
return val;
}
/**
+ * This method converts a Decimal number to
+ * a octal number
+ *
+ * @param The Decimal Number
+ * @return The Octal number
+ */
public static int decimal2octal(int q)
{
int now;
int i=1;
int octnum=0;
while(q>0)
{
now=q%8;
octnum=(now*(int)(Math.pow(10,i)))+octnum;
q/=8;
i++;
}
octnum/=10;
return octnum;
}
// Main method that gets the hex input from user and converts it into octal.
public static void main(String args[])
{
String hexadecnum;
int decnum,octalnum;
Scanner scan = new Scanner(System.in);
System.out.print("Enter Hexadecimal Number : ");
hexadecnum = scan.nextLine();
// first convert hexadecimal to decimal
decnum = hex2decimal(hexadecnum); //Pass the string to the hex2decimal function and get the decimal form in variable decnum
// convert decimal to octal
octalnum=decimal2octal(decnum);
System.out.println("Number in octal: "+octalnum);
}
}

View File

@ -80,7 +80,7 @@ class AdjacencyMatrixGraph {
/**
* This method adds an edge to the graph between two specified
* verticies
* vertices
*
* @param from the data of the vertex the edge is from
* @param to the data of the vertex the edge is going to
@ -101,7 +101,7 @@ class AdjacencyMatrixGraph {
/**
* this method removes an edge from the graph between two specified
* verticies
* vertices
*
* @param from the data of the vertex the edge is from
* @param to the data of the vertex the edge is going to

View File

@ -0,0 +1,55 @@
import java.util.Scanner;
/**
* Program to implement Kadanes Algorithm to
* calculate maximum contiguous subarray sum of an array
* Time Complexity: O(n)
*
* @author Nishita Aggarwal
*
*/
public class KadaneAlgorithm {
/**
* This method implements Kadane's Algorithm
*
* @param arr The input array
* @return The maximum contiguous subarray sum of the array
*
*/
static int largestContiguousSum(int arr[]){
int i,len=arr.length,cursum=0,maxsum=Integer.MIN_VALUE;
if(len==0) //empty array
return 0;
for(i=0;i<len;i++){
cursum+=arr[i];
if(cursum>maxsum){
maxsum=cursum;
}
if(cursum<=0){
cursum=0;
}
}
return maxsum;
}
/**
* Main method
*
* @param args Command line arguments
*/
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n,arr[],i;
n=sc.nextInt();
arr=new int[n];
for(i=0;i<n;i++){
arr[i]=sc.nextInt();
}
int maxContSum=largestContiguousSum(arr);
System.out.println(maxContSum);
sc.close();
}
}