Skip to content

Instantly share code, notes, and snippets.

@sbsatter
sbsatter / segment_tree_max_sum.cpp
Last active December 6, 2020 12:15
Segment Tree Maximum Sum (from SPOJ) problem solution
//
// segment_tree_max_sum.cpp
// Competitive Programming
//
// Created by Shahed Bin Satter on 24/9/20.
// Copyright © 2020 Shahed Bin Satter. All rights reserved.
//
#include <stdio.h>
#include <iostream>
@sbsatter
sbsatter / multiset.cpp
Created December 6, 2020 12:06
Codeforce's multiset solution using segment tree
//
// multiset.cpp
// Competitive Programming
//
// Created by Shahed Bin Satter on 24/9/20.
// Copyright © 2020 Shahed Bin Satter. All rights reserved.
//
#include <stdio.h>
#include <iostream>
@sbsatter
sbsatter / supercomputer.cpp
Created December 6, 2020 10:43
Solution to Supercomputer using segment tree
//
// supercomputer.cpp
// Competitive Programming
//
// Created by Shahed Bin Satter on 25/9/20.
// Copyright © 2020 Shahed Bin Satter. All rights reserved.
//
#include <iostream>
using namespace std;
@sbsatter
sbsatter / Sherlock and subqueries
Created December 6, 2020 10:37
Solution to Hackerrank's problem: Sherlock and Subqueries with segment tree
#include <iostream>
using namespace std;
const int nmax = 1e5 + 1;
const int MIN = -1;
int arr[nmax];
pair<int, int> tree [nmax * 4];
void build(int id, int l, int r) {
if (l == r) {
@sbsatter
sbsatter / segment_tree_build.cpp
Created November 5, 2020 16:22
Process to build a segment tree
// [left, right] => range spanned by current node
void build(int id, int left, int right) { // O (n)
if (left == right) {
// when we divide and conquer on the middle element, left and right becomes equal when we reach the leaf nodes
tree[id] = arr[left];
return;
}
int mid = (left + right)/2;
// build left child first
build(2 * id, left, mid);
@sbsatter
sbsatter / segment_tree_main.cpp
Last active November 5, 2020 16:29
Segment tree for sum of elements in an array
int n; // size of array
int arr[n]; // global array of elements that are provided as input
int tree[4 * n]; // global structure for containing segment tree
// initialize with global array arr
build(1, 1, n);
// query
query(1, 1, n, a, b); // query against range [a, b]
// update
update(1, 1, n, idx, val);
@sbsatter
sbsatter / Navigation.js
Created June 12, 2020 05:30
An updated way to set up React Native Navigation using bottom tabs. Originally, I was following along the tutorial series episode https://www.linkedin.com/learning/create-a-crm-mobile-application-with-react-native-2/create-tab-menu?u=70939946 when I got stuck with deprecated library. Hence, here's the update that solves it. Note, you may need to…
import React from 'react';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { NavigationContainer } from '@react-navigation/native';
import Icon from 'react-native-vector-icons/EvilIcons';
import PeopleList from './PeopleList';
import CompanyList from './CompanyList';
import AddPerson from './AddPerson';
// NOTE: Setting up static navigation options in other individual components is unnecessary now
# Enter your code here. Read input from STDIN. Print output to STDOUT
n = int(input())
arr = list(map(int, input().split()))
w = list(map(int, input().split()))
s = 0
for i in range(n):
s = s + arr[i] * w[i]
# Enter your code here. Read input from STDIN. Print output to STDOUT
n = int(input())
array = sorted(list(map(int, input().split())))
# print(array)
mean = sum(array) / n
if n % 2 == 0:
n1 = n//2
n0 = n1 - 1
@sbsatter
sbsatter / linear_regression.py
Created June 6, 2019 20:09
Day 6: Multiple Linear Regression: Predicting House Prices (https://www.hackerrank.com/challenges/predicting-house-prices/problem)
# Enter your code here. Read input from STDIN. Print output to STDOUT
import numpy as np
def cost(y, y_):
m = y.shape[1]
cost = 1/(2*m) * np.sum(np.square(y-y_))
return cost
dimensions = input()