Skip to content

Instantly share code, notes, and snippets.

@gunpreet34
gunpreet34 / 1. longestCommonSubsequence.cpp
Created May 7, 2020 10:41
LCS based famous problems. Read LCS property.txt for better understanding
//We can think of recursive solution as we did with Knapsack and build Memoized and Tabulation approach from there
#include<bits/stdc++.h>
using namespace std;
void initializeLcs(int **lcs, int val,int n, int m){
for(int i=0;i<=n;i++){
for(int j=0;j<=m;j++){
lcs[i][j] = val;
}
@gunpreet34
gunpreet34 / 1. 0-1 Knapsack.cpp
Created May 6, 2020 23:19
Knapsack based famous problems, read KnapsackProperty.txt for more
#include<bits/stdc++.h>
using namespace std;
void print2dVector(vector< vector<int> > v){
int x = v.size();
int y = v[0].size();
for(int i=0;i<x;i++){
for(int j=0;j<y;j++){
cout << v[i][j] << " ";
}
@gunpreet34
gunpreet34 / kim_refrigeration_samsung.cpp
Created September 10, 2018 13:30
Samsung coding question:
/*
Mr. Kim has to deliver refrigerators to N customers. From the office, he is going to visit all the customers and then return to his home. Each location of the office, his home, and the customers is given in the form of integer coordinates (x,y) (0≤x≤100, 0≤y≤100) . The distance between two arbitrary locations (x1, y1) and (x2, y2) is computed by |x1-x2| + |y1-y2|, where |x| denotes the absolute value of x; for instance, |3|=|-3|=3. The locations of the office, his home, and the customers are all distinct. You should plan an optimal way to visit all the N customers and return to his among all the possibilities.
You are given the locations of the office, Mr. Kim’s home, and the customers; the number of the customers is in the range of 5 to 10. Write a program that, starting at the office, finds a (the) shortest path visiting all the customers and returning to his home. Your program only have to report the distance of a (the) shortest path.
Constraints
5≤N≤10. Each location (x,y) is in a bounded grid, 0≤x≤1
@gunpreet34
gunpreet34 / bomb_spaceship_samsung
Created September 9, 2018 20:41
Samsung coding question
/*
You’ll be given a grid as below:
0 1 0 2 0 --> Non highlighted part
0 2 2 2 1
0 2 1 1 1
1 0 1 0 0
0 0 1 2 2
1 1 0 0 1
x x S x x -->highlighted yellow
@gunpreet34
gunpreet34 / bipartite_graph_samsung.cpp
Last active May 8, 2020 12:07
Samsung coding round question: Given a graph print either of the set of the vertices that are colored with the same color. And if the graph is not bipartite print “-1”. Test cases also included the cases when a graph is not connected.
#include<iostream>
using namespace std;
int n;
int arr[100][100]={0};
bool isBiPartite(int i,int color[]){
bool flag = true;
for(int j=0;j<n;j++){
if(arr[i][j] == 1){
if(color[j] == -1){//If color of current node not set
@gunpreet34
gunpreet34 / baloon_burst_samsung
Created September 9, 2018 16:09
Samsung coding round question: There are n balloons and n bullets and each balloon is assigned with a particular number (point). Whenever a particular balloon is shot the no of points increases by 1.the multiplication of point assigned to balloon on left and that of right side. 2.point assigned to left if no right exists 3.point assigned to righ…
#include<iostream>
using namespace std;
int findLeft(int arr[],int n,int j,bool isBurst[],bool &found){//To find next left balloon that is not burst
if(j <= 0){
found = false;
return 1;
}
for(int i=j-1;i>=0;i--){
if(!isBurst[i]){
@gunpreet34
gunpreet34 / wormhole_samsung.cpp
Created September 8, 2018 05:05
Code for question: There is a source (S) and destination (D) and a spacecraft has to go from S to D. There are N number of wormholes in between which has following properties: Each wormhole has an entry and an exit. Each wormhole is bi-directional i.e. one can enter and exit from any of the ends. The time to cross the wormhole is given and the s…
#include<iostream>
using namespace std;
#define i_max 2147483647
int nw,sx,sy,dx,dy;//Source and destination co-ordinates
int dist[1001][1001];
bool set[1001][1001];
class Wormhole{
public:
int x1,y1,x2,y2,cost;
@gunpreet34
gunpreet34 / MaxHeap.cpp
Created March 31, 2018 22:22
Create a MaxHeap from a given non-arranged vector and also implement Heap Sort functionality. Time complexity constraints of all the methods are described.
#include<bits/stdc++.h>
using namespace std;
class MaxHeap{
private:
//Variables
vector<int> v;
int size;
//Properties