UVa 686 Goldbach’s Conjecture II Solution in Java

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * Created by dhruv.pancholi on 20/11/16.
 */
public class Main {

    public static void main(String[] args) throws FileNotFoundException {
        InputReader in = new InputReader(System.in);

        int limit = 2 << 14;
        SieveOfEratosthenes soe = new SieveOfEratosthenes(limit);
        List<Integer> primes = soe.getPrimes();
        int[] arr = new int[limit + 1];

        int p1, p2;
        for (int i = 0; i < primes.size(); i++) {
            p1 = primes.get(i);
            for (int j = i; j < primes.size(); j++) {
                p2 = primes.get(j);
                if (p1 + p2 > limit) break;
                arr[p1 + p2]++;
            }
        }

        int n = 0;
        while (true) {
            n = in.nextInt();
            if (n == 0) break;
            System.out.println(arr[n]);
        }
    }

    private static class SieveOfEratosthenes {
        private boolean[] a;

        public SieveOfEratosthenes(int N) {
            a = new boolean[N + 1];
            for (int i = 0; i <= N; i++) {
                a[i] = true;
            }
            a[0] = false;
            a[1] = false;

            int root = (int) Math.sqrt(N);
            for (int i = 2; i <= root; i++) {
                for (int j = 2 * i; j <= N; j += i) {
                    a[j] = false;
                }
            }
        }

        public boolean[] getArray() {
            return a;
        }

        public boolean isPrime(int n) {
            return a[n];
        }

        public List<Integer> getPrimes() {
            List<Integer> list = new ArrayList<Integer>();
            for (int i = 1; i < a.length; i++) {
                if (a[i]) {
                    list.add(i);
                }
            }
            return list;
        }
    }

    public static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }
    }
}

Sample Input:

6
10
12
0

Sample Output:

1
2
1

UVa 583 Prime Factors Solution in Java

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * Created by dhruv.pancholi on 05/11/16.
 */
public class Main {

    public static List<Integer> getPrimeFactors(int N, List<Integer> primes) {
        List<Integer> factors = new ArrayList<Integer>();
        int index = 0;
        int factor = primes.get(index);
        int limit = (int) Math.sqrt(N) + 1;
        while (N != 1 && factor < limit) {
            while (N % factor == 0) {
                N /= factor;
                factors.add(factor);
            }
            factor = primes.get(++index);
        }
        if (N != 1) factors.add(N);
        return factors;
    }

    public static void main(String[] args) throws FileNotFoundException {
        InputReader in = new InputReader(System.in);

        SieveOfEratosthenes soe = new SieveOfEratosthenes((int) Math.sqrt(Integer.MAX_VALUE) * 10);
        List<Integer> primes = soe.getPrimes();


        int n;
        while (true) {
            n = in.nextInt();
            if (n == 0) break;
            List<Integer> factors = getPrimeFactors(Math.abs(n), primes);
            System.out.print(n + " = ");
            if (n < 0) System.out.print("-1 x ");
            for (int i = 0; i < factors.size() - 1; i++) {
                System.out.print(factors.get(i) + " x ");
            }
            System.out.println(factors.get(factors.size() - 1));
        }
    }

    private static class SieveOfEratosthenes {
        private boolean[] a;

        public SieveOfEratosthenes(int N) {
            a = new boolean[N + 1];
            for (int i = 0; i <= N; i++) {
                a[i] = true;
            }
            a[0] = false;
            a[1] = false;

            int root = (int) Math.sqrt(N);
            for (int i = 2; i <= root; i++) {
                for (int j = 2 * i; j <= N; j += i) {
                    a[j] = false;
                }
            }
        }

        public boolean[] getArray() {
            return a;
        }

        public boolean isPrime(int n) {
            return a[n];
        }

        public List<Integer> getPrimes() {
            List<Integer> list = new ArrayList<Integer>();
            for (int i = 1; i < a.length; i++) {
                if (a[i]) {
                    list.add(i);
                }
            }
            return list;
        }
    }

    public static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }
    }
}

Sample Input:

-190
-191
-192
-193
-194
195
196
197
198
199
200
0

Sample Output:

-190 = -1 x 2 x 5 x 19
-191 = -1 x 191
-192 = -1 x 2 x 2 x 2 x 2 x 2 x 2 x 3
-193 = -1 x 193
-194 = -1 x 2 x 97
195 = 3 x 5 x 13
196 = 2 x 2 x 7 x 7
197 = 197
198 = 2 x 3 x 3 x 11
199 = 199
200 = 2 x 2 x 2 x 5 x 5

