Skip to content

Instantly share code, notes, and snippets.

View VardanGrigoryan's full-sized avatar

Vardan VardanGrigoryan

View GitHub Profile
@VardanGrigoryan
VardanGrigoryan / bst average
Created December 23, 2013 16:38
Տրված է երկուական փնտրման ծառ, որի յուրաքանչյուր հանգույց (node) պարունակում է ինչ-որ ամբողջ թիվ։ Գտնել այդ թվերի միջին թվաբանականը։ Ալգորիթմի պահանջվող բարդությունն է O(n):
#include <iostream>
#include <stdlib.h>
struct node
{
struct node* left;
struct node* right;
int data;
};
@VardanGrigoryan
VardanGrigoryan / lca
Created December 15, 2013 16:54
Գտնել երկուական փնտրման ծառում տրված երկու տարրերի ամենափոքր ընդհանուր parent-ը (lowest common ancestor)։ Ալգորիթմի բարդությունը O(n):
#include <iostream>
#include <queue>
#include <stdlib.h>
struct node
{
struct node* left;
struct node* rigth;
struct node* parent;
@VardanGrigoryan
VardanGrigoryan / Is path exist
Last active December 31, 2015 08:49
Տրված է միաչափ զանգված, որի տարրերը կարող են լինել 0 կամ 1։ Ընդ որում առաջին տարրը միշտ 0 - է։ Զանգվածը կարելի է շրջանցել, անցնելով միայն 0-ների վրայով, ընդ որում մեկ քայլի երկարությունն է 3 կամ 5 (3 կամ 5 տարր)։ Անհրաժեշտ է գրել ալգորիթմ, որը կստուգի տվյալ զանգվածում առաջին տարրից մինչև վերջին տարր ընկած ճանապարհի առկայությունը։ Պահանջվող ալգոր…
#include <iostream>
#include <queue>
#include <stdlib.h>
struct node
{
struct node* left;
struct node* rigth;
int data;
};
@VardanGrigoryan
VardanGrigoryan / gist:7958104
Created December 14, 2013 11:22
Տրված է երկուական փնտրման ծառ։ Տպել ծառը մակարդակ առ մակարդակ (level order), ալգորիթմի բարդություն է O(n):
#include <iostream>
#include <queue>
#include <stdlib.h>
struct node
{
struct node* left;
struct node* rigth;
int data;
@VardanGrigoryan
VardanGrigoryan / Median problem
Last active December 31, 2015 00:39
Տրված են երկու n չափանի սորտավորված զանգվածներ։ Գտնել դրանց միավորումից ստացված զանգվածի մեջտեղի տարրը։ Ալգորիթմի բարդությունը O(logn):
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
int get_median(int* a, int s)
{
if(s % 2 == 0)
{
return (a[s/2] + a[s/2 - 1]) / 2;