├── README.md ├── 30-days-of-code-challenges ├── day_16_exceptions_string_to_integer.py ├── day_0_hello_world.py ├── day_29_bitwise_and.py ├── day_28_regex_patterns_and_intro_to_databases.py ├── day_5_loops.java ├── day_26_nested_logic.py ├── day_17_more_exceptions.py ├── day_21_generics.cpp ├── day_7_arrays.java ├── day_25_running_time_and_complexity.py ├── day_27_testing.py ├── day_16_exceptions_string_to_integer.java ├── day_20_sorting.py ├── day_9_recursion.java ├── day_0_hello_world.java ├── day_6_let's_review.java ├── day_0_hello_world.cpp ├── day_21_generics.java ├── day_10_binary_numbers.java ├── day_26_nested_logic.cpp ├── day_29_bitwise_and.java ├── day_16_exceptions_string_to_integer.cpp ├── day_3_intro_to_conditional_statements.java ├── day_8_dictionaries_and_maps.java ├── day_25_running_time_and_complexity.cpp ├── day_17_more_exceptions.cpp ├── day_19_interfaces.cpp ├── day_27_testing.cpp ├── day_19_interfaces.java ├── day_2_operators.java ├── day_28_regex_patterns_and_intro_to_databases.java ├── day_11_2d_arrays.java ├── day_29_bitwise_and.cpp ├── day_22_binary_search_trees.py ├── day_25_running_time_and_complexity.java ├── day_28_regex_patterns_and_intro_to_databases.cpp ├── day_26_nested_logic.java ├── day_17_more_exceptions.java ├── day_20_sorting.java ├── day_13_abstract_classes.java ├── day_23_bst_level_order_traversal.py ├── day_1_data_types.java ├── day_15_linked_list.java ├── day_18_queues_and_stacks.py ├── day_14_scope.java ├── day_12_inheritance.java ├── day_4_class_vs_instance.java ├── day_24_more_linked_lists.py ├── day_20_sorting.cpp ├── day_27_testing.java ├── day_22_binary_search_trees.java ├── day_22_binary_search_trees.cpp ├── day_18_queues_and_stacks.cpp ├── day_23_bst_level_order_traversal.cpp ├── day_24_more_linked_lists.java ├── day_23_bst_level_order_traversal.java ├── day_18_queues_and_stacks.java └── day_24_more_linked_lists.cpp ├── algorithms ├── warmup │ ├── solve-me-first │ │ ├── Solution.py │ │ ├── Solution.cpp │ │ └── Solution.java │ └── save-the-prisoner │ │ ├── Solution.py │ │ ├── Solution.cpp │ │ └── Solution.java └── dynamic-programming │ └── knapsack │ ├── Solution.py │ ├── Solution.cpp │ └── Solution.java ├── 10-days-of-statistics ├── day-0-weighted-mean │ ├── Solution.py │ ├── Solution.java │ └── Solution.cpp ├── day-1-quartiles │ ├── Solution.py │ ├── Solution.java │ └── Solution.cpp ├── day-1-interquartile-range │ ├── Solution.py │ ├── Solution.cpp │ └── Solution.java └── day-0-mean-median-and-mode │ ├── Solution.py │ ├── Solution.cpp │ └── Solution.java ├── data-structures ├── arrays │ ├── left-rotation │ │ ├── Solution.py │ │ ├── Solution.cpp │ │ └── Solution.java │ ├── dynamic-array │ │ ├── Solution.py │ │ ├── Solution.cpp │ │ └── Solution.java │ ├── arrays_ds.java │ ├── sparse_arrays.java │ └── 2d_array_ds.java ├── trees │ ├── tree-height-of-a-binary-tree │ │ └── Solution.java │ ├── tree-inorder-traversal │ │ └── Solution.java │ ├── tree-postorder-traversal │ │ └── Solution.java │ ├── tree-preorder-traversal │ │ └── Solution.java │ ├── tree-level-order-traversal │ │ ├── Solution.java │ │ └── Solution.cpp │ ├── tree-top-view │ │ ├── Solution.java │ │ └── Solution.cpp │ ├── tree-huffman-decoding │ │ ├── Solution.cpp │ │ └── Solution.java │ ├── binary-search-tree-insertion │ │ ├── Solution.java │ │ └── Solution.cpp │ └── binary-search-tree-lowest-common-ancestor │ │ ├── Solution.cpp │ │ └── Solution.java ├── linked-lists │ ├── insert_a_node_at_the_head_of_a_linked_list.java │ ├── delete-duplicate-value-nodes-from-a-sorted-linked-list │ │ ├── Solution.java │ │ ├── Solution.py │ │ └── Solution.cpp │ ├── print_the_elements_of_a_linked_list.java │ ├── cycle-detection │ │ ├── Solution.py │ │ ├── Solution.cpp │ │ └── Solution.java │ ├── get-node-value │ │ ├── Solution.py │ │ ├── Solution.java │ │ └── Solution.cpp │ ├── reverse-a-doubly-linked-list │ │ ├── Solution.cpp │ │ ├── Solution.py │ │ └── Solution.java │ ├── compare-two -linked-lists │ │ ├── Solution.java │ │ ├── Solution.py │ │ └── Solution.cpp │ ├── insert_a_node_at_the_tail_of_a_linked_list.java │ ├── delete_a_node.java │ ├── print_in_reverse.java │ ├── reverse_a_linked_list.java │ ├── merge-two-sorted-linked-lists │ │ ├── Solution.py │ │ ├── Solution.cpp │ │ └── Solution.java │ ├── find-merge-point-of-two-lists │ │ ├── Solution.cpp │ │ ├── Solution.py │ │ └── Solution.java │ ├── insert_a_node_at_a_specific_position_in_a_linked_list.java │ └── inserting-a-node-into-a-sorted-doubly-linked-list │ │ ├── Solution.java │ │ ├── Solution.py │ │ └── Solution.cpp └── heap │ └── qheap1.java ├── cracking-the-coding-interview ├── bit-manipulation-lonely-integer │ ├── Solution.py │ ├── Solution.java │ └── Solution.cpp ├── arrays-left-rotation │ ├── Solution.py │ ├── Solution.java │ └── Solution.cpp ├── strings-making-anagrams │ ├── Solution.py │ ├── Solution.java │ └── Solution.cpp └── stacks-balanced-brackets │ ├── Solution.py │ ├── Solution.java │ └── Solution.cpp ├── linkedIn-placements └── bitwise-and │ ├── Solution.py │ ├── Solution.java │ └── Solution.cpp ├── project euler ├── project_euler_ 1.py ├── project_euler_ 1.cpp └── project_euler_ 1.java ├── interview-preparation-kit ├── arrays │ ├── new-year-chaos │ │ ├── Solution.py │ │ ├── Solution.java │ │ └── Solution.cpp │ ├── ctci-array-left-rotation │ │ ├── Solution.py │ │ ├── Solution.java │ │ └── Solution.cpp │ └── 2d-array │ │ ├── Solution.py │ │ ├── Solution.cpp │ │ └── Solution.java ├── warm-up-challenges │ ├── counting-valleys │ │ ├── Solution.py │ │ ├── Solution.cpp │ │ └── Solution.java │ ├── repeated-string │ │ ├── Solution.py │ │ ├── Solution.cpp │ │ └── Solution.java │ ├── sock-merchant │ │ ├── Solution.py │ │ ├── Solution.java │ │ └── Solution.cpp │ └── jumping-on-the-clouds │ │ ├── Solution.py │ │ ├── Solution.java │ │ └── Solution.cpp └── sorting │ ├── fraudulent-activity-notifications │ ├── Solution.py │ └── Solution.java │ ├── sorting-comparator │ └── Solution.java │ ├── sorting-bubble-sort │ └── Solution.java │ ├── mark-and-toys │ └── Solution.java │ └── ctci-merge-sort │ └── Solution.java └── functional-programming └── recursion └── computing-the-gcd └── Solution.java /README.md: -------------------------------------------------------------------------------- 1 | # hackerrank 2 | Coding practices on https://www.hackerrank.com 3 | -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_16_exceptions_string_to_integer.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | 3 | import sys 4 | 5 | S = raw_input().strip() 6 | try: 7 | print(int(S)) 8 | except: 9 | print("Bad String") 10 | -------------------------------------------------------------------------------- /algorithms/warmup/solve-me-first/Solution.py: -------------------------------------------------------------------------------- 1 | def solveMeFirst(a,b): 2 | # Hint: Type return a+b below 3 | return a+b 4 | 5 | 6 | num1 = input() 7 | num2 = input() 8 | res = solveMeFirst(num1,num2) 9 | print res -------------------------------------------------------------------------------- /10-days-of-statistics/day-0-weighted-mean/Solution.py: -------------------------------------------------------------------------------- 1 | n = int(raw_input()) 2 | x = list(map(int, raw_input().split())) 3 | w = list(map(int, raw_input().split())) 4 | dividend, divisor = 0.0, 0 5 | for i in range(n): 6 | dividend += x[i] * w[i] 7 | divisor += w[i] 8 | print "%0.1f" % (dividend/divisor) -------------------------------------------------------------------------------- /algorithms/warmup/save-the-prisoner/Solution.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | T = raw_input() 3 | for i in range(0, int(T)): 4 | N, M, S = map(int,raw_input().strip().split(' ')) 5 | id = (S+M-1)%N 6 | if id == 0: 7 | print N 8 | else: 9 | print id -------------------------------------------------------------------------------- /data-structures/arrays/left-rotation/Solution.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | n, d = map(int, raw_input().split()) 3 | arr = map(int, raw_input().split()) 4 | result = [0] * n 5 | for i in range(0,n): 6 | result[(i+n-d)%n] = arr[i] 7 | for i in range(0,n): 8 | print result[i], 9 | -------------------------------------------------------------------------------- /cracking-the-coding-interview/bit-manipulation-lonely-integer/Solution.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | 3 | import sys 4 | 5 | def lonely_integer(a): 6 | result = 0 7 | for i in range(len(a)): 8 | result ^= a[i] 9 | return result 10 | 11 | n = int(raw_input().strip()) 12 | a = map(int,raw_input().strip().split(' ')) 13 | print lonely_integer(a) -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_0_hello_world.py: -------------------------------------------------------------------------------- 1 | # Read a full line of input from stdin and save it to our dynamically typed variable, input_string. 2 | inputString = raw_input() 3 | 4 | # Print a string literal saying "Hello, World." to stdout. 5 | print 'Hello, World.' 6 | print inputString 7 | # TODO: Write a line of code here that prints the contents of input_string to stdout. -------------------------------------------------------------------------------- /data-structures/trees/tree-height-of-a-binary-tree/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | class Node 4 | int data; 5 | Node left; 6 | Node right; 7 | */ 8 | int height(Node root) 9 | { 10 | if (root==null) 11 | return -1; 12 | else 13 | return 1+ Math.max(height(root.left), height(root.right)); 14 | } -------------------------------------------------------------------------------- /linkedIn-placements/bitwise-and/Solution.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | 3 | import sys 4 | 5 | 6 | t = int(raw_input().strip()) 7 | for a0 in xrange(t): 8 | n,k = raw_input().strip().split(' ') 9 | n,k = [int(n),int(k)] 10 | if(k%2==0): 11 | if(((k-1)|k)<=n): 12 | print k-1 13 | else: 14 | print k-2 15 | else: 16 | print k-1 -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_29_bitwise_and.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | 3 | import sys 4 | 5 | t = int(raw_input().strip()) 6 | for a0 in xrange(t): 7 | n,k = raw_input().strip().split(' ') 8 | n,k = [int(n),int(k)] 9 | if(k%2==0): 10 | if(((k-1)|k)<=n): 11 | print k-1 12 | else: 13 | print k-2 14 | else: 15 | print k-1 16 | -------------------------------------------------------------------------------- /cracking-the-coding-interview/arrays-left-rotation/Solution.py: -------------------------------------------------------------------------------- 1 | def array_left_rotation(a, n, k): 2 | rotated = [0] * n 3 | for i in range(0, n): 4 | rotated[(i+n-k)%n] = a[i] 5 | return rotated 6 | 7 | n, k = map(int, raw_input().strip().split(' ')) 8 | a = map(int, raw_input().strip().split(' ')) 9 | answer = array_left_rotation(a, n, k); 10 | print ' '.join(map(str,answer)) -------------------------------------------------------------------------------- /data-structures/trees/tree-inorder-traversal/Solution.java: -------------------------------------------------------------------------------- 1 | /* you only have to complete the function given below. 2 | Node is defined as 3 | 4 | class Node { 5 | int data; 6 | Node left; 7 | Node right; 8 | } 9 | 10 | */ 11 | 12 | void inOrder(Node root) { 13 | if (root != null) { 14 | inOrder(root.left); 15 | System.out.print(root.data + " "); 16 | inOrder(root.right); 17 | } 18 | } -------------------------------------------------------------------------------- /data-structures/trees/tree-postorder-traversal/Solution.java: -------------------------------------------------------------------------------- 1 | /* you only have to complete the function given below. 2 | Node is defined as 3 | 4 | class Node { 5 | int data; 6 | Node left; 7 | Node right; 8 | } 9 | 10 | */ 11 | void postOrder(Node root) { 12 | if (root != null) { 13 | postOrder(root.left); 14 | postOrder(root.right); 15 | System.out.print(root.data + " "); 16 | } 17 | } -------------------------------------------------------------------------------- /cracking-the-coding-interview/strings-making-anagrams/Solution.py: -------------------------------------------------------------------------------- 1 | def number_needed(a, b): 2 | freq = [0] * 26 3 | for c in a: 4 | freq[ord(c)-ord('a')]+=1 5 | for c in b: 6 | freq[ord(c)-ord('a')]-=1 7 | deleteCount = 0 8 | for val in freq: 9 | deleteCount += abs(val) 10 | return deleteCount 11 | 12 | a = raw_input().strip() 13 | b = raw_input().strip() 14 | 15 | print number_needed(a, b) -------------------------------------------------------------------------------- /data-structures/trees/tree-preorder-traversal/Solution.java: -------------------------------------------------------------------------------- 1 | /* you only have to complete the function given below. 2 | Node is defined as 3 | 4 | class Node { 5 | int data; 6 | Node left; 7 | Node right; 8 | } 9 | 10 | */ 11 | 12 | void preOrder(Node root) { 13 | if (root != null) { 14 | System.out.print(root.data + " "); 15 | preOrder(root.left); 16 | preOrder(root.right); 17 | } 18 | } -------------------------------------------------------------------------------- /algorithms/dynamic-programming/knapsack/Solution.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | t=raw_input() 3 | for i in range(0,int(t)): 4 | n,k = map(int, raw_input().strip().split(" ")) 5 | cost = [int(j) for j in raw_input().strip().split(" ")] 6 | c = [0] * (k+1) 7 | for j in range(0,n): 8 | for z in range(cost[j],k+1): 9 | c[z] = max(c[z], c[z-cost[j]]+cost[j]) 10 | print c[k] -------------------------------------------------------------------------------- /algorithms/warmup/solve-me-first/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int solveMeFirst(int a, int b) { 10 | // Hint: Type return a+b; below 11 | return a+b; 12 | } 13 | int main() { 14 | int num1, num2; 15 | int sum; 16 | cin>>num1>>num2; 17 | sum = solveMeFirst(num1,num2); 18 | cout< 2 | #include 3 | 4 | using namespace std; 5 | 6 | // Add your code here 7 | template 8 | void printArray(vector array) { 9 | for(T i : array){ 10 | cout << i << endl; 11 | } 12 | } 13 | 14 | int main() { 15 | 16 | vector vInt{1, 2, 3}; 17 | vector vString{"Hello", "World"}; 18 | 19 | printArray(vInt); 20 | printArray(vString); 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_7_arrays.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class day_7_arrays { 5 | 6 | public static void main(String[] args) { 7 | Scanner in = new Scanner(System.in); 8 | int n = in.nextInt(); 9 | int[] arr = new int[n]; 10 | for(int i=0; i < n; i++){ 11 | arr[i] = in.nextInt(); 12 | } 13 | in.close(); 14 | for (int j=n-1; j>=0; j--){ 15 | System.out.print(arr[j] + " "); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /data-structures/arrays/left-rotation/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 11 | int n,d; 12 | cin >> n >> d ; 13 | int array[n]; 14 | for(int i=0; i> array[(i+n-d)%n]; 16 | } 17 | for(int i=0; i q = new LinkedList(); 11 | q.add(root); 12 | while(!q.isEmpty()) { 13 | Node curr = q.remove(); 14 | if(curr != null) { 15 | System.out.print(curr.data + " "); 16 | q.add(curr.left); 17 | q.add(curr.right); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_25_running_time_and_complexity.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | # Enter your code here. Read input from STDIN. Print output to STDOUT 4 | def isPrime(n): 5 | if n<2: 6 | return False 7 | else: 8 | for x in xrange(2, int(math.sqrt(n))+1): 9 | if(n%x==0): 10 | return False 11 | return True 12 | 13 | t = int(input()) 14 | for i in xrange(t): 15 | n = int(input()) 16 | if(isPrime(n)): 17 | print "Prime" 18 | else: 19 | print "Not prime" -------------------------------------------------------------------------------- /data-structures/trees/tree-level-order-traversal/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | /* 3 | struct node 4 | { 5 | int data; 6 | node* left; 7 | node* right; 8 | }*/ 9 | 10 | void LevelOrder(node * root) 11 | { 12 | std::queue q; 13 | q.push(root); 14 | while(!q.empty()) { 15 | node * curr = q.front(); 16 | q.pop(); 17 | if(curr != NULL) { 18 | std::cout << curr->data << " "; 19 | q.push(curr->left); 20 | q.push(curr->right); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /10-days-of-statistics/day-1-quartiles/Solution.py: -------------------------------------------------------------------------------- 1 | def getMedian(a, start, end): 2 | median = 0 3 | size = end - start + 1 4 | if (size % 2 == 0): 5 | median = (a[start + size / 2 - 1] + a[start + size / 2]) / 2 6 | else: 7 | median = a[start + (size - 1) / 2] 8 | return median 9 | 10 | n = int(raw_input()) 11 | a = list(map(int, raw_input().split())) 12 | a.sort() 13 | print getMedian(a, 0, n / 2 - 1) 14 | print getMedian(a, 0, n - 1) 15 | if (n % 2 == 0): 16 | print getMedian(a, n / 2, n - 1) 17 | else: 18 | print getMedian(a, n / 2 + 1, n - 1) -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_27_testing.py: -------------------------------------------------------------------------------- 1 | print(5) 2 | print('4 3') 3 | print('0 -3 4 2') 4 | print('5 2') 5 | print('0 -3 4 2 2') 6 | print('3 3') 7 | print('0 -3 4') 8 | print('7 2') 9 | print('0 -3 1 1 1 1 1') 10 | print('6 3') 11 | print('0 -3 4 2 1 1') 12 | 13 | t = int(raw_input()); 14 | for i in range (0,t): 15 | n,k = map(int, raw_input().strip().split()) 16 | arrive = 0 17 | a = map(int,raw_input().split()) 18 | for j in range (0,n): 19 | if a[j] <=0: 20 | arrive += 1 21 | if arrive >= k: 22 | print "NO" 23 | else: 24 | print "YES" -------------------------------------------------------------------------------- /algorithms/warmup/save-the-prisoner/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 11 | int T; 12 | cin >> T; 13 | for(int i=0;i> N >> M >> S; 16 | int id = (S+M-1)%N; 17 | if(id == 0) 18 | cout << N << endl; 19 | else 20 | cout << id << endl; 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /data-structures/linked-lists/delete-duplicate-value-nodes-from-a-sorted-linked-list/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | Node is defined as 3 | class Node { 4 | int data; 5 | Node next; 6 | } 7 | */ 8 | 9 | Node RemoveDuplicates(Node head) { 10 | // This is a "method-only" submission. 11 | // You only need to complete this method. 12 | Node n = head; 13 | while(n.next != null) { 14 | if(n.data==n.next.data) 15 | { 16 | Node t = n.next; 17 | n.next= t.next; 18 | } 19 | else 20 | n = n.next; 21 | } 22 | return head; 23 | } -------------------------------------------------------------------------------- /data-structures/linked-lists/print_the_elements_of_a_linked_list.java: -------------------------------------------------------------------------------- 1 | /* 2 | Print elements of a linked list on console 3 | head pointer input could be NULL as well for empty list 4 | Node is defined as 5 | class Node { 6 | int data; 7 | Node next; 8 | } 9 | */ 10 | 11 | // This is a "method-only" submission. 12 | // You only need to complete this method. 13 | 14 | void Print(Node head) { 15 | if(head == null) return; 16 | else { 17 | while (head != null) { 18 | System.out.println(head.data); 19 | head = head.next; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /data-structures/trees/tree-top-view/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | class Node 3 | int data; 4 | Node left; 5 | Node right; 6 | */ 7 | void top_view(Node root) 8 | { 9 | Node curr = root; 10 | Stack stack = new Stack(); 11 | while(curr != null) { 12 | stack.add(curr.data); 13 | curr = curr.left; 14 | } 15 | while(!stack.empty()) { 16 | System.out.print(stack.pop() + " "); 17 | } 18 | curr = root; 19 | while(curr.right != null) { 20 | curr = curr.right; 21 | System.out.print(curr.data + " "); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /data-structures/linked-lists/cycle-detection/Solution.py: -------------------------------------------------------------------------------- 1 | """ 2 | Detect a cycle in a linked list. Note that the head pointer may be 'None' if the list is empty. 3 | 4 | A Node is defined as: 5 | 6 | class Node(object): 7 | def __init__(self, data = None, next_node = None): 8 | self.data = data 9 | self.next = next_node 10 | """ 11 | 12 | def has_cycle(head): 13 | fast = head 14 | slow = head 15 | while(fast != None and fast.next != None): 16 | fast = fast.next.next 17 | slow = slow.next 18 | if(slow == fast): 19 | return True 20 | return False -------------------------------------------------------------------------------- /data-structures/trees/tree-top-view/Solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | struct node 3 | { 4 | int data; 5 | node* left; 6 | node* right; 7 | }; 8 | 9 | */ 10 | 11 | void top_view(node * root) 12 | { 13 | node* curr = root; 14 | stack stack; 15 | while(curr != NULL) { 16 | stack.push(curr->data); 17 | curr = curr->left; 18 | } 19 | while(!stack.empty()) { 20 | cout << stack.top() << " "; 21 | stack.pop(); 22 | } 23 | curr = root; 24 | while(curr->right != NULL) { 25 | curr = curr->right; 26 | cout << curr->data << " "; 27 | } 28 | 29 | } -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_16_exceptions_string_to_integer.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class day_16_exceptions_string_to_integer { 8 | 9 | public static void main(String[] args) { 10 | Scanner in = new Scanner(System.in); 11 | String S = in.next(); 12 | try{ 13 | // write exception-throwing code here 14 | System.out.println(Integer.parseInt(S)); 15 | } 16 | catch(Exception e){ 17 | System.out.println("Bad String"); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_20_sorting.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | 3 | import sys 4 | 5 | 6 | n = int(raw_input().strip()) 7 | a = map(int,raw_input().strip().split(' ')) 8 | numSwaps = 0 9 | 10 | for i in range(n): 11 | currentSwaps = 0 12 | 13 | for j in range(0, n - 1): 14 | if a[j] > a[j + 1]: 15 | a[j], a[j + 1] = a[j + 1], a[j] 16 | currentSwaps += 1 17 | numSwaps += 1 18 | 19 | if currentSwaps == 0: 20 | break 21 | 22 | print("Array is sorted in " + str(numSwaps) + " swaps.") 23 | print("First Element: " + str(a[0])) 24 | print("Last Element: " + str(a[n - 1])) -------------------------------------------------------------------------------- /data-structures/linked-lists/get-node-value/Solution.py: -------------------------------------------------------------------------------- 1 | #Body 2 | """ 3 | Get Node data of the Nth Node from the end. 4 | head could be None as well for empty list 5 | Node is defined as 6 | 7 | class Node(object): 8 | 9 | def __init__(self, data=None, next_node=None): 10 | self.data = data 11 | self.next = next_node 12 | 13 | return back the node data of the linked list in the below method. 14 | """ 15 | 16 | def GetNode(head, position): 17 | p = head 18 | list = [] 19 | list.append(p.data) 20 | while(p.next!= None): 21 | p = p.next 22 | list.append(p.data) 23 | return list[len(list)-position-1] -------------------------------------------------------------------------------- /algorithms/warmup/solve-me-first/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | 9 | 10 | static int solveMeFirst(int a, int b) { 11 | // Hint: Type return a+b; below 12 | return a+b; 13 | } 14 | 15 | 16 | public static void main(String[] args) { 17 | Scanner in = new Scanner(System.in); 18 | int a; 19 | a = in.nextInt(); 20 | int b; 21 | b = in.nextInt(); 22 | int sum; 23 | sum = solveMeFirst(a, b); 24 | System.out.println(sum); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /data-structures/linked-lists/reverse-a-doubly-linked-list/Solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Reverse a doubly linked list, input list may also be empty 3 | Node is defined as 4 | struct Node 5 | { 6 | int data; 7 | Node *next; 8 | Node *prev; 9 | } 10 | */ 11 | Node* Reverse(Node* head) 12 | { 13 | Node *temp = NULL; 14 | Node *current = head; 15 | 16 | while (current != NULL) { 17 | temp = current->prev; 18 | current->prev = current->next; 19 | current->next = temp; 20 | current = current->prev; 21 | } 22 | 23 | if (temp != NULL) { 24 | head = temp->prev; 25 | } 26 | return head; 27 | } -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_9_recursion.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class day_9_recursion { 8 | private static int factorial(int n){ 9 | 10 | if(n > 1){ // recursive case 11 | return n * factorial(n - 1); 12 | } 13 | else{ // base case n = 1 14 | return 1; 15 | } 16 | } 17 | public static void main(String[] args) { 18 | Scanner scan =new Scanner(System.in); 19 | int n = scan.nextInt(); 20 | int sum = factorial(n); 21 | System.out.print(sum); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /data-structures/linked-lists/delete-duplicate-value-nodes-from-a-sorted-linked-list/Solution.py: -------------------------------------------------------------------------------- 1 | """ 2 | Delete duplicate nodes 3 | head could be None as well for empty list 4 | Node is defined as 5 | 6 | class Node(object): 7 | 8 | def __init__(self, data=None, next_node=None): 9 | self.data = data 10 | self.next = next_node 11 | 12 | return back the head of the linked list in the below method. 13 | """ 14 | 15 | def RemoveDuplicates(head): 16 | n = head 17 | while (n.next != None): 18 | if(n.data==n.next.data): 19 | t = n.next 20 | n.next= t.next 21 | else: 22 | n = n.next 23 | return head -------------------------------------------------------------------------------- /data-structures/trees/tree-huffman-decoding/Solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | The structure of the node is 3 | 4 | typedef struct node 5 | { 6 | int freq; 7 | char data; 8 | node * left; 9 | node * right; 10 | 11 | }node; 12 | 13 | */ 14 | 15 | 16 | void decode_huff(node * root,string s) 17 | { 18 | node * curr = root; 19 | for(int i=0; i< s.size(); i++) { 20 | char c = s[i]; 21 | if(c == '0') { 22 | curr = curr->left; 23 | } else { 24 | curr = curr->right; 25 | } 26 | if (curr->data != '\0') { 27 | cout << curr->data; 28 | curr = root; 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /data-structures/linked-lists/cycle-detection/Solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Detect a cycle in a linked list. Note that the head pointer may be 'NULL' if the list is empty. 3 | 4 | A Node is defined as: 5 | struct Node { 6 | int data; 7 | struct Node* next; 8 | } 9 | */ 10 | 11 | bool has_cycle(Node* head) { 12 | // Complete this function 13 | // Do not write the main method 14 | Node* fast = head; 15 | Node* slow = head; 16 | while(fast != NULL && fast->next != NULL) { 17 | fast = fast->next->next; 18 | slow = slow->next; 19 | if(slow == fast) { 20 | return true; 21 | } 22 | } 23 | return false; 24 | } -------------------------------------------------------------------------------- /data-structures/trees/tree-huffman-decoding/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | class Node 3 | public int frequency; // the frequency of this tree 4 | public char data; 5 | public Node left, right; 6 | 7 | */ 8 | 9 | void decode(String S ,Node root) 10 | { 11 | Node curr = root; 12 | for(char c: S.toCharArray()) { 13 | if(c == '0') { 14 | curr = curr.left; 15 | } else { 16 | curr = curr.right; 17 | } 18 | if(curr.data != '\0') { 19 | System.out.print(curr.data); 20 | curr = root; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /data-structures/linked-lists/get-node-value/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | Get Nth element from the end in a linked list of integers 3 | Number of elements in the list will always be greater than N. 4 | Node is defined as 5 | class Node { 6 | int data; 7 | Node next; 8 | } 9 | */ 10 | 11 | int GetNode(Node head,int n) { 12 | // This is a "method-only" submission. 13 | // You only need to complete this method. 14 | Node p = head; 15 | ArrayList list = new ArrayList(); 16 | list.add(p.data); 17 | while(p.next != null) { 18 | p = p.next; 19 | list.add(p.data); 20 | } 21 | return list.get(list.size()-n-1); 22 | } -------------------------------------------------------------------------------- /data-structures/linked-lists/delete-duplicate-value-nodes-from-a-sorted-linked-list/Solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Remove all duplicate elements from a sorted linked list 3 | Node is defined as 4 | struct Node 5 | { 6 | int data; 7 | struct Node *next; 8 | } 9 | */ 10 | Node* RemoveDuplicates(Node *head) 11 | { 12 | // This is a "method-only" submission. 13 | // You only need to complete this method. 14 | Node *n = head; 15 | while(n->next != NULL) { 16 | if(n->data==n->next->data) 17 | { 18 | Node *t = n->next; 19 | n->next= t->next; 20 | } 21 | else 22 | n = n->next; 23 | } 24 | return head; 25 | } -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_0_hello_world.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class day_0_hello_world { 8 | public static void main(String[] args) { 9 | Scanner scan = new Scanner(System.in); // use the Scanner class to read from stdin 10 | String inputString = scan.nextLine(); // read a line of input and save it to a variable 11 | scan.close(); // close the scanner 12 | 13 | // Your first line of output goes here 14 | System.out.println("Hello, World."); 15 | 16 | 17 | // Write the second line of output 18 | System.out.println(inputString); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /interview-preparation-kit/arrays/new-year-chaos/Solution.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the minimumBribes function below. 10 | def minimumBribes(q): 11 | result = 0 12 | for i in range(0, len(q)): 13 | if q[i] - (i + 1) > 2: 14 | print("Too chaotic") 15 | return 16 | for j in range(max(0, q[i] - 2), i): 17 | if q[j] > q[i]: 18 | result += 1 19 | print(result) 20 | 21 | if __name__ == '__main__': 22 | t = int(input()) 23 | 24 | for t_itr in range(t): 25 | n = int(input()) 26 | 27 | q = list(map(int, input().rstrip().split())) 28 | 29 | minimumBribes(q) -------------------------------------------------------------------------------- /10-days-of-statistics/day-0-weighted-mean/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | 9 | public static void main(String[] args) { 10 | Scanner scan = new Scanner(System.in); 11 | int n = scan.nextInt(); 12 | int [] x = new int[n]; 13 | for(int i=0; i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | // Declare a variable named 'input_string' to hold our input. 10 | string input_string; 11 | 12 | // Read a full line of input from stdin (cin) and save it to our variable, input_string. 13 | getline(cin, input_string); 14 | 15 | // Print a string literal saying "Hello, World." to stdout using cout. 16 | cout << "Hello, World." << endl; 17 | 18 | // TODO: Write a line of code here that prints the contents of input_string to stdout. 19 | cout << input_string << endl; 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /data-structures/linked-lists/compare-two -linked-lists/Solution.py: -------------------------------------------------------------------------------- 1 | #Body 2 | """ 3 | Compare two linked list 4 | head could be None as well for empty list 5 | Node is defined as 6 | 7 | class Node(object): 8 | 9 | def __init__(self, data=None, next_node=None): 10 | self.data = data 11 | self.next = next_node 12 | 13 | return back the head of the linked list in the below method. 14 | """ 15 | 16 | def CompareLists(headA, headB): 17 | nA = headA 18 | nB = headB 19 | while(nA != None and nB != None): 20 | if(nA.data != nB.data): 21 | return 0 22 | nA = nA.next 23 | nB = nB.next 24 | if(nA == None and nB == None): 25 | return 1 26 | else: 27 | return 0 -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_21_generics.java: -------------------------------------------------------------------------------- 1 | import java.lang.reflect.Method; 2 | 3 | class day_21_generics { 4 | //Write your code here 5 | public static void printArray (E[] inputArray) { 6 | for (E element: inputArray) { 7 | System.out.println(element); 8 | } 9 | } 10 | 11 | public static void main(String args[]){ 12 | Integer[] intArray = { 1, 2, 3 }; 13 | String[] stringArray = { "Hello", "World" }; 14 | 15 | printArray( intArray ); 16 | printArray( stringArray ); 17 | 18 | if(Solution.class.getDeclaredMethods().length > 2){ 19 | System.out.println("You should only have 1 method named printArray."); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /data-structures/linked-lists/compare-two -linked-lists/Solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Compare two linked lists A and B 3 | Return 1 if they are identical and 0 if they are not. 4 | Node is defined as 5 | struct Node 6 | { 7 | int data; 8 | struct Node *next; 9 | } 10 | */ 11 | int CompareLists(Node *headA, Node* headB) 12 | { 13 | // This is a "method-only" submission. 14 | // You only need to complete this method 15 | Node *nA = headA, *nB = headB; 16 | while(nA!=NULL && nB!=NULL) { 17 | if(nA->data != nB->data) { 18 | return 0; 19 | } 20 | nA = nA->next; 21 | nB = nB->next; 22 | } 23 | if(nA == NULL && nB == NULL) 24 | return 1; 25 | else 26 | return 0; 27 | } -------------------------------------------------------------------------------- /data-structures/linked-lists/get-node-value/Solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Get Nth element from the end in a linked list of integers 3 | Number of elements in the list will always be greater than N. 4 | Node is defined as 5 | struct Node 6 | { 7 | int data; 8 | struct Node *next; 9 | } 10 | */ 11 | int GetNode(Node *head,int positionFromTail) 12 | { 13 | // This is a "method-only" submission. 14 | // You only need to complete this method. 15 | Node *ptr1 = head; 16 | Node *ptr2 = head; 17 | int count = 0; 18 | while(ptr1->next != NULL) { 19 | ptr1 = ptr1->next; 20 | count++; 21 | if(count > positionFromTail) { 22 | ptr2 = ptr2->next; 23 | } 24 | } 25 | return ptr2->data; 26 | } -------------------------------------------------------------------------------- /data-structures/arrays/left-rotation/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | 9 | public static void main(String[] args) { 10 | /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ 11 | Scanner scan = new Scanner(System.in); 12 | int n = scan.nextInt(); 13 | int d = scan.nextInt(); 14 | int[] array = new int[n]; 15 | for(int i=0; i 0) { 14 | int remainder = n%2; 15 | n = n/2; 16 | if (remainder == 1) { 17 | count++; 18 | if (count > max) 19 | max = count; 20 | } 21 | else 22 | count = 0; 23 | } 24 | System.out.println(max); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /cracking-the-coding-interview/bit-manipulation-lonely-integer/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | 9 | public static int lonelyInteger(int[] a) { 10 | int sum = 0; 11 | for(int i=0; i< a.length; i++) { 12 | sum ^= a[i]; 13 | } 14 | return sum; 15 | } 16 | 17 | public static void main(String[] args) { 18 | Scanner in = new Scanner(System.in); 19 | int n = in.nextInt(); 20 | int a[] = new int[n]; 21 | for(int a_i=0; a_i < n; a_i++){ 22 | a[a_i] = in.nextInt(); 23 | } 24 | System.out.println(lonelyInteger(a)); 25 | } 26 | } -------------------------------------------------------------------------------- /data-structures/linked-lists/delete_a_node.java: -------------------------------------------------------------------------------- 1 | /* 2 | Insert Node at the end of a linked list 3 | head pointer input could be NULL as well for empty list 4 | Node is defined as 5 | class Node { 6 | int data; 7 | Node next; 8 | } 9 | */ 10 | 11 | // This is a "method-only" submission. 12 | // You only need to complete this method. 13 | 14 | Node Delete(Node head, int position) { 15 | // Complete this method 16 | Node n = head; 17 | if (position == 0) { 18 | head = head.next; 19 | return head; 20 | } 21 | else { 22 | int p=0; 23 | while (p!=position-1){ 24 | n= n.next; 25 | p++; 26 | } 27 | n.next = n.next.next; 28 | return head; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /data-structures/linked-lists/print_in_reverse.java: -------------------------------------------------------------------------------- 1 | /* 2 | Insert Node at the end of a linked list 3 | head pointer input could be NULL as well for empty list 4 | Node is defined as 5 | class Node { 6 | int data; 7 | Node next; 8 | } 9 | */ 10 | // This is a "method-only" submission. 11 | // You only need to complete this method. 12 | 13 | void ReversePrint(Node head) { 14 | // This is a "method-only" submission. 15 | // You only need to complete this method. 16 | Node p = head; 17 | ArrayList list = new ArrayList(); 18 | while (p!=null){ 19 | list.add(p.data); 20 | p = p.next; 21 | } 22 | Collections.reverse(list); 23 | for(int i=0; i= 0; arr_i--){ 18 | if (arr_i == 0) { 19 | System.out.print(arr[arr_i]); 20 | } 21 | else { 22 | System.out.print(arr[arr_i] + " "); 23 | } 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /data-structures/linked-lists/reverse-a-doubly-linked-list/Solution.py: -------------------------------------------------------------------------------- 1 | """ 2 | Reverse a doubly linked list 3 | head could be None as well for empty list 4 | Node is defined as 5 | 6 | class Node(object): 7 | 8 | def __init__(self, data=None, next_node=None, prev_node = None): 9 | self.data = data 10 | self.next = next_node 11 | self.prev = prev_node 12 | 13 | return the head node of the updated list 14 | """ 15 | def Reverse(head): 16 | temp = None 17 | current = head 18 | 19 | while (current != None): 20 | temp = current.prev 21 | current.prev = current.next 22 | current.next = temp 23 | current = current.prev 24 | 25 | if (temp != None): 26 | head = temp.prev 27 | return head -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_26_nested_logic.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 11 | int day_a, month_a, year_a, day_e, month_e, year_e; 12 | cin >> day_a >> month_a >> year_a; 13 | cin >> day_e >> month_e >> year_e; 14 | int fine = 0; 15 | if (day_a > day_e && month_a == month_e && year_a == year_e) 16 | fine = (day_a - day_e) * 15; 17 | if (month_a > month_e && year_a == year_e) 18 | fine = (month_a - month_e) * 500; 19 | if (year_a > year_e) 20 | fine = 10000; 21 | cout << fine << endl; 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /functional-programming/recursion/computing-the-gcd/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Solution { 5 | 6 | public static void main(String[] args) { 7 | /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ 8 | Scanner scan = new Scanner(System.in); 9 | int a = scan.nextInt(); 10 | int b = scan.nextInt(); 11 | if(a == b) 12 | System.out.println(a); 13 | else { 14 | int x, y; 15 | if(a > b) { 16 | x = a; 17 | y = b; 18 | } 19 | else { 20 | x = b; 21 | y = a; 22 | } 23 | while(x!=y) { 24 | x = x-y; 25 | if(x 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | int main() { 11 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 12 | int t; 13 | cin >> t; 14 | for(int i=0;i> n >> k; 17 | int cost[n]; 18 | for(int j=0;j> cost[j]; 20 | } 21 | int c[k+1]; 22 | memset(c, 0, sizeof(c)); 23 | for(int j=0;j 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | 11 | int main() { 12 | int n; 13 | cin >> n; 14 | 15 | vector a(n); 16 | vector w(n); 17 | 18 | for (int i = 0; i < n; i++) { 19 | cin >> a[i]; 20 | } 21 | 22 | for (int i = 0; i < n; i++) { 23 | cin >> w[i]; 24 | } 25 | 26 | double sum = 0; 27 | double cnt = 0; 28 | 29 | for (int i = 0; i < n; i++) { 30 | sum += a[i] * w[i]; 31 | cnt += w[i]; 32 | } 33 | 34 | double wmean = sum / cnt; 35 | 36 | cout << fixed << setprecision(1) << wmean; 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /10-days-of-statistics/day-1-interquartile-range/Solution.py: -------------------------------------------------------------------------------- 1 | def getMedian(a, start, end): 2 | median = 0 3 | size = end - start + 1 4 | if (size % 2 == 0): 5 | median = (a[start + size / 2 - 1] + a[start + size / 2]) / 2 6 | else: 7 | median = a[start + (size - 1) / 2] 8 | return median 9 | 10 | n = int(raw_input()) 11 | a = list(map(int, raw_input().split())) 12 | f = list(map(int, raw_input().split())) 13 | for i in range(n): 14 | element = a[i] 15 | frequency = f[i] 16 | while(frequency > 1): 17 | a.append(element) 18 | frequency -= 1 19 | a.sort() 20 | n = len(a) 21 | Q1 = getMedian(a, 0, n / 2 - 1) 22 | Q2 = getMedian(a, 0, n - 1) 23 | if (n % 2 == 0): 24 | Q3 = getMedian(a, n / 2, n - 1) 25 | else: 26 | Q3 = getMedian(a, n / 2 + 1, n - 1) 27 | print "%0.1f" % (Q3 - Q1) -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_29_bitwise_and.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | 9 | public static void main(String[] args) { 10 | Scanner in = new Scanner(System.in); 11 | int t = in.nextInt(); 12 | for(int a0 = 0; a0 < t; a0++){ 13 | int n = in.nextInt(); 14 | int k = in.nextInt(); 15 | if(k%2==0) { 16 | if(((k-1)|k)<=n) { 17 | System.out.println(k-1); 18 | } 19 | else { 20 | System.out.println(k-2); 21 | } 22 | } 23 | else { 24 | System.out.println(k-1); 25 | } 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /data-structures/linked-lists/merge-two-sorted-linked-lists/Solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Merge two sorted lists A and B as one linked list 3 | Node is defined as 4 | struct Node 5 | { 6 | int data; 7 | struct Node *next; 8 | } 9 | */ 10 | Node* MergeLists(Node *headA, Node* headB) 11 | { 12 | // This is a "method-only" submission. 13 | // You only need to complete this method 14 | if(headA == NULL && headB == NULL) 15 | return NULL; 16 | else if (headA == NULL) 17 | return headB; 18 | else if (headB == NULL) { 19 | return headA; 20 | } 21 | else if (headA->data <= headB->data){ 22 | headA->next = MergeLists(headA->next, headB); 23 | return headA; 24 | } else { 25 | headB->next = MergeLists(headA, headB->next); 26 | return headB; 27 | } 28 | } -------------------------------------------------------------------------------- /interview-preparation-kit/arrays/2d-array/Solution.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the hourglassSum function below. 10 | def hourglassSum(arr): 11 | max_sum, temp = 0, 0 12 | for i in range(0, 4): 13 | for j in range(0, 4): 14 | temp = arr[i][j] + arr[i][j+1] + arr[i][j+2] + arr[i+1][j+1] + arr[i+2][j] + arr[i+2][j+1] + arr[i+2][j+2] 15 | if i ==0 and j == 0 or temp > max_sum: 16 | max_sum = temp 17 | return max_sum 18 | 19 | if __name__ == '__main__': 20 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 21 | 22 | arr = [] 23 | 24 | for _ in range(6): 25 | arr.append(list(map(int, input().rstrip().split()))) 26 | 27 | result = hourglassSum(arr) 28 | 29 | fptr.write(str(result) + '\n') 30 | 31 | fptr.close() -------------------------------------------------------------------------------- /data-structures/linked-lists/merge-two-sorted-linked-lists/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | Merge two linked lists 3 | head pointer input could be NULL as well for empty list 4 | Node is defined as 5 | class Node { 6 | int data; 7 | Node next; 8 | } 9 | */ 10 | 11 | Node MergeLists(Node headA, Node headB) { 12 | // This is a "method-only" submission. 13 | // You only need to complete this method 14 | if(headA == null && headB == null) 15 | return null; 16 | else if (headA == null) 17 | return headB; 18 | else if (headB == null) { 19 | return headA; 20 | } 21 | else if (headA.data <= headB.data){ 22 | headA.next = MergeLists(headA.next, headB); 23 | return headA; 24 | } else { 25 | headB.next = MergeLists(headA, headB.next); 26 | return headB; 27 | } 28 | } -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_16_exceptions_string_to_integer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | using namespace std; 25 | 26 | 27 | int main(){ 28 | string S; 29 | cin >> S; 30 | try { 31 | cout << std::stoi (S,nullptr,0) << endl; 32 | } 33 | catch (exception& e) { 34 | cout << "Bad String" << endl; 35 | } 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_3_intro_to_conditional_statements.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | public class day_3_intro_to_conditional_statements { 7 | 8 | public static void main(String[] args) { 9 | Scanner scan = new Scanner(System.in); 10 | int n = scan.nextInt(); 11 | scan.close(); 12 | String ans=""; 13 | 14 | // if 'n' is NOT evenly divisible by 2 (i.e.: n is odd) 15 | if(n%2==1){ 16 | ans = "Weird"; 17 | } 18 | else if (n%2 == 0){ 19 | if (n>=2 && n<=5) 20 | ans = "Not Weird"; 21 | if (n>=6 && n<=20) 22 | ans = "Weird"; 23 | if (n>20) 24 | ans = "Not Weird"; 25 | } 26 | System.out.println(ans); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_8_dictionaries_and_maps.java: -------------------------------------------------------------------------------- 1 | //Complete this code or write your own from scratch 2 | import java.util.*; 3 | import java.io.*; 4 | 5 | class day_8_dictionaries_and_maps { 6 | public static void main(String []argh){ 7 | Scanner in = new Scanner(System.in); 8 | int n = in.nextInt(); 9 | Map myMap = new HashMap(); 10 | for(int i = 0; i < n; i++){ 11 | String name = in.next(); 12 | int phone = in.nextInt(); 13 | myMap.put(name, phone); 14 | } 15 | while(in.hasNext()){ 16 | String s = in.next(); 17 | if (myMap.containsKey(s)) 18 | System.out.println(s+"="+myMap.get(s)); 19 | else 20 | System.out.println("Not found"); 21 | } 22 | in.close(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /interview-preparation-kit/warm-up-challenges/sock-merchant/Solution.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the sockMerchant function below. 10 | def sockMerchant(n, ar): 11 | hmap = {} 12 | result = 0 13 | for element in ar: 14 | if element not in hmap: 15 | hmap[element] = 1 16 | else: 17 | if hmap[element] == 0: 18 | hmap[element] = 1 19 | else: 20 | result += 1 21 | hmap[element] = 0 22 | return result 23 | 24 | if __name__ == '__main__': 25 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 26 | 27 | n = int(input()) 28 | 29 | ar = list(map(int, input().rstrip().split())) 30 | 31 | result = sockMerchant(n, ar) 32 | 33 | fptr.write(str(result) + '\n') 34 | 35 | fptr.close() 36 | -------------------------------------------------------------------------------- /data-structures/arrays/dynamic-array/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 11 | int N, Q; 12 | cin >> N >> Q; 13 | vector seqList[N]; 14 | int lastAns = 0; 15 | for(int i=0;i> choose >> x >> y; 18 | int pos = (x^lastAns)%N; 19 | if(choose == 1) { 20 | seqList[pos].push_back(y); 21 | } 22 | else if(choose == 2){ 23 | int index = y%((int)seqList[pos].size()); 24 | lastAns = seqList[pos][index]; 25 | cout << lastAns << endl; 26 | } 27 | } 28 | return 0; 29 | } -------------------------------------------------------------------------------- /data-structures/linked-lists/reverse-a-doubly-linked-list/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | Insert Node at the end of a linked list 3 | head pointer input could be NULL as well for empty list 4 | Node is defined as 5 | class Node { 6 | int data; 7 | Node next; 8 | Node prev; 9 | } 10 | */ 11 | 12 | Node Reverse(Node head) { 13 | Node temp = null; 14 | Node current = head; 15 | 16 | /* swap next and prev for all nodes of 17 | doubly linked list */ 18 | while (current != null) { 19 | temp = current.prev; 20 | current.prev = current.next; 21 | current.next = temp; 22 | current = current.prev; 23 | } 24 | 25 | /* Before changing head, check for the cases like empty 26 | list and list with only one node */ 27 | if (temp != null) { 28 | head = temp.prev; 29 | } 30 | return head; 31 | } -------------------------------------------------------------------------------- /data-structures/arrays/sparse_arrays.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class sparse_arrays { 5 | 6 | public static void main(String[] args) { 7 | /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ 8 | Scanner scan = new Scanner(System.in); 9 | int n = scan.nextInt(); 10 | String array[] = new String[n]; 11 | for (int i=0; i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | bool isPrime(int); 9 | 10 | 11 | int main() { 12 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 13 | int t,i; 14 | cin >> t; 15 | for(i=0;i> n; 18 | if(isPrime(n)) { 19 | cout << "Prime" << endl; 20 | } 21 | else 22 | cout << "Not prime" << endl; 23 | } 24 | return 0; 25 | } 26 | 27 | bool isPrime(int n) { 28 | if (n < 2) 29 | return false; 30 | else{ 31 | for(int i=2;i<=(int)sqrt(n);i++) { 32 | if(n%i==0) 33 | return false; 34 | } 35 | } 36 | return true; 37 | } -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_17_more_exceptions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | //Write your code here 8 | class Calculator { 9 | public: 10 | int power(int a,int b) { 11 | if(a<0 || b<0){ 12 | throw runtime_error("n and p should be non-negative"); 13 | } 14 | return (int)pow(a,b); 15 | } 16 | }; 17 | 18 | int main() 19 | { 20 | Calculator myCalculator=Calculator(); 21 | int T,n,p; 22 | cin>>T; 23 | while(T-->0){ 24 | if(scanf("%d %d",&n,&p)==2){ 25 | try{ 26 | int ans=myCalculator.power(n,p); 27 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | class AdvancedArithmetic{ 9 | public: 10 | virtual int divisorSum(int n)=0; 11 | }; 12 | 13 | //Write your code here 14 | class Calculator : public AdvancedArithmetic { 15 | public: 16 | int divisorSum(int n) { 17 | int sum = 0, i; 18 | for(i=1; i> n; 29 | AdvancedArithmetic *myCalculator = new Calculator(); 30 | int sum = myCalculator->divisorSum(n); 31 | cout << "I implemented: AdvancedArithmetic\n" << sum; 32 | return 0; 33 | } -------------------------------------------------------------------------------- /interview-preparation-kit/sorting/fraudulent-activity-notifications/Solution.py: -------------------------------------------------------------------------------- 1 | n, d = map(int, raw_input().split()) 2 | arr = map(int, raw_input().split()) 3 | 4 | dic = {} 5 | 6 | def find(idx): 7 | s = 0 8 | for i in xrange(0, 200): 9 | freq = 0 10 | if i in dic: 11 | freq = dic[i] 12 | s = s + freq 13 | if s>=idx: 14 | return i 15 | 16 | ans = 0 17 | for i in xrange(0, n): 18 | val = arr[i] 19 | 20 | if i>=d: 21 | med=find(d/2 + d%2) 22 | 23 | if d%2==0: 24 | ret = find(d/2+1) 25 | if val >=med + ret: 26 | ans = ans+1 27 | else: 28 | if val>=med*2: 29 | ans = ans + 1 30 | 31 | if val not in dic: dic[val] = 0 32 | dic[val] = dic[val] + 1 33 | 34 | #print i,dic 35 | if i>=d: 36 | prev = arr[i-d] 37 | dic[prev] = dic[prev]-1 38 | 39 | print ans -------------------------------------------------------------------------------- /cracking-the-coding-interview/stacks-balanced-brackets/Solution.py: -------------------------------------------------------------------------------- 1 | def is_matched(expression): 2 | stack = [] 3 | for next in expression: 4 | if next == '(' or next == '[' or next == '{': 5 | stack.append(next) 6 | 7 | if next == ')' or next == ']' or next == '}': 8 | if not stack: 9 | return False 10 | top = stack.pop() 11 | if next == ')' and top != '(': 12 | return False 13 | if next == ']' and top != '[': 14 | return False 15 | if next == '}' and top != '{': 16 | return False 17 | if not stack: 18 | return True 19 | 20 | return False 21 | 22 | t = int(raw_input().strip()) 23 | for a0 in xrange(t): 24 | expression = raw_input().strip() 25 | if is_matched(expression) == True: 26 | print "YES" 27 | else: 28 | print "NO" -------------------------------------------------------------------------------- /data-structures/linked-lists/find-merge-point-of-two-lists/Solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Find merge point of two linked lists 3 | Node is defined as 4 | struct Node 5 | { 6 | int data; 7 | Node* next; 8 | } 9 | */ 10 | int FindMergeNode(Node *headA, Node *headB) 11 | { 12 | // Complete this function 13 | // Do not write the main method. 14 | if(headA == NULL && headB == NULL) 15 | return -1; 16 | else { 17 | Node *currentA = headA; 18 | Node *currentB = headB; 19 | while(currentA != currentB) { 20 | if(currentA->next == NULL) 21 | currentA = headB; 22 | else 23 | currentA = currentA->next; 24 | if(currentB->next == NULL) 25 | currentB = headA; 26 | else 27 | currentB = currentB->next; 28 | } 29 | return currentA->data; 30 | } 31 | } -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_27_testing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | cout << "5" << endl; 7 | cout << "4 3" << endl; 8 | cout << "0 -3 4 2" << endl; 9 | cout << "5 2" << endl; 10 | cout << "0 -3 4 2 2" << endl; 11 | cout << "3 3" << endl; 12 | cout << "0 -3 4" << endl; 13 | cout << "7 2" << endl; 14 | cout << "0 -3 1 1 1 1 1" << endl; 15 | cout << "6 3" << endl; 16 | cout << "0 -3 4 2 1 1" << endl; 17 | 18 | int t, n, k; 19 | cin >> t; 20 | for (int i=0;i> n >> k; 22 | int arrive =0; 23 | int a[n]; 24 | for (int j=0;j> a[j]; 26 | if (a[j]<=0) 27 | arrive++; 28 | } 29 | if (arrive>=k) 30 | cout << "NO" << endl; 31 | else 32 | cout << "YES" << endl; 33 | } 34 | } -------------------------------------------------------------------------------- /10-days-of-statistics/day-1-quartiles/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int [] x = new int[n]; for(int i=0; i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 10 | int t; 11 | cin >> t; 12 | for(int i=0;i> n; 15 | unsigned long int NumberOfThree = ((n-1) - (n-1)%3)/3; 16 | unsigned long int sum_three = 3 * NumberOfThree * (NumberOfThree + 1) /2; 17 | unsigned long int NumberOfFive = ((n-1) - (n-1)%5)/5; 18 | unsigned long int sum_five = 5 * NumberOfFive * (NumberOfFive + 1) /2; 19 | unsigned long int NumberOfFifteen = ((n-1) - (n-1)%15)/15; 20 | unsigned long int sum_fifteen = 15 * NumberOfFifteen * (NumberOfFifteen + 1) /2; 21 | cout << sum_three + sum_five - sum_fifteen << endl; 22 | 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /interview-preparation-kit/warm-up-challenges/jumping-on-the-clouds/Solution.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the jumpingOnClouds function below. 10 | def jumpingOnClouds(c): 11 | count = 0 12 | i = 0 13 | while(i != len(c) - 1): 14 | if len(c) - 1 - i >= 2: 15 | if (c[i+1] == 0 and c[i+2] == 0) or (c[i+1] == 1 and c[i+2] == 0): 16 | i += 2 17 | count += 1 18 | elif c[i+1] == 0 and c[i+2] == 1: 19 | i += 1 20 | count += 1 21 | else: 22 | i += 1 23 | count += 1 24 | return count 25 | 26 | if __name__ == '__main__': 27 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 28 | 29 | n = int(input()) 30 | 31 | c = list(map(int, input().rstrip().split())) 32 | 33 | result = jumpingOnClouds(c) 34 | 35 | fptr.write(str(result) + '\n') 36 | 37 | fptr.close() -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_19_interfaces.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | interface AdvancedArithmetic{ 5 | int divisorSum(int n); 6 | } 7 | 8 | //Write your code here 9 | class Calculator implements AdvancedArithmetic { 10 | 11 | public int divisorSum(int n) { 12 | int sum = 0; 13 | for (int i=1; i< n+1; i++) { 14 | if (n%i==0) 15 | sum = sum + i; 16 | } 17 | return sum; 18 | } 19 | } 20 | 21 | class day_19_interfaces { 22 | 23 | public static void main(String[] args) { 24 | Scanner scan = new Scanner(System.in); 25 | int n = scan.nextInt(); 26 | scan.close(); 27 | 28 | AdvancedArithmetic myCalculator = new Calculator(); 29 | int sum = myCalculator.divisorSum(n); 30 | System.out.println("I implemented: " + myCalculator.getClass().getInterfaces()[0].getName() ); 31 | System.out.println(sum); 32 | } 33 | } -------------------------------------------------------------------------------- /interview-preparation-kit/warm-up-challenges/counting-valleys/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Complete the countingValleys function below. 6 | int countingValleys(int n, string s) { 7 | int count = 0, sealevel = 0; 8 | char char_array[n + 1]; 9 | strcpy(char_array, s.c_str()); 10 | for (int i = 0; i < n; i++) { 11 | if(char_array[i] == 'D') { 12 | sealevel--; 13 | } else { 14 | sealevel++; 15 | } 16 | if(sealevel == 0 && char_array[i] == 'U') { 17 | count++; 18 | } 19 | } 20 | return count; 21 | } 22 | 23 | int main() 24 | { 25 | ofstream fout(getenv("OUTPUT_PATH")); 26 | 27 | int n; 28 | cin >> n; 29 | cin.ignore(numeric_limits::max(), '\n'); 30 | 31 | string s; 32 | getline(cin, s); 33 | 34 | int result = countingValleys(n, s); 35 | 36 | fout << result << "\n"; 37 | 38 | fout.close(); 39 | 40 | return 0; 41 | } -------------------------------------------------------------------------------- /10-days-of-statistics/day-1-quartiles/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int getMedian(vector a, int start, int end) { 9 | int median = 0; 10 | int size = end - start + 1; 11 | if (size % 2 == 0) { 12 | median = (a[start + size / 2 - 1] + a[start + size / 2]) / 2; 13 | } 14 | else { 15 | median = a[start + (size - 1) / 2]; 16 | } 17 | return median; 18 | } 19 | 20 | int main() { 21 | int n; 22 | cin >> n; 23 | vector a(n); 24 | for (int i = 0; i < n; i++) 25 | cin >> a[i]; 26 | sort(a.begin(), a.end()); 27 | cout << getMedian(a, 0, n / 2 - 1) << endl; 28 | cout << getMedian(a, 0, n - 1) << endl; 29 | if (n % 2 == 0) { 30 | cout << getMedian(a, n / 2, n - 1) << endl; 31 | } 32 | else { 33 | cout << getMedian(a, n / 2 + 1, n - 1) << endl; 34 | } 35 | } -------------------------------------------------------------------------------- /data-structures/linked-lists/find-merge-point-of-two-lists/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | Insert Node at the end of a linked list 3 | head pointer input could be NULL as well for empty list 4 | Node is defined as 5 | class Node { 6 | int data; 7 | Node next; 8 | } 9 | */ 10 | int FindMergeNode(Node headA, Node headB) { 11 | // Complete this function 12 | // Do not write the main method. 13 | if (headA == null && headB == null) 14 | return -1; 15 | else { 16 | Node currentA = headA; 17 | Node currentB = headB; 18 | 19 | while(currentA != currentB) { 20 | if(currentA.next == null) 21 | currentA = headB; 22 | else 23 | currentA = currentA.next; 24 | 25 | if(currentB.next == null) 26 | currentB = headA; 27 | else 28 | currentB = currentB.next; 29 | } 30 | return currentA.data; 31 | } 32 | } -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_2_operators.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | public class day_2_operators { 7 | 8 | public static void main(String[] args) { 9 | Scanner scan = new Scanner(System.in); 10 | double mealCost = scan.nextDouble(); // original meal price 11 | int tipPercent = scan.nextInt(); // tip percentage 12 | int taxPercent = scan.nextInt(); // tax percentage 13 | scan.close(); 14 | 15 | // Write your calculation code here. 16 | double tip = mealCost * tipPercent / 100; 17 | double tax = mealCost * taxPercent / 100; 18 | // cast the result of the rounding operation to an int and save it as totalCost 19 | int totalCost = (int) Math.round(mealCost + tip + tax); 20 | 21 | // Print your result 22 | System.out.print("The total meal cost is " + totalCost + " dollars."); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_28_regex_patterns_and_intro_to_databases.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class day_28_regex_patterns_and_intro_to_databases { 8 | 9 | public static void main(String[] args) { 10 | Scanner in = new Scanner(System.in); 11 | int N = in.nextInt(); 12 | String emailRegEx = "[a-z]+@gmail.com"; 13 | Pattern p = Pattern.compile(emailRegEx); 14 | List list = new ArrayList(); 15 | for(int a0 = 0; a0 < N; a0++){ 16 | String firstName = in.next(); 17 | String emailID = in.next(); 18 | Matcher matcher = p.matcher(emailID); 19 | if (matcher.find()){ 20 | list.add(firstName); 21 | } 22 | } 23 | Collections.sort(list); 24 | for (String name : list){ 25 | System.out.println(name); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /interview-preparation-kit/warm-up-challenges/repeated-string/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Complete the repeatedString function below. 6 | long repeatedString(string s, long n) { 7 | int aCountInS = 0, aCountInTheLastS = 0; 8 | char char_array[s.length() + 1]; 9 | strcpy(char_array, s.c_str()); 10 | for(int i=0; i < s.length(); i++) { 11 | if (char_array[i] == 'a') { 12 | aCountInS++; 13 | if(i < n % s.length()) { 14 | aCountInTheLastS++; 15 | } 16 | } 17 | } 18 | return (n / s.length()) * aCountInS + aCountInTheLastS; 19 | } 20 | 21 | int main() 22 | { 23 | ofstream fout(getenv("OUTPUT_PATH")); 24 | 25 | string s; 26 | getline(cin, s); 27 | 28 | long n; 29 | cin >> n; 30 | cin.ignore(numeric_limits::max(), '\n'); 31 | 32 | long result = repeatedString(s, n); 33 | 34 | fout << result << "\n"; 35 | 36 | fout.close(); 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_11_2d_arrays.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class day_11_2d_arrays { 8 | 9 | public static void main(String[] args) { 10 | Scanner in = new Scanner(System.in); 11 | int arr[][] = new int[6][6]; 12 | for(int i=0; i < 6; i++){ 13 | for(int j=0; j < 6; j++){ 14 | arr[i][j] = in.nextInt(); 15 | } 16 | } 17 | int max_sum = 0, sum = 0; 18 | for(int i=0; i < 4; i++){ 19 | for(int j=0; j < 4; j++){ 20 | sum = arr[i][j] + arr[i][j+1] + arr[i][j+2] + arr[i+1][j+1] + arr[i+2][j] + arr[i+2][j+1] + arr[i+2][j+2]; 21 | if (i ==0 && j == 0) 22 | max_sum = sum; 23 | if (sum > max_sum) 24 | max_sum = sum; 25 | } 26 | } 27 | System.out.print(max_sum); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /algorithms/dynamic-programming/knapsack/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | 9 | public static void main(String[] args) { 10 | /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ 11 | Scanner scan = new Scanner(System.in); 12 | int t = scan.nextInt(); 13 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | using namespace std; 25 | 26 | int lonely_integer(vector < int > a) { 27 | int result = 0; 28 | for(int i=0; i < a.size(); i++) { 29 | result ^= a[i]; 30 | } 31 | return result; 32 | } 33 | 34 | int main(){ 35 | int n; 36 | cin >> n; 37 | vector a(n); 38 | for(int a_i = 0;a_i < n;a_i++){ 39 | cin >> a[a_i]; 40 | } 41 | cout << lonely_integer(a) << endl; 42 | return 0; 43 | } -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_29_bitwise_and.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | using namespace std; 25 | 26 | 27 | int main(){ 28 | int t; 29 | cin >> t; 30 | for(int a0 = 0; a0 < t; a0++){ 31 | int n; 32 | int k; 33 | cin >> n >> k; 34 | if(k%2==0) { 35 | if(((k-1)|k)<=n) { 36 | cout << k-1 << endl; 37 | } else { 38 | cout << k-2 << endl; 39 | } 40 | } else { 41 | cout << k-1 << endl; 42 | } 43 | } 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /cracking-the-coding-interview/arrays-left-rotation/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | 9 | public static int[] arrayLeftRotation(int[] a, int n, int k) { 10 | int [] rotated = new int[n]; 11 | for(int i=0; i map = new HashMap(); 28 | Node n = head; 29 | int i = 0; 30 | while(n != null) { 31 | if(!map.containsKey(n)) { 32 | map.put(n, i); 33 | i++; 34 | } 35 | else 36 | return true; 37 | n = n.next; 38 | } 39 | return false; 40 | } 41 | */ -------------------------------------------------------------------------------- /interview-preparation-kit/arrays/2d-array/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Complete the hourglassSum function below. 6 | int hourglassSum(vector> arr) { 7 | int max_sum = 0, temp = 0; 8 | for(int i=0; i < 4; i++){ 9 | for(int j=0; j < 4; j++){ 10 | temp = arr[i][j] + arr[i][j+1] + arr[i][j+2] + arr[i+1][j+1] + arr[i+2][j] + arr[i+2][j+1] + arr[i+2][j+2]; 11 | if (i ==0 && j == 0 || temp > max_sum) { 12 | max_sum = temp; 13 | } 14 | } 15 | } 16 | return max_sum; 17 | } 18 | 19 | int main() { 20 | ofstream fout(getenv("OUTPUT_PATH")); 21 | 22 | vector> arr(6); 23 | for (int i = 0; i < 6; i++) { 24 | arr[i].resize(6); 25 | 26 | for (int j = 0; j < 6; j++) { 27 | cin >> arr[i][j]; 28 | } 29 | 30 | cin.ignore(numeric_limits::max(), '\n'); 31 | } 32 | 33 | int result = hourglassSum(arr); 34 | 35 | fout << result << "\n"; 36 | 37 | fout.close(); 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_25_running_time_and_complexity.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class day_25_running_time_and_complexity { 8 | 9 | public static void main(String[] args) { 10 | /* Enter your code here. */ 11 | Scanner scan = new Scanner(System.in); 12 | int t = scan.nextInt(); 13 | for(int i=0; i max_sum) 25 | max_sum = temp; 26 | } 27 | } 28 | 29 | System.out.print(max_sum); 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /data-structures/trees/binary-search-tree-insertion/Solution.java: -------------------------------------------------------------------------------- 1 | /* Node is defined as : 2 | class Node 3 | int data; 4 | Node left; 5 | Node right; 6 | 7 | */ 8 | 9 | static Node Insert(Node root, int value) { 10 | if (root == null) { 11 | Node n = new Node(); 12 | n.data = value; 13 | return n; 14 | } 15 | Node curr = root; 16 | int comp = value - curr.data; 17 | while (comp < 0 && curr.left != null || comp > 0 && curr.right != null) { 18 | if (comp < 0) { 19 | curr = curr.left; 20 | } else { 21 | curr = curr.right; 22 | } 23 | comp = value - curr.data; 24 | } 25 | 26 | // curr points to the last node 27 | if (comp < 0) { 28 | Node newLeft = new Node(); 29 | newLeft.data = value; 30 | curr.left = newLeft; 31 | } else if (comp > 0) { 32 | Node newRight = new Node(); 33 | newRight.data = value; 34 | curr.right = newRight; 35 | } else { 36 | 37 | } 38 | 39 | return root; 40 | } -------------------------------------------------------------------------------- /linkedIn-placements/bitwise-and/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | using namespace std; 25 | 26 | 27 | int main(){ 28 | int t; 29 | cin >> t; 30 | for(int a0 = 0; a0 < t; a0++){ 31 | int n; 32 | int k; 33 | cin >> n >> k; 34 | if(k%2==0) { 35 | if(((k-1)|k)<=n) { 36 | cout << k-1 << endl; 37 | } 38 | else { 39 | cout << k-2 << endl; 40 | } 41 | } else { 42 | cout << k-1 << endl; 43 | } 44 | } 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_28_regex_patterns_and_intro_to_databases.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | using namespace std; 26 | 27 | 28 | int main(){ 29 | int N; 30 | cin >> N; 31 | regex e(".+@gmail.com"); 32 | multiset db; 33 | for(int a0 = 0; a0 < N; a0++){ 34 | string firstName; 35 | string emailID; 36 | cin >> firstName >> emailID; 37 | if(regex_match(emailID,e)) 38 | { 39 | db.insert(firstName); 40 | } 41 | } 42 | for(auto& it : db) 43 | { 44 | cout << it << endl; 45 | } 46 | return 0; 47 | } -------------------------------------------------------------------------------- /cracking-the-coding-interview/strings-making-anagrams/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | using namespace std; 25 | 26 | int number_needed(string a, string b) { 27 | vector freq(26); 28 | for(char c : a) { 29 | freq[c - 'a']++; 30 | } 31 | for(char c : b) { 32 | freq[c - 'a']--; 33 | } 34 | int deleteCount = 0; 35 | for (int val : freq) { 36 | deleteCount += abs(val); 37 | } 38 | return deleteCount; 39 | } 40 | 41 | int main(){ 42 | string a; 43 | cin >> a; 44 | string b; 45 | cin >> b; 46 | cout << number_needed(a, b) << endl; 47 | return 0; 48 | } -------------------------------------------------------------------------------- /data-structures/trees/binary-search-tree-lowest-common-ancestor/Solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Node is defined as 3 | 4 | typedef struct node 5 | { 6 | int data; 7 | node * left; 8 | node * right; 9 | }node; 10 | 11 | */ 12 | 13 | node * lca(node * root, int v1,int v2) { 14 | 15 | // Base case 16 | if (root == NULL) return NULL; 17 | 18 | // If either n1 or n2 matches with root's key, report 19 | // the presence by returning root (Note that if a key is 20 | // ancestor of other, then the ancestor key becomes LCA 21 | if (root->data == v1 || root->data == v2) 22 | return root; 23 | 24 | // Look for keys in left and right subtrees 25 | node *left_lca = lca(root->left, v1, v2); 26 | node *right_lca = lca(root->right, v1, v2); 27 | 28 | // If both of the above calls return Non-NULL, then one key 29 | // is present in once subtree and other is present in other, 30 | // So this node is the LCA 31 | if (left_lca && right_lca) return root; 32 | 33 | // Otherwise check if left subtree or right subtree is LCA 34 | return (left_lca != NULL)? left_lca: right_lca; 35 | } -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_26_nested_logic.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class day_26_nested_logic { 8 | 9 | public static void main(String[] args) { 10 | /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ 11 | Scanner scan= new Scanner(System.in); 12 | int day_a = scan.nextInt(); 13 | int month_a = scan.nextInt(); 14 | int year_a = scan.nextInt(); 15 | int day_e = scan.nextInt(); 16 | int month_e = scan.nextInt(); 17 | int year_e = scan.nextInt(); 18 | scan.close(); 19 | 20 | int fine = 0; 21 | if (day_a > day_e && month_a == month_e && year_a == year_e) { 22 | fine = (day_a - day_e) * 15; 23 | } 24 | if (month_a > month_e && year_a == year_e) { 25 | fine = (month_a - month_e)*500; 26 | } 27 | if (year_a > year_e) { 28 | fine = 10000; 29 | } 30 | System.out.println(fine); 31 | } 32 | } -------------------------------------------------------------------------------- /data-structures/linked-lists/inserting-a-node-into-a-sorted-doubly-linked-list/Solution.java: -------------------------------------------------------------------------------- 1 | /* 2 | Insert Node at the end of a linked list 3 | head pointer input could be NULL as well for empty list 4 | Node is defined as 5 | class Node { 6 | int data; 7 | Node next; 8 | Node prev; 9 | } 10 | */ 11 | 12 | Node SortedInsert(Node head,int data) { 13 | Node n = new Node(); 14 | n.data = data; 15 | 16 | if(head == null){ 17 | head = n; 18 | return head; 19 | } 20 | 21 | if(head.data > data){ 22 | n.next = head; 23 | head.prev = n; 24 | head = head.prev; 25 | return head; 26 | } 27 | 28 | Node hnext = head.next; 29 | Node h = head; 30 | 31 | while(hnext != null){ 32 | if(data < hnext.data){ 33 | h.next = n; 34 | n.prev= h; 35 | n.next = hnext; 36 | hnext.prev = n; 37 | return head; 38 | } 39 | hnext = hnext.next; 40 | h = h.next; 41 | } 42 | 43 | h.next = n; 44 | n.prev = h; 45 | 46 | return head; 47 | } 48 | -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_17_more_exceptions.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | //Write your code here 5 | class Calculator { 6 | public int power(int a ,int b) throws Exception { 7 | int result; 8 | if (a<0||b<0) 9 | throw new Exception("n and p should be non-negative"); 10 | else 11 | result = (int)Math.pow(a,b); 12 | return result; 13 | } 14 | } 15 | 16 | class day_17_more_exceptions{ 17 | 18 | public static void main(String []argh) 19 | { 20 | Scanner in = new Scanner(System.in); 21 | int T=in.nextInt(); 22 | while(T-->0) 23 | { 24 | int n = in.nextInt(); 25 | int p = in.nextInt(); 26 | Calculator myCalculator = new Calculator(); 27 | try 28 | { 29 | int ans=myCalculator.power(n,p); 30 | System.out.println(ans); 31 | 32 | } 33 | catch(Exception e) 34 | { 35 | System.out.println(e.getMessage()); 36 | } 37 | } 38 | 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /cracking-the-coding-interview/arrays-left-rotation/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | using namespace std; 25 | 26 | vector array_left_rotation(vector a, int n, int k) { 27 | vector rotated(n); 28 | for(int i=0; i> n >> k; 38 | vector a(n); 39 | for(int a_i = 0;a_i < n;a_i++){ 40 | cin >> a[a_i]; 41 | } 42 | vector output = array_left_rotation(a, n, k); 43 | for(int i = 0; i < n;i++) 44 | cout << output[i] << " "; 45 | cout << endl; 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /data-structures/linked-lists/inserting-a-node-into-a-sorted-doubly-linked-list/Solution.py: -------------------------------------------------------------------------------- 1 | """ 2 | Insert a node into a sorted doubly linked list 3 | head could be None as well for empty list 4 | Node is defined as 5 | 6 | class Node(object): 7 | 8 | def __init__(self, data=None, next_node=None, prev_node = None): 9 | self.data = data 10 | self.next = next_node 11 | self.prev = prev_node 12 | 13 | return the head node of the updated list 14 | """ 15 | def SortedInsert(head, data): 16 | n = Node() 17 | n.data = data 18 | 19 | if(head == None): 20 | head = n 21 | return head 22 | 23 | if(head.data > data): 24 | n.next = head 25 | head.prev = n 26 | head = head.prev 27 | return head 28 | 29 | hnext = head.next 30 | h = head 31 | 32 | while(hnext != None): 33 | if(data < hnext.data): 34 | h.next = n 35 | n.prev= h 36 | n.next = hnext 37 | hnext.prev = n 38 | return head 39 | hnext = hnext.next 40 | h = h.next 41 | 42 | h.next = n 43 | n.prev = h 44 | 45 | return head -------------------------------------------------------------------------------- /interview-preparation-kit/sorting/sorting-comparator/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Player { 4 | String name; 5 | int score; 6 | 7 | Player(String name, int score) { 8 | this.name = name; 9 | this.score = score; 10 | } 11 | } 12 | 13 | class Checker implements Comparator { 14 | // complete this method 15 | public int compare(Player a, Player b) { 16 | if(a.score == b.score){ 17 | return a.name.compareTo(b.name); 18 | } 19 | return ((Integer) b.score).compareTo(a.score); 20 | } 21 | } 22 | 23 | 24 | public class Solution { 25 | 26 | public static void main(String[] args) { 27 | Scanner scan = new Scanner(System.in); 28 | int n = scan.nextInt(); 29 | 30 | Player[] player = new Player[n]; 31 | Checker checker = new Checker(); 32 | 33 | for(int i = 0; i < n; i++){ 34 | player[i] = new Player(scan.next(), scan.nextInt()); 35 | } 36 | scan.close(); 37 | 38 | Arrays.sort(player, checker); 39 | for(int i = 0; i < player.length; i++){ 40 | System.out.printf("%s %s\n", player[i].name, player[i].score); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /10-days-of-statistics/day-0-mean-median-and-mode/Solution.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | def getMean(list): 3 | sum = 0 4 | for i in list: 5 | sum += i 6 | mean = float(sum) / len(list) 7 | return mean 8 | 9 | def getMedian(list): 10 | median = 0.0 11 | size = len(list) 12 | copy = list 13 | copy.sort() 14 | if(size % 2 == 0): 15 | median = float(copy[size//2 - 1] + copy[size//2]) / 2 16 | else: 17 | median = copy[(size-1)/2] 18 | return median 19 | 20 | def getMode(list): 21 | mode = 0 22 | size = len(list) 23 | count, max = 0, 0 24 | copy = list 25 | copy.sort() 26 | current = 0 27 | for i in copy: 28 | if (i == current): 29 | count += 1 30 | else: 31 | count = 1 32 | current = i 33 | if (count > max): 34 | max = count 35 | mode = i 36 | return mode 37 | 38 | # if it runs in Python 3, change raw_input() to input() 39 | size = int(raw_input()) 40 | a = list(map(int, raw_input().split())) 41 | print(getMean(a)) 42 | print(getMedian(a)) 43 | print(getMode(a)) 44 | -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_20_sorting.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class day_20_sorting { 8 | 9 | public static void main(String[] args) { 10 | Scanner in = new Scanner(System.in); 11 | int n = in.nextInt(); 12 | int a[] = new int[n]; 13 | for(int a_i=0; a_i < n; a_i++){ 14 | a[a_i] = in.nextInt(); 15 | } 16 | 17 | int numberOfSwaps = 0; 18 | for (int i = 0; i < n; i++) { 19 | for (int j = 0; j < n - 1; j++) { 20 | if (a[j] > a[j + 1]) { 21 | int tmp = a[j]; 22 | a[j] = a[j + 1]; 23 | a[j + 1] = tmp; 24 | numberOfSwaps++; 25 | } 26 | } 27 | if (numberOfSwaps == 0) { 28 | break; 29 | } 30 | } 31 | System.out.println("Array is sorted in " + numberOfSwaps + " swaps."); 32 | System.out.println("First Element: " + a[0]); 33 | System.out.println("Last Element: " + a[n-1]); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_13_abstract_classes.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | abstract class Book 3 | { 4 | String title; 5 | String author; 6 | Book(String t,String a){ 7 | title=t; 8 | author=a; 9 | } 10 | abstract void display(); 11 | 12 | 13 | } 14 | 15 | //Write MyBook Class 16 | class MyBook extends Book{ 17 | private int price; 18 | /** Parameterized Constuctor **/ 19 | MyBook(String title, String author, int price) { 20 | super(title, author); 21 | this.price = price; 22 | } 23 | 24 | void display(){ // abstract method implementation 25 | System.out.println("Title: " + title); 26 | System.out.println("Author: " + author); 27 | System.out.println("Price: " + price); 28 | } 29 | } 30 | 31 | public class day_13_abstract_classes 32 | { 33 | 34 | public static void main(String []args) 35 | { 36 | Scanner sc=new Scanner(System.in); 37 | String title=sc.nextLine(); 38 | String author=sc.nextLine(); 39 | int price=sc.nextInt(); 40 | Book new_novel=new MyBook(title,author,price); 41 | new_novel.display(); 42 | 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /data-structures/heap/qheap1.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class qheap1 { 8 | 9 | public static void main(String[] args) { 10 | /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ 11 | Scanner scan = new Scanner(System.in); 12 | int n = scan.nextInt(); 13 | PriorityQueue queue = new PriorityQueue(); 14 | for (int i=0; idata = value; 19 | return root; 20 | } 21 | node * curr = root; 22 | int comp = value - curr->data; 23 | while(comp < 0 && curr->left != NULL || comp > 0 && curr->right != NULL) { 24 | if(comp < 0) { 25 | curr = curr->left; 26 | } else { 27 | curr = curr->right; 28 | } 29 | comp = value - curr->data; 30 | } 31 | 32 | // curr points to the last node 33 | if(comp < 0) { 34 | node * newLeft = (node *)malloc(sizeof(node)); 35 | newLeft->data = value; 36 | curr->left = newLeft; 37 | } else if(comp > 0) { 38 | node * newRight = (node *)malloc(sizeof(node)); 39 | newRight->data = value; 40 | curr->right = newRight; 41 | } else { 42 | 43 | } 44 | 45 | return root; 46 | } 47 | -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_23_bst_level_order_traversal.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | class Node: 4 | def __init__(self,data): 5 | self.right=self.left=None 6 | self.data = data 7 | 8 | class Solution: 9 | def insert(self,root,data): 10 | if root==None: 11 | return Node(data) 12 | else: 13 | if data<=root.data: 14 | cur=self.insert(root.left,data) 15 | root.left=cur 16 | else: 17 | cur=self.insert(root.right,data) 18 | root.right=cur 19 | return root 20 | 21 | def levelOrder(self,root): 22 | #Write your code here 23 | if root != None: 24 | q = [root] 25 | while q: 26 | currentNode = q.pop() 27 | print currentNode.data , 28 | if (currentNode.left != None): 29 | q.insert(0,currentNode.left) 30 | if (currentNode.right != None): 31 | q.insert(0,currentNode.right) 32 | 33 | T=int(raw_input()) 34 | myTree=Solution() 35 | root=None 36 | for i in range(T): 37 | data=int(raw_input()) 38 | root=myTree.insert(root,data) 39 | myTree.levelOrder(root) -------------------------------------------------------------------------------- /data-structures/arrays/dynamic-array/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | 9 | public static void main(String[] args) { 10 | /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ 11 | Scanner scan = new Scanner(System.in); 12 | int N = scan.nextInt(); 13 | int Q = scan.nextInt(); 14 | ArrayList[] seqList = new ArrayList[N]; 15 | for(int i=0; i (); 17 | int lastAns = 0; 18 | for(int i=0;idata = data; 18 | 19 | if(head == NULL){ 20 | head = n; 21 | return head; 22 | } 23 | 24 | if(head->data > data){ 25 | n->next = head; 26 | head->prev = n; 27 | head = head->prev; 28 | return head; 29 | } 30 | 31 | Node* hnext = head->next; 32 | Node* h = head; 33 | 34 | while(hnext != NULL){ 35 | if(data < hnext->data){ 36 | h->next = n; 37 | n->prev= h; 38 | n->next = hnext; 39 | hnext->prev = n; 40 | return head; 41 | } 42 | hnext = hnext->next; 43 | h = h->next; 44 | } 45 | 46 | h->next = n; 47 | n->prev = h; 48 | 49 | return head; 50 | } -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_15_linked_list.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class Node { 5 | int data; 6 | Node next; 7 | Node(int d) { 8 | data = d; 9 | next = null; 10 | } 11 | } 12 | 13 | class day_15_linked_list { 14 | public static Node insert(Node head,int data) { 15 | //Complete this method 16 | Node n = new Node(data); 17 | Node i = head; 18 | if (head == null) 19 | return n; 20 | else { 21 | while (i.next!=null) { 22 | i = i.next; 23 | } 24 | } 25 | i.next = n; 26 | return head; 27 | } 28 | public static void display(Node head) { 29 | Node start = head; 30 | while(start != null) { 31 | System.out.print(start.data + " "); 32 | start = start.next; 33 | } 34 | } 35 | 36 | public static void main(String args[]) { 37 | Scanner sc = new Scanner(System.in); 38 | Node head = null; 39 | int N = sc.nextInt(); 40 | 41 | while(N-- > 0) { 42 | int ele = sc.nextInt(); 43 | head = insert(head,ele); 44 | } 45 | display(head); 46 | sc.close(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_18_queues_and_stacks.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | class Solution: 4 | def __init__(self): 5 | self.stack = [] 6 | self.queue = [] 7 | # Write your code here 8 | def pushCharacter(self,ch): 9 | self.stack.append(ch) 10 | def enqueueCharacter(self,ch): 11 | self.queue.append(ch) 12 | def popCharacter(self): 13 | return self.stack.pop() 14 | def dequeueCharacter(self): 15 | return self.queue.pop(0) 16 | 17 | # read the string s 18 | s=raw_input() 19 | #Create the Solution class object 20 | obj=Solution() 21 | 22 | l=len(s) 23 | # push/enqueue all the characters of string s to stack 24 | for i in range(l): 25 | obj.pushCharacter(s[i]) 26 | obj.enqueueCharacter(s[i]) 27 | 28 | isPalindrome=True 29 | ''' 30 | pop the top character from stack 31 | dequeue the first character from queue 32 | compare both the characters 33 | ''' 34 | for i in range(l / 2): 35 | if obj.popCharacter()!=obj.dequeueCharacter(): 36 | isPalindrome=False 37 | break 38 | #finally print whether string s is palindrome or not. 39 | if isPalindrome: 40 | sys.stdout.write ("The word, "+s+", is a palindrome.") 41 | else: 42 | sys.stdout.write ("The word, "+s+", is not a palindrome.") -------------------------------------------------------------------------------- /10-days-of-statistics/day-1-interquartile-range/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | double getMedian(vector a, int start, int end) { 10 | double median = 0; 11 | int size = end - start + 1; 12 | if (size % 2 == 0) { 13 | median = (a[start + size / 2 - 1] + a[start + size / 2]) / 2; 14 | } 15 | else { 16 | median = a[start + (size - 1) / 2]; 17 | } 18 | return median; 19 | } 20 | 21 | int main() { 22 | int n; 23 | cin >> n; 24 | vector a(n); 25 | for (int i = 0; i < n; i++) 26 | cin >> a[i]; 27 | for(int i = 0; i < n; i++) { 28 | int element = a[i]; 29 | int frequency; 30 | cin >> frequency; 31 | while(frequency-- > 1) { 32 | a.push_back(element); 33 | } 34 | } 35 | sort(a.begin(), a.end()); 36 | double Q1, Q2, Q3; 37 | n = a.size(); 38 | Q1 = getMedian(a, 0, n / 2 - 1); 39 | Q2 = getMedian(a, 0, n - 1); 40 | if (n % 2 == 0) { 41 | Q3 = getMedian(a, n / 2, n - 1); 42 | } else { 43 | Q3 = getMedian(a, n / 2 + 1, n - 1); 44 | } 45 | cout << fixed << setprecision(1) << Q3-Q1 << endl; 46 | 47 | return 0; 48 | } -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_14_scope.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | 8 | class Difference { 9 | private int[] elements; 10 | public int maximumDifference; 11 | // Add your code here 12 | public Difference(int[] elements) { 13 | this.elements = elements; 14 | this.maximumDifference = 0; 15 | } 16 | 17 | public void computeDifference() { 18 | int difference = 0; 19 | for (int i=0;i maximumDifference) 23 | maximumDifference = difference; 24 | } 25 | } 26 | } 27 | } // End of Difference class 28 | 29 | public class day_14_scope { 30 | 31 | public static void main(String[] args) { 32 | Scanner sc = new Scanner(System.in); 33 | int n = sc.nextInt(); 34 | int[] a = new int[n]; 35 | for (int i = 0; i < n; i++) { 36 | a[i] = sc.nextInt(); 37 | } 38 | sc.close(); 39 | 40 | Difference difference = new Difference(a); 41 | 42 | difference.computeDifference(); 43 | 44 | System.out.print(difference.maximumDifference); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /interview-preparation-kit/warm-up-challenges/counting-valleys/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.math.*; 3 | import java.security.*; 4 | import java.text.*; 5 | import java.util.*; 6 | import java.util.concurrent.*; 7 | import java.util.regex.*; 8 | 9 | public class Solution { 10 | 11 | // Complete the countingValleys function below. 12 | static int countingValleys(int n, String s) { 13 | int count = 0, seaLevel = 0; 14 | for(char c: s.toCharArray()) { 15 | if (c == 'D') { 16 | seaLevel--; 17 | } else if(c == 'U') { 18 | seaLevel++; 19 | } 20 | if (seaLevel == 0 & c == 'U') { 21 | count++; 22 | } 23 | } 24 | return count; 25 | } 26 | 27 | private static final Scanner scanner = new Scanner(System.in); 28 | 29 | public static void main(String[] args) throws IOException { 30 | BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); 31 | 32 | int n = scanner.nextInt(); 33 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 34 | 35 | String s = scanner.nextLine(); 36 | 37 | int result = countingValleys(n, s); 38 | 39 | bufferedWriter.write(String.valueOf(result)); 40 | bufferedWriter.newLine(); 41 | 42 | bufferedWriter.close(); 43 | 44 | scanner.close(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_12_inheritance.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Person { 4 | protected String firstName; 5 | protected String lastName; 6 | protected int idNumber; 7 | 8 | // Constructor 9 | Person(String firstName, String lastName, int identification){ 10 | this.firstName = firstName; 11 | this.lastName = lastName; 12 | this.idNumber = identification; 13 | } 14 | 15 | // Print person data 16 | public void printPerson(){ 17 | System.out.println( 18 | "Name: " + lastName + ", " + firstName 19 | + "\nID: " + idNumber); 20 | } 21 | 22 | } 23 | 24 | class Student extends Person{ 25 | private int[] testScores; 26 | Student(String firstName, String lastName, int identification, int[] testScores){ 27 | // explicit call to superclass constructor: 28 | super(firstName, lastName, identification); 29 | this.testScores = testScores; 30 | } 31 | 32 | public char calculate() { 33 | int sum = 0; 34 | for(int i = 0; i < testScores.length; i++){ 35 | sum += testScores[i]; 36 | } 37 | float avg = sum / testScores.length; 38 | if (avg >= 90 && avg <= 100) 39 | return 'O'; 40 | else if (avg >= 80 && avg < 90) 41 | return 'E'; 42 | else if (avg >= 70 && avg < 80) 43 | return 'A'; 44 | else if (avg >= 55 && avg < 70) 45 | return 'P'; 46 | else if (avg >= 40 && avg < 55) 47 | return 'D'; 48 | else 49 | return 'T'; 50 | 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_4_class_vs_instance.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class day_4_class_vs_instance { 5 | 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | int T = sc.nextInt(); 9 | for (int i = 0; i < T; i++) { 10 | int age = sc.nextInt(); 11 | Person p = new Person(age); 12 | p.amIOld(); 13 | for (int j = 0; j < 3; j++) { 14 | p.yearPasses(); 15 | } 16 | p.amIOld(); 17 | System.out.println(); 18 | } 19 | sc.close(); 20 | } 21 | } 22 | 23 | class Person { 24 | private int age; 25 | 26 | public Person(int initialAge) { 27 | // Add some more code to run some checks on initialAge 28 | if (initialAge > 0) 29 | this.age = initialAge; 30 | else { 31 | System.out.println("Age is not valid, setting age to 0."); 32 | this.age = 0; 33 | } 34 | } 35 | 36 | public void amIOld() { 37 | // Write code determining if this person's age is old and print the correct statement: 38 | if (age < 13) 39 | System.out.println("You are young."); 40 | else if ( age >=13 && age < 18) 41 | System.out.println("You are a teenager."); 42 | else 43 | System.out.println("You are old."); 44 | } 45 | 46 | public void yearPasses() { 47 | // Increment this person's age. 48 | this.age = age + 1; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /interview-preparation-kit/warm-up-challenges/repeated-string/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.math.*; 3 | import java.security.*; 4 | import java.text.*; 5 | import java.util.*; 6 | import java.util.concurrent.*; 7 | import java.util.regex.*; 8 | 9 | public class Solution { 10 | 11 | // Complete the repeatedString function below. 12 | static long repeatedString(String s, long n) { 13 | int aCountInS = 0, aCountInTheLastS = 0; 14 | for(int i=0; i < s.length(); i++) { 15 | if (s.charAt(i) == 'a') { 16 | aCountInS++; 17 | if(i < n % s.length()) { 18 | aCountInTheLastS++; 19 | } 20 | } 21 | } 22 | return (n / s.length()) * aCountInS + aCountInTheLastS; 23 | } 24 | 25 | private static final Scanner scanner = new Scanner(System.in); 26 | 27 | public static void main(String[] args) throws IOException { 28 | BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); 29 | 30 | String s = scanner.nextLine(); 31 | 32 | long n = scanner.nextLong(); 33 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 34 | 35 | long result = repeatedString(s, n); 36 | 37 | bufferedWriter.write(String.valueOf(result)); 38 | bufferedWriter.newLine(); 39 | 40 | bufferedWriter.close(); 41 | 42 | scanner.close(); 43 | } 44 | } -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_24_more_linked_lists.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self,data): 3 | self.data = data 4 | self.next = None 5 | 6 | class Solution: 7 | def insert(self,head,data): 8 | p = Node(data) 9 | if head==None: 10 | head=p 11 | elif head.next==None: 12 | head.next=p 13 | else: 14 | start=head 15 | while(start.next!=None): 16 | start=start.next 17 | start.next=p 18 | return head 19 | 20 | def display(self,head): 21 | current = head 22 | while current: 23 | print current.data, 24 | current = current.next 25 | 26 | def removeDuplicates(self,head): 27 | #Write your code here 28 | if head == None: 29 | return head 30 | elif head.next == None: 31 | return head 32 | else: 33 | node = head 34 | while (node.next != None): 35 | if node.data == node.next.data: 36 | node.next = node.next.next 37 | else: 38 | node = node.next 39 | return head 40 | 41 | mylist= Solution() 42 | T=int(input()) 43 | head=None 44 | for i in range(T): 45 | data=int(input()) 46 | head=mylist.insert(head,data) 47 | head=mylist.removeDuplicates(head) 48 | mylist.display(head); -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_20_sorting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | using namespace std; 25 | 26 | void swap(int* a, int* b) { 27 | int temp = *a; 28 | *a = *b; 29 | *b = temp; 30 | } 31 | 32 | 33 | int main(){ 34 | int n; 35 | cin >> n; 36 | vector a(n); 37 | for(int a_i = 0;a_i < n;a_i++){ 38 | cin >> a[a_i]; 39 | } 40 | int numberOfSwaps = 0; 41 | for (int i = 0; i < n; i++) { 42 | for (int j = 0; j < n - 1; j++) { 43 | if (a[j] > a[j + 1]) { 44 | swap(&a[j], &a[j+1]); 45 | numberOfSwaps++; 46 | } 47 | } 48 | if (numberOfSwaps == 0) { 49 | break; 50 | } 51 | } 52 | cout << "Array is sorted in " << numberOfSwaps << " swaps." << endl; 53 | cout << "First Element: " << a[0] << endl; 54 | cout << "Last Element: " << a[n-1] << endl; 55 | return 0; 56 | } -------------------------------------------------------------------------------- /project euler/project_euler_ 1.java: -------------------------------------------------------------------------------- 1 | /*************************** 2 | * This problem is a programming version of Problem 1 from projecteuler.net 3 | * 4 | ***************************/ 5 | 6 | import java.io.*; 7 | import java.util.*; 8 | import java.text.*; 9 | import java.math.*; 10 | import java.util.regex.*; 11 | 12 | public class project_euler_1 { 13 | 14 | public static void main(String[] args) { 15 | /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ 16 | Scanner scan = new Scanner(System.in); 17 | int t = scan.nextInt(); 18 | for (int i=0;i> 1; 27 | long sigma_of_five = 5 * totalNumberOfFive *(totalNumberOfFive + 1) >> 1; 28 | long sigma_of_fifteen = 15 * totalNumberOfFifteen *(totalNumberOfFifteen + 1) >> 1; 29 | System.out.println(sigma_of_three + sigma_of_five - sigma_of_fifteen); 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_27_testing.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class day_27_testing { 8 | 9 | public static void main(String[] args) { 10 | /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ 11 | System.out.println(5); 12 | System.out.println("4 3"); // YES 13 | System.out.println("0 -3 4 2"); 14 | System.out.println("5 2"); // NO 15 | System.out.println("0 -3 4 2 2"); 16 | System.out.println("3 3"); // YES 17 | System.out.println("0 -3 4"); 18 | System.out.println("7 2"); // NO 19 | System.out.println("0 -3 1 1 1 1 1"); 20 | System.out.println("6 3"); // YES 21 | System.out.println("0 -3 4 2 1 1"); 22 | 23 | Scanner scan = new Scanner(System.in); 24 | int t = scan.nextInt(); 25 | for (int i=0;i=k) 36 | System.out.println("NO"); 37 | else 38 | System.out.println("YES"); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_22_binary_search_trees.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | class Node{ 5 | Node left,right; 6 | int data; 7 | Node(int data){ 8 | this.data=data; 9 | left=right=null; 10 | } 11 | } 12 | 13 | class day_22_binary_search_trees { 14 | 15 | public static int getHeight(Node root){ 16 | //Write your code here 17 | if (root == null) return 0; 18 | else if (root.left == null && root.right == null) return 0; 19 | else { 20 | return Math.max(getHeight(root.left), getHeight(root.right)) + 1; 21 | } 22 | } 23 | 24 | public static Node insert(Node root,int data){ 25 | if(root==null){ 26 | return new Node(data); 27 | } 28 | else{ 29 | Node cur; 30 | if(data<=root.data){ 31 | cur=insert(root.left,data); 32 | root.left=cur; 33 | } 34 | else{ 35 | cur=insert(root.right,data); 36 | root.right=cur; 37 | } 38 | return root; 39 | } 40 | } 41 | 42 | public static void main(String args[]){ 43 | Scanner sc=new Scanner(System.in); 44 | int T=sc.nextInt(); 45 | Node root=null; 46 | while(T-->0){ 47 | int data=sc.nextInt(); 48 | root=insert(root,data); 49 | } 50 | int height=getHeight(root); 51 | System.out.println(height); 52 | } 53 | } -------------------------------------------------------------------------------- /interview-preparation-kit/sorting/sorting-bubble-sort/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.math.*; 3 | import java.security.*; 4 | import java.text.*; 5 | import java.util.*; 6 | import java.util.concurrent.*; 7 | import java.util.regex.*; 8 | 9 | public class Solution { 10 | 11 | // Complete the countSwaps function below. 12 | static void countSwaps(int[] a) { 13 | int count = 0; 14 | for (int i = 0; i < a.length - 1; i++) { 15 | for (int j = 0; j < a.length - i - 1; j++) { 16 | if (a[j] > a[j + 1]) { 17 | int temp = a[j]; 18 | a[j] = a[j+1]; 19 | a[j+1] = temp; 20 | count++; 21 | } 22 | } 23 | } 24 | System.out.printf("Array is sorted in %d swaps.\n", count); 25 | System.out.printf("First Element: %d\n", a[0]); 26 | System.out.printf("Last Element: %d", a[a.length - 1]); 27 | } 28 | 29 | private static final Scanner scanner = new Scanner(System.in); 30 | 31 | public static void main(String[] args) { 32 | int n = scanner.nextInt(); 33 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 34 | 35 | int[] a = new int[n]; 36 | 37 | String[] aItems = scanner.nextLine().split(" "); 38 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 39 | 40 | for (int i = 0; i < n; i++) { 41 | int aItem = Integer.parseInt(aItems[i]); 42 | a[i] = aItem; 43 | } 44 | 45 | countSwaps(a); 46 | 47 | scanner.close(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_22_binary_search_trees.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Node { 7 | public: 8 | int data; 9 | Node *left,*right; 10 | Node(int d){ 11 | data=d; 12 | left=right=NULL; 13 | } 14 | }; 15 | 16 | class Solution { 17 | public: 18 | Node* insert(Node* root, int data){ 19 | if(root==NULL){ 20 | return new Node(data); 21 | } 22 | else{ 23 | Node* cur; 24 | if(data<=root->data){ 25 | cur=insert(root->left,data); 26 | root->left=cur; 27 | } 28 | else{ 29 | cur=insert(root->right,data); 30 | root->right=cur; 31 | } 32 | return root; 33 | } 34 | } 35 | int getHeight(Node* root){ 36 | //Write your code here 37 | if (root == NULL) return 0; 38 | else if (root->left == NULL && root->right == NULL) return 0; 39 | else { 40 | return max(getHeight(root->left), getHeight(root->right)) + 1; 41 | } 42 | } 43 | };//End of Solution 44 | 45 | int main() { 46 | Solution myTree; 47 | Node* root=NULL; 48 | int T,data; 49 | cin>>T; 50 | while(T-->0){ 51 | cin>>data; 52 | root= myTree.insert(root,data); 53 | } 54 | int height= myTree.getHeight(root); 55 | cout< stack = new Stack(); 12 | for (int i = 0; i < expression.length(); i++) { 13 | char next = expression.charAt(i); 14 | 15 | if (next == '(' || next == '[' || next == '{') { 16 | stack.push(next ); 17 | } 18 | 19 | if (next == ')' || next == ']' || next == '}') { 20 | 21 | if(stack.empty()) { 22 | return false; 23 | } 24 | char top = stack.pop(); 25 | if(next == ')' && top != '(') { 26 | return false; 27 | } 28 | if(next == ']' && top != '[') { 29 | return false; 30 | } 31 | if(next == '}' && top != '{') { 32 | return false; 33 | } 34 | } 35 | } 36 | 37 | if(stack.empty()) 38 | return true; 39 | 40 | return false; 41 | } 42 | 43 | public static void main(String[] args) { 44 | Scanner in = new Scanner(System.in); 45 | int t = in.nextInt(); 46 | for(int a0 = 0; a0 < t; a0++) { 47 | String expression = in.next(); 48 | boolean answer = isBalanced(expression); 49 | if(answer) 50 | System.out.println("YES"); 51 | else System.out.println("NO"); 52 | } 53 | } 54 | } -------------------------------------------------------------------------------- /interview-preparation-kit/arrays/new-year-chaos/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.math.*; 3 | import java.security.*; 4 | import java.text.*; 5 | import java.util.*; 6 | import java.util.concurrent.*; 7 | import java.util.regex.*; 8 | 9 | public class Solution { 10 | 11 | // Complete the minimumBribes function below. 12 | static void minimumBribes(int[] q) { 13 | int result = 0; 14 | for(int i = 0; i< q.length; i++) { 15 | if(q[i] - (i + 1) > 2) { 16 | System.out.println("Too chaotic"); 17 | return; 18 | } 19 | for (int j = Math.max(0, q[i] - 2); j < i; j++) { 20 | if (q[j] > q[i]) { 21 | result++; 22 | } 23 | } 24 | } 25 | System.out.println(result); 26 | } 27 | 28 | private static final Scanner scanner = new Scanner(System.in); 29 | 30 | public static void main(String[] args) { 31 | int t = scanner.nextInt(); 32 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 33 | 34 | for (int tItr = 0; tItr < t; tItr++) { 35 | int n = scanner.nextInt(); 36 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 37 | 38 | int[] q = new int[n]; 39 | 40 | String[] qItems = scanner.nextLine().split(" "); 41 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 42 | 43 | for (int i = 0; i < n; i++) { 44 | int qItem = Integer.parseInt(qItems[i]); 45 | q[i] = qItem; 46 | } 47 | 48 | minimumBribes(q); 49 | } 50 | 51 | scanner.close(); 52 | } 53 | } -------------------------------------------------------------------------------- /10-days-of-statistics/day-1-interquartile-range/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | 9 | private static double getMedian(ArrayList list, int start, int end) { 10 | double median = 0; 11 | int size = end - start + 1; 12 | if(size % 2 == 0) 13 | median = (list.get(start + size/2 - 1) + list.get(start + size/2)) / 2; 14 | else { 15 | median = list.get(start + (size -1)/2); 16 | } 17 | return median; 18 | } 19 | 20 | public static void main(String[] args) { 21 | Scanner scan = new Scanner(System.in); 22 | int n = scan.nextInt(); 23 | 24 | ArrayList list = new ArrayList(); 25 | for(int i=0; i 1) { 32 | list.add(element); 33 | } 34 | } 35 | scan.close(); 36 | 37 | Collections.sort(list); 38 | 39 | double Q1, Q2, Q3; 40 | n = list.size(); 41 | Q1 = getMedian(list, 0, n / 2 - 1); 42 | Q2 = getMedian(list, 0, n - 1); 43 | if (n % 2 == 0) { 44 | Q3 = getMedian(list, n / 2, n - 1); 45 | } else { 46 | Q3 = getMedian(list, n / 2 + 1, n - 1); 47 | } 48 | 49 | System.out.println(Q3-Q1); 50 | } 51 | } -------------------------------------------------------------------------------- /interview-preparation-kit/sorting/mark-and-toys/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.math.*; 3 | import java.security.*; 4 | import java.text.*; 5 | import java.util.*; 6 | import java.util.concurrent.*; 7 | import java.util.regex.*; 8 | 9 | public class Solution { 10 | 11 | // Complete the maximumToys function below. 12 | static int maximumToys(int[] prices, int k) { 13 | int count = 0; 14 | Arrays.sort(prices); 15 | int i = 0; 16 | while(i < prices.length) { 17 | if(k >= prices[i]) { 18 | k = k - prices[i]; 19 | count++; 20 | } 21 | i++; 22 | } 23 | return count; 24 | } 25 | 26 | private static final Scanner scanner = new Scanner(System.in); 27 | 28 | public static void main(String[] args) throws IOException { 29 | BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); 30 | 31 | String[] nk = scanner.nextLine().split(" "); 32 | 33 | int n = Integer.parseInt(nk[0]); 34 | 35 | int k = Integer.parseInt(nk[1]); 36 | 37 | int[] prices = new int[n]; 38 | 39 | String[] pricesItems = scanner.nextLine().split(" "); 40 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 41 | 42 | for (int i = 0; i < n; i++) { 43 | int pricesItem = Integer.parseInt(pricesItems[i]); 44 | prices[i] = pricesItem; 45 | } 46 | 47 | int result = maximumToys(prices, k); 48 | 49 | bufferedWriter.write(String.valueOf(result)); 50 | bufferedWriter.newLine(); 51 | 52 | bufferedWriter.close(); 53 | 54 | scanner.close(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /interview-preparation-kit/arrays/2d-array/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.math.*; 3 | import java.security.*; 4 | import java.text.*; 5 | import java.util.*; 6 | import java.util.concurrent.*; 7 | import java.util.regex.*; 8 | 9 | public class Solution { 10 | 11 | // Complete the hourglassSum function below. 12 | static int hourglassSum(int[][] arr) { 13 | int max_sum = 0, temp = 0; 14 | for(int i=0; i < 4; i++){ 15 | for(int j=0; j < 4; j++){ 16 | temp = arr[i][j] + arr[i][j+1] + arr[i][j+2] + arr[i+1][j+1] + arr[i+2][j] + arr[i+2][j+1] + arr[i+2][j+2]; 17 | if (i ==0 && j == 0 || temp > max_sum) { 18 | max_sum = temp; 19 | } 20 | } 21 | } 22 | return max_sum; 23 | } 24 | 25 | private static final Scanner scanner = new Scanner(System.in); 26 | 27 | public static void main(String[] args) throws IOException { 28 | BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); 29 | 30 | int[][] arr = new int[6][6]; 31 | 32 | for (int i = 0; i < 6; i++) { 33 | String[] arrRowItems = scanner.nextLine().split(" "); 34 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 35 | 36 | for (int j = 0; j < 6; j++) { 37 | int arrItem = Integer.parseInt(arrRowItems[j]); 38 | arr[i][j] = arrItem; 39 | } 40 | } 41 | 42 | int result = hourglassSum(arr); 43 | 44 | bufferedWriter.write(String.valueOf(result)); 45 | bufferedWriter.newLine(); 46 | 47 | bufferedWriter.close(); 48 | 49 | scanner.close(); 50 | } 51 | } -------------------------------------------------------------------------------- /interview-preparation-kit/arrays/ctci-array-left-rotation/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.math.*; 3 | import java.security.*; 4 | import java.text.*; 5 | import java.util.*; 6 | import java.util.concurrent.*; 7 | import java.util.regex.*; 8 | 9 | public class Solution { 10 | 11 | // Complete the rotLeft function below. 12 | static int[] rotLeft(int[] a, int d) { 13 | int n = a.length; 14 | int [] rotated = new int[n]; 15 | for(int i=0; i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | double getMean(const vector &array) 9 | { 10 | int sum = 0; 11 | for (int i = 0; i < array.size(); ++i) 12 | sum += array[i]; 13 | double mean = (double)(sum) / array.size(); 14 | return mean; 15 | } 16 | 17 | double getMedian(const vector &array) { 18 | double median = 0; 19 | int size = array.size(); 20 | vector copy (array.size()); 21 | copy = array; 22 | sort (copy.begin(), copy.end()); 23 | if(size % 2 == 0) 24 | median = (double) (copy[size/2 - 1] + copy[size/2]) / 2; 25 | else { 26 | median = (double) copy[(size-1)/2]; 27 | } 28 | return median; 29 | } 30 | 31 | int getMode(const vector &array) { 32 | int mode = 0; 33 | int size = array.size(); 34 | int count = 0, max = 0; 35 | vector copy (array.size()); 36 | copy = array; 37 | sort (copy.begin(), copy.end()); 38 | int current = 0; 39 | for(int i = 0;i < size;i++) { 40 | if (copy[i] == current) { 41 | count++; 42 | } else { 43 | count = 1; 44 | current = copy[i]; 45 | } 46 | if (count > max) { 47 | max = count; 48 | mode = copy[i]; 49 | } 50 | } 51 | return mode; 52 | } 53 | 54 | int main() { 55 | int size; 56 | cin >> size; 57 | vector a(size); 58 | for (int i = 0;i < size;i++) 59 | cin >> a[i]; 60 | cout << getMean(a) << endl; 61 | cout << getMedian(a) << endl; 62 | cout << getMode(a) << endl; 63 | return 0; 64 | } -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_18_queues_and_stacks.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution { 8 | //Write your code here 9 | stack s; 10 | queue q; 11 | 12 | public: 13 | void pushCharacter(char ch) { 14 | s.push(ch); 15 | } 16 | 17 | void enqueueCharacter(char ch) { 18 | q.push(ch); 19 | } 20 | 21 | char popCharacter() { 22 | return s.top(); 23 | } 24 | 25 | char dequeueCharacter() { 26 | return q.front(); 27 | } 28 | }; 29 | 30 | int main() { 31 | // read the string s. 32 | string s; 33 | getline(cin, s); 34 | 35 | // create the Solution class object p. 36 | Solution obj; 37 | 38 | // push/enqueue all the characters of string s to stack. 39 | for (int i = 0; i < s.length(); i++) { 40 | obj.pushCharacter(s[i]); 41 | obj.enqueueCharacter(s[i]); 42 | } 43 | 44 | bool isPalindrome = true; 45 | 46 | // pop the top character from stack. 47 | // dequeue the first character from queue. 48 | // compare both the characters. 49 | for (int i = 0; i < s.length() / 2; i++) { 50 | if (obj.popCharacter() != obj.dequeueCharacter()) { 51 | isPalindrome = false; 52 | 53 | break; 54 | } 55 | } 56 | 57 | // finally print whether string s is palindrome or not. 58 | if (isPalindrome) { 59 | cout << "The word, " << s << ", is a palindrome."; 60 | } else { 61 | cout << "The word, " << s << ", is not a palindrome."; 62 | } 63 | 64 | return 0; 65 | } -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_23_bst_level_order_traversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | class Node{ 9 | public: 10 | int data; 11 | Node *left,*right; 12 | Node(int d){ 13 | data=d; 14 | left=right=NULL; 15 | } 16 | }; 17 | class Solution{ 18 | public: 19 | Node* insert(Node* root, int data){ 20 | if(root==NULL){ 21 | return new Node(data); 22 | } 23 | else{ 24 | Node* cur; 25 | if(data<=root->data){ 26 | cur=insert(root->left,data); 27 | root->left=cur; 28 | } 29 | else{ 30 | cur=insert(root->right,data); 31 | root->right=cur; 32 | } 33 | return root; 34 | } 35 | } 36 | 37 | void levelOrder(Node * root){ 38 | //Write your code here 39 | queue q; 40 | if(root) { 41 | q.push(root); 42 | while(!q.empty()) { 43 | Node *currentNode = q.front(); 44 | q.pop(); 45 | cout << currentNode->data << " "; 46 | if(currentNode->left) { 47 | q.push(currentNode->left); 48 | } 49 | if(currentNode->right) { 50 | q.push(currentNode->right); 51 | } 52 | } 53 | } 54 | } 55 | };//End of Solution 56 | 57 | int main(){ 58 | Solution myTree; 59 | Node* root=NULL; 60 | int T,data; 61 | cin>>T; 62 | while(T-->0){ 63 | cin>>data; 64 | root= myTree.insert(root,data); 65 | } 66 | myTree.levelOrder(root); 67 | return 0; 68 | } -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_24_more_linked_lists.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class Node{ 5 | int data; 6 | Node next; 7 | Node(int d){ 8 | data=d; 9 | next=null; 10 | } 11 | 12 | } 13 | 14 | class day_24_more_linked_lists 15 | { 16 | public static Node removeDuplicates(Node head) { 17 | //Write your code here 18 | if (head == null) return head; 19 | else if (head.next == null) return head; 20 | else { 21 | Node node = head; 22 | while (node.next != null) { 23 | if (node.data == node.next.data) 24 | node.next = node.next.next; 25 | else 26 | node = node.next; 27 | } 28 | return head; 29 | } 30 | } 31 | 32 | public static Node insert(Node head,int data) { 33 | Node p=new Node(data); 34 | if(head==null) 35 | head=p; 36 | else if(head.next==null) 37 | head.next=p; 38 | else 39 | { 40 | Node start=head; 41 | while(start.next!=null) 42 | start=start.next; 43 | start.next=p; 44 | 45 | } 46 | return head; 47 | } 48 | 49 | public static void display(Node head){ 50 | Node start=head; 51 | while(start!=null) 52 | { 53 | System.out.print(start.data+" "); 54 | start=start.next; 55 | } 56 | } 57 | 58 | public static void main(String args[]) { 59 | Scanner sc=new Scanner(System.in); 60 | Node head=null; 61 | int T=sc.nextInt(); 62 | while(T-->0){ 63 | int ele=sc.nextInt(); 64 | head=insert(head,ele); 65 | } 66 | head=removeDuplicates(head); 67 | display(head); 68 | } 69 | } -------------------------------------------------------------------------------- /cracking-the-coding-interview/stacks-balanced-brackets/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | using namespace std; 25 | 26 | bool is_balanced(string expression) { 27 | stack stack; 28 | for (int i = 0; i < expression.length(); i++) { 29 | char next = expression[i]; 30 | 31 | if (next == '(' || next == '[' || next == '{') { 32 | stack.push(next); 33 | } 34 | 35 | if (next == ')' || next == ']' || next == '}') { 36 | if(stack.empty()) { 37 | return false; 38 | } 39 | char top = stack.top(); 40 | stack.pop(); 41 | if(next == ')' && top != '(') { 42 | return false; 43 | } 44 | if(next == ']' && top != '[') { 45 | return false; 46 | } 47 | if(next == '}' && top != '{') { 48 | return false; 49 | } 50 | } 51 | } 52 | 53 | // Printing answer, write your code here 54 | if(stack.empty()) 55 | return true; 56 | 57 | return false; 58 | } 59 | 60 | int main(){ 61 | int t; 62 | cin >> t; 63 | for(int a0 = 0; a0 < t; a0++){ 64 | string expression; 65 | cin >> expression; 66 | bool answer = is_balanced(expression); 67 | if(answer) 68 | cout << "YES\n"; 69 | else cout << "NO\n"; 70 | } 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_23_bst_level_order_traversal.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class Node { 5 | Node left,right; 6 | int data; 7 | Node(int data){ 8 | this.data=data; 9 | left=right=null; 10 | } 11 | } 12 | 13 | class day_23_bst_level_order_traversal { 14 | static void levelOrder(Node root){ 15 | //Write your code here 16 | Queue queue = new LinkedList(); 17 | if (root != null) { 18 | queue.add(root); 19 | while (!queue.isEmpty()) { 20 | Node currentNode = queue.remove(); 21 | System.out.print(currentNode.data + " "); 22 | if (currentNode.left != null) 23 | queue.add(currentNode.left); 24 | if (currentNode.right != null) 25 | queue.add(currentNode.right); 26 | } 27 | } 28 | } 29 | 30 | public static Node insert(Node root,int data){ 31 | if(root==null){ 32 | return new Node(data); 33 | } 34 | else{ 35 | Node cur; 36 | if(data<=root.data){ 37 | cur=insert(root.left,data); 38 | root.left=cur; 39 | } 40 | else{ 41 | cur=insert(root.right,data); 42 | root.right=cur; 43 | } 44 | return root; 45 | } 46 | } 47 | 48 | public static void main(String args[]){ 49 | Scanner sc=new Scanner(System.in); 50 | int T=sc.nextInt(); 51 | Node root=null; 52 | while(T-->0){ 53 | int data=sc.nextInt(); 54 | root=insert(root,data); 55 | } 56 | levelOrder(root); 57 | } 58 | } -------------------------------------------------------------------------------- /interview-preparation-kit/warm-up-challenges/sock-merchant/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.math.*; 3 | import java.security.*; 4 | import java.text.*; 5 | import java.util.*; 6 | import java.util.concurrent.*; 7 | import java.util.regex.*; 8 | 9 | public class Solution { 10 | 11 | // Complete the sockMerchant function below. 12 | static int sockMerchant(int n, int[] ar) { 13 | HashMap hmap = new HashMap(); 14 | int result = 0; 15 | for (int element: ar) { 16 | if (hmap.get(element) == null || !hmap.containsKey(element)) { 17 | hmap.put(element, 1); 18 | } else { 19 | if(hmap.get(element) == 0) { 20 | hmap.put(element, 1); 21 | } else { 22 | result += 1; 23 | hmap.put(element, 0); 24 | } 25 | } 26 | } 27 | return result; 28 | } 29 | 30 | private static final Scanner scanner = new Scanner(System.in); 31 | 32 | public static void main(String[] args) throws IOException { 33 | BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); 34 | 35 | int n = scanner.nextInt(); 36 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 37 | 38 | int[] ar = new int[n]; 39 | 40 | String[] arItems = scanner.nextLine().split(" "); 41 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 42 | 43 | for (int i = 0; i < n; i++) { 44 | int arItem = Integer.parseInt(arItems[i]); 45 | ar[i] = arItem; 46 | } 47 | 48 | int result = sockMerchant(n, ar); 49 | 50 | bufferedWriter.write(String.valueOf(result)); 51 | bufferedWriter.newLine(); 52 | 53 | bufferedWriter.close(); 54 | 55 | scanner.close(); 56 | } 57 | } -------------------------------------------------------------------------------- /interview-preparation-kit/warm-up-challenges/jumping-on-the-clouds/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.math.*; 3 | import java.security.*; 4 | import java.text.*; 5 | import java.util.*; 6 | import java.util.concurrent.*; 7 | import java.util.regex.*; 8 | 9 | public class Solution { 10 | 11 | // Complete the jumpingOnClouds function below. 12 | // 000, 001, 010, 00 13 | static int jumpingOnClouds(int[] c) { 14 | int count = 0, i = 0; 15 | while(i != c.length -1) { 16 | if(c.length - 1 - i >= 2) { 17 | if ((c[i+1] == 0 && c[i+2] == 0) || (c[i+1] == 1 && c[i+2] == 0)) { 18 | i+=2; 19 | count++; 20 | } else if(c[i+1] == 0 && c[i+2] == 1) { 21 | i++; 22 | count++; 23 | } 24 | } else { 25 | i++; 26 | count++; 27 | } 28 | } 29 | return count; 30 | } 31 | 32 | private static final Scanner scanner = new Scanner(System.in); 33 | 34 | public static void main(String[] args) throws IOException { 35 | BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); 36 | 37 | int n = scanner.nextInt(); 38 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 39 | 40 | int[] c = new int[n]; 41 | 42 | String[] cItems = scanner.nextLine().split(" "); 43 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 44 | 45 | for (int i = 0; i < n; i++) { 46 | int cItem = Integer.parseInt(cItems[i]); 47 | c[i] = cItem; 48 | } 49 | 50 | int result = jumpingOnClouds(c); 51 | 52 | bufferedWriter.write(String.valueOf(result)); 53 | bufferedWriter.newLine(); 54 | 55 | bufferedWriter.close(); 56 | 57 | scanner.close(); 58 | } 59 | } -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_18_queues_and_stacks.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class Solution { 5 | // Write your code here. 6 | private LinkedList stack = new LinkedList(); 7 | private LinkedList queue = new LinkedList(); 8 | 9 | void pushCharacter(char ch) { 10 | stack.addLast(ch); 11 | } 12 | 13 | void enqueueCharacter(char ch) { 14 | queue.addLast(ch); 15 | } 16 | 17 | char popCharacter() { 18 | return stack.removeLast(); 19 | } 20 | 21 | char dequeueCharacter() { 22 | return queue.remove(0); 23 | } 24 | } 25 | 26 | public class day_18_queues_and_stacks { 27 | 28 | public static void main(String[] args) { 29 | Scanner scan = new Scanner(System.in); 30 | String input = scan.nextLine(); 31 | scan.close(); 32 | 33 | // Convert input String to an array of characters: 34 | char[] s = input.toCharArray(); 35 | 36 | // Create a Solution object: 37 | Solution p = new Solution(); 38 | 39 | // Enqueue/Push all chars to their respective data structures: 40 | for (char c : s) { 41 | p.pushCharacter(c); 42 | p.enqueueCharacter(c); 43 | } 44 | 45 | // Pop/Dequeue the chars at the head of both data structures and compare them: 46 | boolean isPalindrome = true; 47 | for (int i = 0; i < s.length/2; i++) { 48 | if (p.popCharacter() != p.dequeueCharacter()) { 49 | isPalindrome = false; 50 | break; 51 | } 52 | } 53 | 54 | //Finally, print whether string s is palindrome or not. 55 | System.out.println( "The word, " + input + ", is " 56 | + ( (!isPalindrome) ? "not a palindrome." : "a palindrome." ) ); 57 | } 58 | } -------------------------------------------------------------------------------- /30-days-of-code-challenges/day_24_more_linked_lists.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | class Node 11 | { 12 | public: 13 | int data; 14 | Node *next; 15 | Node(int d){ 16 | data=d; 17 | next=NULL; 18 | } 19 | }; 20 | 21 | class Solution{ 22 | public: 23 | Node* removeDuplicates(Node *head) 24 | { 25 | //Write your code here 26 | if (head == NULL) return head; 27 | else if (head->next == NULL) return head; 28 | else { 29 | Node *node = head; 30 | while (node->next != NULL) { 31 | if (node->data == node->next->data) 32 | node->next = node->next->next; 33 | else 34 | node = node->next; 35 | } 36 | return head; 37 | } 38 | } 39 | 40 | Node* insert(Node *head,int data) 41 | { 42 | Node* p=new Node(data); 43 | if(head==NULL){ 44 | head=p; 45 | } 46 | else if(head->next==NULL){ 47 | head->next=p; 48 | } 49 | else{ 50 | Node *start=head; 51 | while(start->next!=NULL){ 52 | start=start->next; 53 | } 54 | start->next=p; 55 | } 56 | return head; 57 | } 58 | 59 | void display(Node *head) 60 | { 61 | Node *start=head; 62 | while(start) 63 | { 64 | cout<data<<" "; 65 | start=start->next; 66 | } 67 | } 68 | }; 69 | 70 | int main() 71 | { 72 | Node* head=NULL; 73 | Solution mylist; 74 | int T,data; 75 | cin>>T; 76 | while(T-->0){ 77 | cin>>data; 78 | head=mylist.insert(head,data); 79 | } 80 | head=mylist.removeDuplicates(head); 81 | mylist.display(head); 82 | } -------------------------------------------------------------------------------- /10-days-of-statistics/day-0-mean-median-and-mode/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | 9 | public static void main(String[] args) { 10 | Scanner s = new Scanner(System.in); 11 | int size = s.nextInt(); 12 | int [] a = new int[size]; 13 | for(int i = 0;i < size;i++) { 14 | a[i] = s.nextInt(); 15 | } 16 | System.out.println(getMean(a)); 17 | System.out.println(getMedian(a)); 18 | System.out.println(getMode(a)); 19 | } 20 | 21 | private static double getMean(int[] a) { 22 | double mean = 0; 23 | int sum = 0; 24 | int size = a.length; 25 | for(int i : a) { 26 | sum += i; 27 | } 28 | mean = (double) sum/size; 29 | return mean; 30 | } 31 | 32 | private static double getMedian(int[] a) { 33 | double median = 0; 34 | int size = a.length; 35 | int [] copy = a.clone(); 36 | Arrays.sort(copy); 37 | if(size % 2 == 0) 38 | median = (double) (copy[size/2 - 1] + copy[size/2]) / 2; 39 | else { 40 | median = (double) copy[(size-1)/2]; 41 | } 42 | return median; 43 | } 44 | 45 | private static int getMode(int[] a) { 46 | int mode = 0; 47 | int size = a.length; 48 | int [] copy = a.clone(); 49 | Arrays.sort(copy); 50 | int count = 0, max = 0; 51 | int current = copy[0]; 52 | for(int i = 0;i < size;i++) { 53 | if (copy[i] == current) { 54 | count++; 55 | } else { 56 | count = 1; 57 | current = copy[i]; 58 | } 59 | if (count > max) { 60 | max = count; 61 | mode = copy[i]; 62 | } 63 | } 64 | return mode; 65 | } 66 | } -------------------------------------------------------------------------------- /interview-preparation-kit/arrays/ctci-array-left-rotation/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | vector split_string(string); 6 | 7 | // Complete the rotLeft function below. 8 | vector rotLeft(vector a, int d) { 9 | int n = a.size(); 10 | vector rotated(n); 11 | for(int i=0; i nd = split_string(nd_temp); 26 | 27 | int n = stoi(nd[0]); 28 | 29 | int d = stoi(nd[1]); 30 | 31 | string a_temp_temp; 32 | getline(cin, a_temp_temp); 33 | 34 | vector a_temp = split_string(a_temp_temp); 35 | 36 | vector a(n); 37 | 38 | for (int i = 0; i < n; i++) { 39 | int a_item = stoi(a_temp[i]); 40 | 41 | a[i] = a_item; 42 | } 43 | 44 | vector result = rotLeft(a, d); 45 | 46 | for (int i = 0; i < result.size(); i++) { 47 | fout << result[i]; 48 | 49 | if (i != result.size() - 1) { 50 | fout << " "; 51 | } 52 | } 53 | 54 | fout << "\n"; 55 | 56 | fout.close(); 57 | 58 | return 0; 59 | } 60 | 61 | vector split_string(string input_string) { 62 | string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) { 63 | return x == y and x == ' '; 64 | }); 65 | 66 | input_string.erase(new_end, input_string.end()); 67 | 68 | while (input_string[input_string.length() - 1] == ' ') { 69 | input_string.pop_back(); 70 | } 71 | 72 | vector splits; 73 | char delimiter = ' '; 74 | 75 | size_t i = 0; 76 | size_t pos = input_string.find(delimiter); 77 | 78 | while (pos != string::npos) { 79 | splits.push_back(input_string.substr(i, pos - i)); 80 | 81 | i = pos + 1; 82 | pos = input_string.find(delimiter, i); 83 | } 84 | 85 | splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1)); 86 | 87 | return splits; 88 | } -------------------------------------------------------------------------------- /interview-preparation-kit/arrays/new-year-chaos/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | vector split_string(string); 6 | 7 | // Complete the minimumBribes function below. 8 | void minimumBribes(vector q) { 9 | int result = 0; 10 | for(int i = 0; i < q.size(); i++){ 11 | if(q[i] - (i + 1) > 2) { 12 | cout << "Too chaotic" << endl; 13 | return; 14 | } 15 | for (int j = std::max(0, q[i] - 2); j < i; j++) { 16 | if (q[j] > q[i]) { 17 | result++; 18 | } 19 | } 20 | } 21 | cout << result << endl; 22 | } 23 | 24 | int main() 25 | { 26 | int t; 27 | cin >> t; 28 | cin.ignore(numeric_limits::max(), '\n'); 29 | 30 | for (int t_itr = 0; t_itr < t; t_itr++) { 31 | int n; 32 | cin >> n; 33 | cin.ignore(numeric_limits::max(), '\n'); 34 | 35 | string q_temp_temp; 36 | getline(cin, q_temp_temp); 37 | 38 | vector q_temp = split_string(q_temp_temp); 39 | 40 | vector q(n); 41 | 42 | for (int i = 0; i < n; i++) { 43 | int q_item = stoi(q_temp[i]); 44 | 45 | q[i] = q_item; 46 | } 47 | 48 | minimumBribes(q); 49 | } 50 | 51 | return 0; 52 | } 53 | 54 | vector split_string(string input_string) { 55 | string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) { 56 | return x == y and x == ' '; 57 | }); 58 | 59 | input_string.erase(new_end, input_string.end()); 60 | 61 | while (input_string[input_string.length() - 1] == ' ') { 62 | input_string.pop_back(); 63 | } 64 | 65 | vector splits; 66 | char delimiter = ' '; 67 | 68 | size_t i = 0; 69 | size_t pos = input_string.find(delimiter); 70 | 71 | while (pos != string::npos) { 72 | splits.push_back(input_string.substr(i, pos - i)); 73 | 74 | i = pos + 1; 75 | pos = input_string.find(delimiter, i); 76 | } 77 | 78 | splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1)); 79 | 80 | return splits; 81 | } -------------------------------------------------------------------------------- /interview-preparation-kit/warm-up-challenges/jumping-on-the-clouds/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | vector split_string(string); 6 | 7 | // Complete the jumpingOnClouds function below. 8 | // 000, 001, 010, 00 9 | int jumpingOnClouds(vector c) { 10 | int count = 0, i = 0; 11 | while(i != c.size() -1) { 12 | if(c.size() - 1 - i >= 2) { 13 | if ((c.at(i+1) == 0 && c.at(i+2) == 0) || (c.at(i+1) == 1 && c.at(i+2) == 0)) { 14 | i+=2; 15 | count++; 16 | } else if(c.at(i+1) == 0 && c.at(i+2) == 1) { 17 | i++; 18 | count++; 19 | } 20 | } else { 21 | i++; 22 | count++; 23 | } 24 | } 25 | return count; 26 | } 27 | 28 | int main() 29 | { 30 | ofstream fout(getenv("OUTPUT_PATH")); 31 | 32 | int n; 33 | cin >> n; 34 | cin.ignore(numeric_limits::max(), '\n'); 35 | 36 | string c_temp_temp; 37 | getline(cin, c_temp_temp); 38 | 39 | vector c_temp = split_string(c_temp_temp); 40 | 41 | vector c(n); 42 | 43 | for (int i = 0; i < n; i++) { 44 | int c_item = stoi(c_temp[i]); 45 | 46 | c[i] = c_item; 47 | } 48 | 49 | int result = jumpingOnClouds(c); 50 | 51 | fout << result << "\n"; 52 | 53 | fout.close(); 54 | 55 | return 0; 56 | } 57 | 58 | vector split_string(string input_string) { 59 | string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) { 60 | return x == y and x == ' '; 61 | }); 62 | 63 | input_string.erase(new_end, input_string.end()); 64 | 65 | while (input_string[input_string.length() - 1] == ' ') { 66 | input_string.pop_back(); 67 | } 68 | 69 | vector splits; 70 | char delimiter = ' '; 71 | 72 | size_t i = 0; 73 | size_t pos = input_string.find(delimiter); 74 | 75 | while (pos != string::npos) { 76 | splits.push_back(input_string.substr(i, pos - i)); 77 | 78 | i = pos + 1; 79 | pos = input_string.find(delimiter, i); 80 | } 81 | 82 | splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1)); 83 | 84 | return splits; 85 | } -------------------------------------------------------------------------------- /interview-preparation-kit/warm-up-challenges/sock-merchant/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | vector split_string(string); 7 | 8 | // Complete the sockMerchant function below. 9 | int sockMerchant(int n, vector ar) { 10 | unordered_map umap; 11 | int result = 0; 12 | for (int i = 0; i < n; i++) { 13 | int element = ar.at(i); 14 | if (umap.find(element) == umap.end()) { 15 | umap[element] = 1; 16 | } else { 17 | if(umap.at(element) == 0) { 18 | umap[element] = 1; 19 | } else { 20 | result += 1; 21 | umap[element] = 0; 22 | } 23 | } 24 | } 25 | return result; 26 | } 27 | 28 | int main() 29 | { 30 | ofstream fout(getenv("OUTPUT_PATH")); 31 | 32 | int n; 33 | cin >> n; 34 | cin.ignore(numeric_limits::max(), '\n'); 35 | 36 | string ar_temp_temp; 37 | getline(cin, ar_temp_temp); 38 | 39 | vector ar_temp = split_string(ar_temp_temp); 40 | 41 | vector ar(n); 42 | 43 | for (int i = 0; i < n; i++) { 44 | int ar_item = stoi(ar_temp[i]); 45 | 46 | ar[i] = ar_item; 47 | } 48 | 49 | int result = sockMerchant(n, ar); 50 | 51 | fout << result << "\n"; 52 | 53 | fout.close(); 54 | 55 | return 0; 56 | } 57 | 58 | vector split_string(string input_string) { 59 | string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) { 60 | return x == y and x == ' '; 61 | }); 62 | 63 | input_string.erase(new_end, input_string.end()); 64 | 65 | while (input_string[input_string.length() - 1] == ' ') { 66 | input_string.pop_back(); 67 | } 68 | 69 | vector splits; 70 | char delimiter = ' '; 71 | 72 | size_t i = 0; 73 | size_t pos = input_string.find(delimiter); 74 | 75 | while (pos != string::npos) { 76 | splits.push_back(input_string.substr(i, pos - i)); 77 | 78 | i = pos + 1; 79 | pos = input_string.find(delimiter, i); 80 | } 81 | 82 | splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1)); 83 | 84 | return splits; 85 | } -------------------------------------------------------------------------------- /interview-preparation-kit/sorting/fraudulent-activity-notifications/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.math.*; 3 | import java.security.*; 4 | import java.text.*; 5 | import java.util.*; 6 | import java.util.concurrent.*; 7 | import java.util.regex.*; 8 | 9 | public class Solution { 10 | 11 | // Complete the activityNotifications function below. 12 | static int activityNotifications(int[] expenditure, int d) { 13 | int n = expenditure.length; 14 | int ans = 0, med = 0; 15 | int[] freq = new int[201]; 16 | for(int i = 0; i < n; i++) { 17 | int val = expenditure[i]; 18 | 19 | if(i >= d) { 20 | med = findMedian(freq, d / 2 + d % 2); 21 | 22 | if(d % 2 == 0) { 23 | int ret = findMedian(freq, d / 2 + 1); 24 | if (val >= med + ret) { 25 | ans++; 26 | } 27 | } else { 28 | if (val >= med * 2) { 29 | ans++; 30 | } 31 | } 32 | } 33 | 34 | freq[val]++; 35 | 36 | if(i >= d) { 37 | int prev = expenditure[i-d]; 38 | freq[prev]--; 39 | } 40 | } 41 | return ans; 42 | } 43 | 44 | private static int findMedian(int[] freq, int idx) { 45 | int s = 0; 46 | for(int i = 0; i < 200; i++) { 47 | s += freq[i]; 48 | if(s >= idx) { 49 | return i; 50 | } 51 | } 52 | return 0; 53 | } 54 | 55 | private static final Scanner scanner = new Scanner(System.in); 56 | 57 | public static void main(String[] args) throws IOException { 58 | BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); 59 | 60 | String[] nd = scanner.nextLine().split(" "); 61 | 62 | int n = Integer.parseInt(nd[0]); 63 | 64 | int d = Integer.parseInt(nd[1]); 65 | 66 | int[] expenditure = new int[n]; 67 | 68 | String[] expenditureItems = scanner.nextLine().split(" "); 69 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 70 | 71 | for (int i = 0; i < n; i++) { 72 | int expenditureItem = Integer.parseInt(expenditureItems[i]); 73 | expenditure[i] = expenditureItem; 74 | } 75 | 76 | int result = activityNotifications(expenditure, d); 77 | 78 | bufferedWriter.write(String.valueOf(result)); 79 | bufferedWriter.newLine(); 80 | 81 | bufferedWriter.close(); 82 | 83 | scanner.close(); 84 | } 85 | } -------------------------------------------------------------------------------- /interview-preparation-kit/sorting/ctci-merge-sort/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.math.*; 3 | import java.security.*; 4 | import java.text.*; 5 | import java.util.*; 6 | import java.util.concurrent.*; 7 | import java.util.regex.*; 8 | 9 | public class Solution { 10 | 11 | // Complete the countInversions function below. 12 | static long countInversions(int[] arr) { 13 | MergeSort ms = new MergeSort(); 14 | return ms.sort(arr); 15 | } 16 | 17 | /* reference: https://algs4.cs.princeton.edu/22mergesort/Merge.java.html */ 18 | private static class MergeSort { 19 | private long swapCount = 0; 20 | 21 | public long sort(int[] arr) { 22 | int[] helper = new int[arr.length]; 23 | sort(arr, helper, 0, arr.length - 1); 24 | return swapCount; 25 | } 26 | 27 | private void sort(int[] arr, int[] helper, int low, int high) { 28 | if (high <= low) return; 29 | int mid = low + (high - low) / 2; 30 | sort(arr, helper, low, mid); 31 | sort(arr, helper, mid + 1, high); 32 | merge(arr, helper, low, mid, high); 33 | } 34 | 35 | private void merge(int[] arr, int[] helper, int low, int mid, int high) { 36 | // copy to helper[] 37 | for (int k = low; k <= high; k++) { 38 | helper[k] = arr[k]; 39 | } 40 | 41 | // merge back to a[] 42 | int i = low, j = mid + 1; 43 | for (int k = low; k <= high; k++) { 44 | if(i > mid) { 45 | swapCount += mid + 1 - i; 46 | arr[k] = helper[j++]; 47 | } else if(j > high) { 48 | arr[k] = helper[i++]; 49 | } else if(helper[j] < helper[i]) { 50 | swapCount += mid + 1 - i; 51 | arr[k] = helper[j++]; 52 | } else { 53 | arr[k] = helper[i++]; 54 | } 55 | } 56 | } 57 | } 58 | 59 | private static final Scanner scanner = new Scanner(System.in); 60 | 61 | public static void main(String[] args) throws IOException { 62 | BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); 63 | 64 | int t = scanner.nextInt(); 65 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 66 | 67 | for (int tItr = 0; tItr < t; tItr++) { 68 | int n = scanner.nextInt(); 69 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 70 | 71 | int[] arr = new int[n]; 72 | 73 | String[] arrItems = scanner.nextLine().split(" "); 74 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 75 | 76 | for (int i = 0; i < n; i++) { 77 | int arrItem = Integer.parseInt(arrItems[i]); 78 | arr[i] = arrItem; 79 | } 80 | 81 | long result = countInversions(arr); 82 | 83 | bufferedWriter.write(String.valueOf(result)); 84 | bufferedWriter.newLine(); 85 | } 86 | 87 | bufferedWriter.close(); 88 | 89 | scanner.close(); 90 | } 91 | } --------------------------------------------------------------------------------