UVa 543 Goldbach’s Conjecture Solution in Java

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * Created by dhruv.pancholi on 20/11/16.
 */
public class Main {

    public static void main(String[] args) {

        InputReader in = new InputReader(System.in);

        SieveOfEratosthenes soe = new SieveOfEratosthenes(1000000);
        List<Integer> primes = soe.getPrimes();

        while (true) {
            int n = in.nextInt();
            if (n == 0) break;
            for (int i = 0; i < primes.size(); i++) {
                if (soe.isPrime(n - primes.get(i))) {
                    System.out.println(n + " = " + primes.get(i) + " + " + (n - primes.get(i)));
                    break;
                }
            }
        }
    }

    private static class SieveOfEratosthenes {
        private boolean[] a;

        public SieveOfEratosthenes(int N) {
            a = new boolean[N + 1];
            for (int i = 0; i <= N; i++) {
                a[i] = true;
            }
            a[0] = false;
            a[1] = false;

            int root = (int) Math.sqrt(N);
            for (int i = 2; i <= root; i++) {
                for (int j = 2 * i; j <= N; j += i) {
                    a[j] = false;
                }
            }
        }

        public boolean[] getArray() {
            return a;
        }

        public boolean isPrime(int n) {
            return a[n];
        }

        public List<Integer> getPrimes() {
            List<Integer> list = new ArrayList<Integer>();
            for (int i = 1; i < a.length; i++) {
                if (a[i]) {
                    list.add(i);
                }
            }
            return list;
        }
    }

    private static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (Exception e) {
                    System.exit(0);
                }
            }
            return tokenizer.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }
    }
}

Sample Input:

8
20
42
0

Sample Output:

8 = 3 + 5
20 = 3 + 17
42 = 5 + 37

UVa 524 Prime Ring Problem Solution in Java

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * Created by dhruv.pancholi on 20/11/16.
 */
public class Main {

    private static boolean[] onStack = new boolean[16];

    private static void dfs(List<Integer>[] adjacency, String out, int node, int n, int N) {
        if (n == 1) {
            if (adjacency[node - 1].get(0) == 1) System.out.println(out);
            return;
        }
        for (Integer i : adjacency[node - 1]) {
            if (onStack[i - 1] || i > N) {
                continue;
            } else {
                out += " " + i;
                onStack[i - 1] = true;
                dfs(adjacency, out, i, n - 1, N);
                out = out.substring(0, out.lastIndexOf(" "));
                onStack[i - 1] = false;
            }
        }
    }

    public static void main(String[] args) throws FileNotFoundException {
        InputReader in = new InputReader(System.in);
        SieveOfEratosthenes soe = new SieveOfEratosthenes(50);
        List<Integer>[] adjacency;
        adjacency = new List[16];
        for (int i = 0; i < 16; i++) {
            adjacency[i] = new ArrayList<Integer>();
        }
        for (int i = 0; i < 16; i++) {
            for (int j = i + 1; j < 16; j++) {
                if (soe.isPrime(i + j + 2)) {
                    adjacency[i].add(j + 1);
                    adjacency[j].add(i + 1);
                }
            }
        }

        int c = 0;
        while (true) {
            LinkedList<Integer> stack = new LinkedList<Integer>();
            stack.add(1);
            onStack[0] = true;
            int n = in.nextInt();
            if (c != 0) System.out.println();
            System.out.println("Case " + (++c) + ":");
            if (n == 1) {
                System.out.println(1);
            } else {
                dfs(adjacency, "1", 1, n, n);
            }
        }
    }

    private static class SieveOfEratosthenes {
        private boolean[] a;

        public SieveOfEratosthenes(int N) {
            a = new boolean[N + 1];
            for (int i = 0; i <= N; i++) {
                a[i] = true;
            }
            a[0] = false;
            a[1] = false;

            int root = (int) Math.sqrt(N);
            for (int i = 2; i <= root; i++) {
                for (int j = 2 * i; j <= N; j += i) {
                    a[j] = false;
                }
            }
        }

        public boolean[] getArray() {
            return a;
        }

        public boolean isPrime(int n) {
            return a[n];
        }

        public List<Integer> getPrimes() {
            List<Integer> list = new ArrayList<Integer>();
            for (int i = 1; i < a.length; i++) {
                if (a[i]) {
                    list.add(i);
                }
            }
            return list;
        }
    }

