Skip to content

Instantly share code, notes, and snippets.

@kkabdol
kkabdol / unique_ptr.cpp
Created December 14, 2020 15:20
unique_ptr example from effective modern c++
//////////////////////////////////////////
// std::unique_ptr usage
// - factory function return type for
// objects in a hierarchy.
//////////////////////////////////////////
//////////////////////////////////////////
// factory function with custom deleter
//////////////////////////////////////////
class Investment
@kkabdol
kkabdol / substrCount.cpp
Created November 14, 2019 19:42
Special palindromic substrings
// problem : https://www.hackerrank.com/challenges/special-palindrome-again/problem
long substrCount(int n, string s) {
long count = 0;
// 1st pass
vector<pair<char, int>> v;
char cur = 0;
for( auto ch : s )
{
@kkabdol
kkabdol / activityNotifications.cpp
Last active October 19, 2019 12:40
Fraudulent Activity Notifications
// problem : https://www.hackerrank.com/challenges/fraudulent-activity-notifications/problem
int activityNotifications(vector<int> expenditure, int d) {
const int MAX_EXPENSE = 200;
const bool isEven = ( d % 2 ) == 0;
const int medianIndex = isEven ? ( d / 2 ) - 1 : d / 2;
// setup counting sort
vector<int> expenditureCounter( MAX_EXPENSE + 1, 0 );
@kkabdol
kkabdol / multithread.cpp
Created March 28, 2019 03:29
Multithread (Review the last test)
#include <iostream>
#include <cassert>
#include <pthread.h>
using namespace std;
int g_Count = 0;
void *thread( void *vargp )
{
for( int i = 0; i < 1000; ++i )
@kkabdol
kkabdol / ReverseLinkedList.cpp
Last active October 19, 2019 12:33
Reverse Linked List (Review the last test)
// https://www.geeksforgeeks.org/reverse-a-linked-list/
#include <iostream>
#include <cassert>
using namespace std;
struct List
{
List* next;
int data;
@kkabdol
kkabdol / ConstructsInHierarchy.cpp
Last active March 28, 2019 00:36
Constructs In Hierarchy (Review the last test)
#include <iostream>
using namespace std;
class A
{
public:
A() { cout << "A()" << endl; }
~A() { cout << "~A()" << endl; }
};
@kkabdol
kkabdol / counttriplets.cpp
Last active February 28, 2019 09:08
Count Triplets
// problem : https://www.hackerrank.com/challenges/count-triplets-1/problem
// solution : https://www.hackerrank.com/challenges/count-triplets-1/forum/comments/468507
long countTriplets(vector<long> arr, long r) {
map<int,long> mp2, mp3;
//mp2 to hold count of needed values after this one to complete 2nd part of triplet
//mp3 to hold count of needed values to complete triplet
long count = 0;
for( long val : arr )
@kkabdol
kkabdol / countOfAnagramSubstring.cpp
Last active October 19, 2019 13:02
Count of total anagram substrings
// problem : https://www.hackerrank.com/challenges/sherlock-and-anagrams/problem
// solution : https://www.geeksforgeeks.org/count-total-anagram-substrings/
// C++ program to count total anagram
// substring of a string
#include <bits/stdc++.h>
using namespace std;
// Total number of lowercase characters
#define MAX_CHAR 26
@kkabdol
kkabdol / arraymanipulation.cpp
Created January 29, 2019 08:33
Array Manipulation
// problem : https://www.hackerrank.com/challenges/crush/problem
// solution : https://www.hackerrank.com/challenges/crush/forum
long arrayManipulation(int n, vector<vector<int>> queries) {
vector<long> arr( n + 1, 0 );
for( auto q : queries )
{
arr[ q[0] ] += q[2];
@kkabdol
kkabdol / minswapstosort.cpp
Created January 28, 2019 08:19
Minimum number of swaps required to sort an array
// https://www.geeksforgeeks.org/minimum-number-swaps-required-sort-array/
int minimumSwaps( vector<int> arr ) {
pair< int, int > arrPos[ arr.size() ];
for( size_t i = 0; i < arr.size(); ++i )
{
arrPos[ i ].first = arr[ i ];
arrPos[ i ].second = i;
}
sort( arrPos, arrPos + arr.size() );