├── README.md └── HackerRank - Interview Preparation Kit └── Array ├── Left Rotation ├── Left Rotation.py └── README.md ├── Minimum Swaps 2 ├── Minimum Swaps 2.py └── README.md └── 2D Array - DS ├── 2D Array - DS.py └── README.md /README.md: -------------------------------------------------------------------------------- 1 | # Interview -------------------------------------------------------------------------------- /HackerRank - Interview Preparation Kit/Array/Left Rotation/Left Rotation.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the rotLeft function below. 10 | def rotLeft(a, d): 11 | return a[d:]+a[:d] 12 | 13 | if __name__ == '__main__': 14 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 15 | 16 | nd = input().split() 17 | 18 | n = int(nd[0]) 19 | 20 | d = int(nd[1]) 21 | 22 | a = list(map(int, input().rstrip().split())) 23 | 24 | result = rotLeft(a, d) 25 | 26 | fptr.write(' '.join(map(str, result))) 27 | fptr.write('\n') 28 | 29 | fptr.close() 30 | -------------------------------------------------------------------------------- /HackerRank - Interview Preparation Kit/Array/Minimum Swaps 2/Minimum Swaps 2.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the minimumSwaps function below. 10 | def minimumSwaps(arr): 11 | swaps = 0 12 | n = len(arr) 13 | 14 | for idx in range(n): 15 | while arr[idx]-1 != idx: 16 | ele = arr[idx] 17 | arr[ele-1], arr[idx] = arr[idx], arr[ele-1] 18 | swaps += 1 19 | return swaps 20 | 21 | if __name__ == '__main__': 22 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 23 | 24 | n = int(input()) 25 | 26 | arr = list(map(int, input().rstrip().split())) 27 | 28 | res = minimumSwaps(arr) 29 | 30 | fptr.write(str(res) + '\n') 31 | 32 | fptr.close() 33 | -------------------------------------------------------------------------------- /HackerRank - Interview Preparation Kit/Array/2D Array - DS/2D Array - DS.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 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 | total = 0 12 | max_total = -100 13 | 14 | for i in range(len(arr)): 15 | for j in range(len(arr[i])): 16 | if (j+2 < 6) and (i+2 < 6): 17 | total = 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] 18 | if max_total < total: 19 | max_total = total 20 | return max_total 21 | 22 | 23 | if __name__ == '__main__': 24 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 25 | 26 | arr = [] 27 | 28 | for _ in xrange(6): 29 | arr.append(map(int, raw_input().rstrip().split())) 30 | 31 | result = hourglassSum(arr) 32 | 33 | fptr.write(str(result) + '\n') 34 | 35 | fptr.close() 36 | -------------------------------------------------------------------------------- /HackerRank - Interview Preparation Kit/Array/Left Rotation/README.md: -------------------------------------------------------------------------------- 1 | 2 | ## Array: Left Rotation 3 | A left rotation operation on an array shifts each of the array's elements 1 unit to the left. For example, if 2 left rotations are performed on array [1, 2, 3, 4, 5], then the array would become [3, 4, 5, 1, 2]. Note that the lowest index item moves to the highest index in a rotation. This is called a circular array. 4 | 5 | Given an array a of n integers and a number, d, perform d left rotations on the array. Return the updated array to be printed as a single line of space-separated integers.

6 | 7 | Function Description
8 | Complete the function rotLeft in the editor below.
9 | rotLeft has the following parameter(s):
10 | - int a[n]: the array to rotate
11 | - int d: the number of rotations

12 | 13 | Returns
14 | - int a'[n]: the rotated array 15 | Input Format
16 | The first line contains two space-separated integers n and d, the size of a and the number of left rotations. 17 | The second line contains n space-separated integers, each an n[i].

18 | 19 | Constraints
20 | - 1 <= n <= 105 21 | - 1 <= d <= n 22 | - 1 <= a[i] <= 106