    private static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (Exception e) {
                    System.exit(0);
                }
            }
            return tokenizer.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }
    }
}

Sample Input:

6
8

Sample Output:

Case 1:
1 4 3 2 5 6
1 6 5 2 3 4

Case 2:
1 2 3 8 5 6 7 4
1 2 5 8 3 4 7 6
1 4 7 6 5 8 3 2
1 6 7 4 3 8 5 2

UVa 406 Prime Cuts Solution in Java

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class PrimeCuts {
    public static void main(String[] args) {
        InputReader in = new InputReader(System.in);
        SieveOfEratosthenes soe = new SieveOfEratosthenes(1000);
        boolean[] arr = soe.getArray();
        arr[1] = true;
        int[] agg = new int[1001];
        for (int i = 1; i < agg.length; i++) {
            agg[i] += agg[i - 1];
            if (arr[i]) agg[i]++;
        }

        while (true) {
            int n = in.nextInt();
            int c = in.nextInt();
            int startIndex = (agg[n] % 2 == 0) ? (agg[n] / 2) : (agg[n] / 2 + 1);
            startIndex -= (c - 1);
            startIndex = startIndex > 0 ? startIndex : 1;
            int endIndex = startIndex + ((agg[n] % 2 == 0) ? (2 * c - 1) : (2 * c - 2));
            endIndex = endIndex > n ? n : endIndex;

            System.out.print(n + " " + c + ":");
            int index = 0;
            for (int i = 1; i <= n; i++) {

                if (arr[i]) {
                    index++;
                    if (index >= startIndex && index <= endIndex) {
                        System.out.print(" " + i);
                    }
                }
            }
            System.out.println();
            System.out.println();
        }
    }

    public static class SieveOfEratosthenes {
        private boolean[] a;

        public SieveOfEratosthenes(int N) {
            a = new boolean[N + 1];
            for (int i = 0; i <= N; i++) {
                a[i] = true;
            }
            a[0] = false;
            a[1] = false;

            int root = (int) Math.sqrt(N);
            for (int i = 2; i <= root; i++) {
                for (int j = 2 * i; j <= N; j += i) {
                    a[j] = false;
                }
            }
        }

        public boolean[] getArray() {
            return a;
        }

        public boolean isPrime(int n) {
            return a[n];
        }

        public List<Integer> getPrimes() {
            List<Integer> list = new ArrayList<Integer>();
            for (int i = 1; i < a.length; i++) {
                if (a[i]) {
                    list.add(i);
                }
            }
            return list;
        }
    }

    public static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (Exception e) {
                    System.exit(0);
                }
            }
            return tokenizer.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }
    }
}

Sample Input:

21 2
18 2
18 18
100 7

Sample Output:

21 2: 5 7 11

18 2: 3 5 7 11

18 18: 1 2 3 5 7 11 13 17

100 7: 13 17 19 23 29 31 37 41 43 47 53 59 61 67

UVa 10130 SuperSale solution in Java

import java.io.*;
import java.util.StringTokenizer;

public class SuperSale {

    public static void main(String[] args) throws FileNotFoundException {
        InputReader in = new InputReader(System.in);

        int T = in.nextInt();
        while (T-- != 0) {
            int N = in.nextInt();
            Object[] A = new Object[N];
            int sum = 0;
            for (int n = 0; n < N; n++) {
                A[n] = new Object(in.nextInt(), in.nextInt());
            }

            int G = in.nextInt();
            int MW;
            int max = 0;
            for (int g = 0; g < G; g++) {
                MW = in.nextInt();
                int[][] memo = new int[A.length + 1][31];

                for (int i = 1; i <= A.length; i++) {
                    for (int j = 0; j <= MW; j++) {
                        if (j + A[i - 1].weight <= MW) {
                            memo[i][j + A[i - 1].weight] = Math.max(memo[i - 1][j] + A[i - 1].price, memo[i][j]);
                        }
                        memo[i][j] = Math.max(memo[i][j], memo[i - 1][j]);
                    }
                }

                int localMax = 0;
                for (int j = 0; j <= MW; j++) {
                    localMax = Math.max(localMax, memo[A.length][j]);
                }
                max += localMax;
            }

            System.out.println(max);
        }
    }

    public static class Object {
        public int price;
        public int weight;

        public Object(int price, int weight) {
            this.price = price;
            this.weight = weight;
        }
    }
    
    public static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }
    }

}

Sample Input:

