Skip to content

Instantly share code, notes, and snippets.

View sreeprasad's full-sized avatar

Sreeprasad sreeprasad

  • BlackRock
  • New York
View GitHub Profile
public int moves(int N, int M) {
int x = (N+1)/2;
int result = ((x+M-1)/M)*M;
if(result>N) result = -1;
System.out.printf("%d\n", result);
}
@sreeprasad
sreeprasad / MinMoves.java
Last active August 13, 2019 05:56
Minimum number of moves
private void find(N,M) {
boolean [][] visited = new boolean[100000][1000];
int [][] cache = new int[100000][13];
int ans = dfs(N, visited, cache, 0, M);
if(ans>N) ans = -1;
System.out.printf("%d\n", ans);
}
private int dfs(int N, boolean[][]visited, int [][] cache, int moves, int M) {
if(N==0) {
@sreeprasad
sreeprasad / SubsetsRecursive.java
Last active July 11, 2018 04:36
Simple Recursive way of printing all the subsets of an array.
public void printAllSubsets(int[] input){
if ((input == null) || (input.length == 0)) return;
int N = input.length;
int [] output = new int[N];
printAllSubsets(input, 0, output, 0);
}
private void printAllSubsets(int [] input, int read, int [] output, int write) {
@sreeprasad
sreeprasad / SagheerTheHausmeister.java
Last active July 10, 2018 14:55
This is solution to Sagheer The Hausmeister (812B) problem in Codeforces
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class SagheerTheHausmeister {
private static BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
private static int M_MAX;
private static int N,M;
private static int MATRIX [][] = new int [16][150];
private static int ILL [] = new int [16]; // has index of last bulb while entering from left.
@sreeprasad
sreeprasad / SubsetSum.java
Last active July 9, 2018 14:40
subset sum problem in SPOJ
import java.util.Scanner;
import java.util.Arrays;
import java.util.Comparator;
class Main {
private static Scanner sc;
// input number in array of size N
private static long[] input(int N){
@sreeprasad
sreeprasad / YouAllWillConfirm.cpp
Last active July 3, 2018 03:26
From Programming for the Puzzled course from MIT
# include <iostream>
# include <vector>
using namespace std;
int main() {
char caps[] = {'F', 'F', 'B', 'B', 'B', 'F', 'B', 'B', 'B', 'F', 'F', 'B', 'F'};
# include <iostream>
# include <algorithm>
# include <string>
using namespace std;
bool isOdd(int number){
return number&1;
}
@sreeprasad
sreeprasad / payingUp.cpp
Created June 29, 2018 13:36
The is practice problem MARCHA1 from code chef
#include <iostream>
using namespace std;
// for each subset calculate the sum and compare with expected sum
bool checkSetBitAndValidateSum(long *array, long b, long sum) {
long i=0;
long runningSum=0;
while (b>0) {
if(b&1) { // check if bit is set
@sreeprasad
sreeprasad / all subsets of set
Last active June 28, 2018 14:56
all subsets of set
void generateSubset(char* array) {
int N = strlen(array);
int range = (1<<N)-1;
for (int i=0;i<range;i++) {
checkBitSetAndPrint(array, i);
}
}
n & n-1