23 | 24 | Sample Input
25 | ``` 26 | 5 4 27 | 1 2 3 4 5 28 | ``` 29 |
30 | 31 | Sample Output
32 | ``` 33 | 3 4 5 1 2 34 | ``` 35 |
36 | -------------------------------------------------------------------------------- /HackerRank - Interview Preparation Kit/Array/Minimum Swaps 2/README.md: -------------------------------------------------------------------------------- 1 | 2 | ## Minimum Swaps 2 3 | You are given an unordered array consisting of consecutive integers ([1, 2, 3, ..., n]) without any duplicates. You are allowed to swap any two elements. You need to find the minimum number of swaps required to sort the array in ascending order. 4 | 5 | For example, given the array arr = [7, 1, 3, 2, 4, 5, 6] we perform the following steps:
6 | 7 | i arr swap (indices) 8 | 0 [7, 1, 3, 2, 4, 5, 6] swap (0,3) 9 | 1 [2, 1, 3, 7, 4, 5, 6] swap (0,1) 10 | 2 [1, 2, 3, 7, 4, 5, 6] swap (3,4) 11 | 3 [1, 2, 3, 4, 7, 5, 6] swap (4,5) 12 | 4 [1, 2, 3, 4, 5, 7, 6] swap (5,6) 13 | 5 [1, 2, 3, 4, 5, 6, 7] 14 | It took 5 swaps to sort the array.

15 | 16 | Function Description 17 | 18 | Complete the function minimumSwaps in the editor below. It must return an integer representing the minimum number of swaps to sort the array. 19 | minimumSwaps has the following parameter(s): 20 | - arr: an unordered array of integers

21 | 22 | Input Format 23 | 24 | The first line contains an integer, n, the size of arr.
The second line contains n space-separated integers arr[i] .

25 | 26 | Constraints
27 | - 1 <= n <= 105 28 | - 1 <= arr[i] <= n 29 | 30 | Output Format
31 | Return the minimum number of swaps to sort the given array. 32 | 33 | Sample Input
34 | ``` 35 | 4 36 | 4 3 1 2 37 | ``` 38 | 39 | Sample Output
40 | ``` 41 | 0 42 | ``` 43 |
44 | -------------------------------------------------------------------------------- /HackerRank - Interview Preparation Kit/Array/2D Array - DS/README.md: -------------------------------------------------------------------------------- 1 | ## [2D Array - DS](https://www.hackerrank.com/challenges/2d-array/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=arrays) 2 | Given a 6 X 6 2D Array, arr:
3 |
1 1 1 0 0 0
 4 | 0 1 0 0 0 0
 5 | 1 1 1 0 0 0
 6 | 0 0 0 0 0 0
 7 | 0 0 0 0 0 0
 8 | 0 0 0 0 0 0
 9 | 

10 | We define an hourglass in A to be a subset of values with indices falling in this pattern in arr's graphical representation:
11 |
a b c
12 |   d
13 | e f g
14 | 

15 | 16 | There are 16 hourglasses in arr, and an hourglass sum is the sum of an hourglass' values. Calculate the hourglass sum for every hourglass in arr, then print the maximum hourglass sum.
17 | For example, given the 2D array:
18 |
-9 -9 -9  1 1 1 
19 |  0 -9  0  4 3 2
20 | -9 -9 -9  1 2 3
21 |  0  0  8  6 6 0
22 |  0  0  0 -2 0 0
23 |  0  0  1  2 4 0
24 | 

25 | We calculate the following 16 hourglass values:
26 |
-63, -34, -9, 12, 
27 | -10, 0, 28, 23, 
28 | -27, -11, -2, 10, 
29 | 9, 17, 25, 18
30 | 

31 | Our highest hourglass value is 28 from the hourglass: 32 |
0 4 3
33 |   1
34 | 8 6 6
35 | 

36 | 37 | 38 | **Function Description**
39 | Complete the function hourglassSum in the editor below. It should return an integer, the maximum hourglass sum in the array.
40 | hourglassSum has the following parameter(s): 41 | 42 | 43 | 44 | **Input Format**
45 | Each of the 6 lines of inputs arr[i] contains 6 space-separated integers arr[i][j].
46 | 47 | **Output Format**
48 | Print the largest (maximum) hourglass sum found in arr. 49 | 50 | **Sample Input**
51 |
1 1 1 0 0 0
52 | 0 1 0 0 0 0
53 | 1 1 1 0 0 0
54 | 0 0 2 4 4 0
55 | 0 0 0 2 0 0
56 | 0 0 1 2 4 0
57 | 

58 | 59 | **Sample Output**
60 | 19 --------------------------------------------------------------------------------