Thursday, 16 March 2017

integerbinconstructor

package com.paper;

public class integerbinconstructor {

public static void main (String[] args){
integerbinconstructor test = new integerbinconstructor();

int N = 18;
System.out.println(test.solution(N));//6

System.out.println(test.solution(2147483647));
}

public int solution (int N){
int counter = 1;
if (N == 1){
return counter;
}
while (N > 1){
if (N % 2 == 0){
counter++;
N = N/2;

} else if ( (N - 1) % 2 == 0){
counter++;
N = N-1;
}
}
return counter;
}
}

longestsentence

package com.paper;

public class longestsentence {

public static void main (String[] args){
longestsentence test = new longestsentence();

System.out.println(test.solution("We test coders. Give us a try?")); //4
System.out.println(test.solution("Forget CVs..Save time . x     x")); //2
System.out.println(test.solution(".")); //0
System.out.println(test.solution("We test coders give us a try")); //7
}

public int solution (String s){

String[] sentencesplits = s.trim().split("[.?!]");

int maxwords = 0;
for (int i = 0; i < sentencesplits.length; i++){

String[] wordssplits = sentencesplits[i].trim().replaceAll(" +", ".").split("[.]");
if (maxwords < wordssplits.length){
maxwords = wordssplits.length;
}
}
return maxwords;
}
}

bugfixingconsecutiveintegers

package com.paper;

public class bugfixingconsecutiveintegers {

public static void main(String[] args) {

bugfixingconsecutiveintegers test = new bugfixingconsecutiveintegers();
int[] A = {1,1,2,3,3};
int K = 2;
System.out.println(test.solution(A, K));
int[] AA = {1,1,3};
int KK = 2;
System.out.println(test.solution(AA, KK));

int[] AAA = {1,2,3,4,5,6,7,8,9};
int KKK = 8;
System.out.println(test.solution(AAA, KKK));
}
public boolean solution (int[] A, int K){
boolean isFound = false;
for (int i = 1; i<= K ; i++){
isFound = false;
for (int j = 0; j < A.length; j++){
if (A[j] == i){
isFound = true;
}
}
if (!isFound){
return isFound;
}
}
return isFound;
}

}

decreprsenior

package com.paper;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class decreprsenior {
public static void main (String[] args){
decreprsenior test = new decreprsenior();
System.out.println(test.solution(353));
System.out.println(test.solution(1000000001));
}
public int solution (int n){
if (n > 1_000_000_000){
return -1;
}

Integer[] digits = new Integer[Integer.toString(n).length()];


for (int i = 0; i < digits.length; i++){
digits[i] = n % 10;
n = n/10;
}
List<Integer> digitslist = Arrays.asList(digits);
Collections.sort(digitslist);

int maxvalue = 0;
for (int j = digitslist.size() ; j > 0; j-- ){
maxvalue = maxvalue + digitslist.get(j-1);
maxvalue = maxvalue * 10;
}
return maxvalue / 10;
}
}

bugfixingmaxonesspan

package com.paper;

public class bugfixingmaxonesspan {
public static void main (String[] args){
bugfixingmaxonesspan test = new bugfixingmaxonesspan();


int[] A = {0,1,1,1,0,1,1,1,0,1};
System.out.println(test.solution(A));

int[] B = {0,0};
System.out.println(test.solution(B));
}

// returns starting position which starts the most number of 1's
public int solution (int[] A){

int count = 0;
int maxcount = 0;
int startingIndex = -1;

for (int i = 0; i <A.length; i++){
if (A[i] == 1){
count++;
} else if (A[i] == 0){
if (maxcount < count){
maxcount = count;
startingIndex = i - count; //when i = 0, not when 1
}
count = 0;
}
}
return startingIndex;
}
}

arrlistlen

package com.paper;

public class arrlistlen {

public static void main(String[] args) {
arrlistlen test = new arrlistlen ();
//A = linked list
//A[0] = head
//A[K] = value of node at index K
//A[K] = -1 = last node of list
//A[K] != -1 = get the next index from this value
// return number of connections
int[] A = {1, 4, -1, 3, 2};
System.out.println(test.solution(A));

int[] B = {-1};
System.out.println(test.solution(B));

}

public int solution(int[] A){

int index = 0;
int head = A[index];
int count = 0;
while (head != -1){
count++;
index = head;
head = A[index];
}
return ++count; //adds -1
}

}

largestsequencegap

package com.paper;

import java.util.Arrays;

public class largestsequencegap {
public static void main(String[] args){
largestsequencegap test = new largestsequencegap();

int[] A = {10, 0, 8, 2, -1, 12, 11, 3};
System.out.println(test.solution(A));

int[] B = {5,5};
System.out.println(test.solution(B));
}


//A - bikes already parked
//A[K] - position of the rack (0 =< K =< N)

//A[K] = 0 , 0 is center rack
//A[k] = +N, N meters right of center
//A[K] = -N, N meters left of center
public int solution (int[] A){

Arrays.sort(A);

int maxgap = 0;
for (int i = 0, j= 1; i < A.length && j < A.length; i++, j++){
int left = A[i];
int right = A[j];
int gap = Math.abs(left - right);
if (gap > maxgap){
maxgap = gap;
}
}
return maxgap;
}
}