2
3
72 17
44 23
31 24
1
26
6
64 26
85 22
52 4
99 18
39 13
54 9
4
23
20
20
26

Sample Output:

72
514

ACM ICPC LA (Live Archive) 3619 solution in Java

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class SumOfDifferentPrimes {

    public static class SieveOfEratosthenes {
        private boolean[] a;

        public SieveOfEratosthenes(int N) {
            a = new boolean[N + 1];
            for (int i = 0; i < N; i++) {
                a[i] = true;
            }
            a[0] = false;
            a[1] = false;

            int root = (int) Math.sqrt(N);
            for (int i = 2; i <= root; i++) {
                for (int j = 2 * i; j <= N; j += i) {
                    a[j] = false;
                }
            }
        }

        public boolean isPrime(int n) {
            return a[n];
        }

        public List<Integer> getPrimes() {
            List<Integer> list = new ArrayList<Integer>();
            for (int i = 1; i < a.length; i++) {
                if (a[i]) {
                    list.add(i);
                }
            }
            return list;
        }
    }

    public static void main(String[] args) throws FileNotFoundException {
        InputReader in = new InputReader(System.in);
        SieveOfEratosthenes soe = new SieveOfEratosthenes(1120);
        List<Integer> primes = soe.getPrimes();
        int[][][] arr = new int[primes.size() + 1][1121][15];

        arr[0][0][0] = 1;

        for (int i = 1; i < primes.size() + 1; i++) {
            int prime = primes.get(i - 1);

            for (int j = 0; j <= 1120; j++) {
                for (int k = 0; k <= 14; k++) {
                    if ((j + prime) <= 1120 && k < 14) {
                        arr[i][j + prime][k + 1] += arr[i - 1][j][k];
                    }
                    arr[i][j][k] += arr[i - 1][j][k];
                }
            }
        }

        while (true) {
            int n = in.nextInt();
            int k = in.nextInt();
            if (n == 0 && k == 0) break;
            if (n == 0 || k == 0) {
                System.out.println(0);
                continue;
            }
            System.out.println(arr[primes.size()][n][k]);
        }
    }

    public static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }
    }
}

Sample Input:

24 3
24 2
2 1
1 1
4 2
18 3
17 1
17 3
17 4
100 5
1000 10
1120 14
0 0

Sample Output:

2
3
1
0
0
2
1
0
1
55
200102899
2079324314

Hackerrank Algorithmic Crush Solution in Python

from heapq import *

class Node(object):
	def __init__(self, l):
		super(Node, self).__init__()
		# Start, Finish and Weight
		self.s = l[0]; self.f = l[1]; self.w = l[2]
	def __lt__(self, o):
		return self.f < o.f

N, M = map(int, raw_input().split())
A = []; heap = []

for m in xrange(M):
	A.append(Node(map(int, raw_input().split())))

A.sort(key = lambda x: x.s)

maxi = 0; m = 0
for a in A:
	while len(heap)>0 and heap[0].f<a.s:
		m -= heappop(heap).w
	m += a.w
	if m>maxi: maxi = m
	heappush(heap, a)

print maxi

Sample Input:

5 3
1 2 100
2 5 100
3 4 100

Sample Output:

200

Hackerrank Chief Hopper Solution in C++

#include <cmath>
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long ll;

int main() {
    int N; scanf("%d", &N);
    vector <ll> Arr(N);
    for (int i=0;i<N;i++) {
        scanf("%lld", &Arr[i]);
    }
    ll ans = 0;
    for (int i = N-1;i>=0;i--) {
        ans = (Arr[i] + ans)/2 + (Arr[i] + ans)%2;
    }
    printf("%lld\n", ans);
    return 0;
}

Sample Input:

5
3 4 3 2 4

Sample Output:

4

Hackerrank Two Arrays Solution in C++

#include<cstdio>
#include<vector>
#include<map>
#include<algorithm>
#include<queue>

using namespace std;

int main(){
	int N, K; scanf("%d %d", &N, &K);
	priority_queue<int, vector<int>, greater<int> > q;
	int s;
	for(int n = 0; n<N; n++){
		scanf("%d", &s);
		q.push(s);
	}
	int sum = 0, count = -1;
	while(sum<K && !q.empty()){
		sum += q.top(); q.pop();
		count++;
	}
	if(count>0) printf("%d\n", count);
	else printf("0\n");
	return 0;
}

Sample Input:

2
3 10
2 1 3
7 8 9
4 5
1 2 2 1
3 3 3 4

Sample Output:

YES
NO