Design a site like this with WordPress.com

## PICKING NUMBERS| PYTHON PROGRAMMING | HACKERRANK SOLUTION

Given an array of integers, find the longest subarray where the absolute difference between any two elements is less than or equal to .

Example

There are two subarrays meeting the criterion:  and . The maximum length subarray has  elements.

Function Description

Complete the pickingNumbers function in the editor below.

pickingNumbers has the following parameter(s):

• int a[n]: an array of integers

Returns

• int: the length of the longest subarray that meets the criterion

Input Format

The first line contains a single integer , the size of the array .
The second line contains  space-separated integers, each an .

Constraints

• The answer will be .

Sample Input 0

```6
4 6 5 3 3 1
```

Sample Output 0

```3
```

Explanation 0

We choose the following multiset of integers from the array: . Each pair in the multiset has an absolute difference  (i.e.,  and ), so we print the number of chosen integers, , as our answer.

Sample Input 1

```6
1 2 2 3 1 2
```

Sample Output 1

```5
```

Explanation 1

We choose the following multiset of integers from the array: . Each pair in the multiset has an absolute difference  (i.e., , , and ), so we print the number of chosen integers, 5, as our answer.

Solution:

## Python Program to draw Infinity Symbol|Using Turtle

from turtle import *
bgcolor(“white”)
color(“red”)
speed(11)
right(45)
for i in range(150):
circle(30)
if 7<i<62:
left(5)
if 80<i<133:
right(5)
if i<80:
forward(10)
else:
forward(5)

## Python Program to draw colour rings|Using Turtle

import turtle
colors=[‘orange’,’red’,’pink’,’yellow’,’blue’,’green’]
screen=turtle.Screen()
trtl=turtle.Turtle()
trtl.speed(0)
screen.bgcolor(“black”)

for x in range(120):
trtl.pencolor(colors[x%6])
trtl.width(x/5+1)
trtl.forward(x)
trtl.left(20)

## Minimum Swaps 2| HACKERRANK| PYPY SOLUTION

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. Find the minimum number of swaps required to sort the array in ascending order.

Example

Perform the following steps:

```i   arr                         swap (indices)
0   [7, 1, 3, 2, 4, 5, 6]   swap (0,3)
1   [2, 1, 3, 7, 4, 5, 6]   swap (0,1)
2   [1, 2, 3, 7, 4, 5, 6]   swap (3,4)
3   [1, 2, 3, 4, 7, 5, 6]   swap (4,5)
4   [1, 2, 3, 4, 5, 7, 6]   swap (5,6)
5   [1, 2, 3, 4, 5, 6, 7]

```

It took  swaps to sort the array.

Function Description

Complete the function minimumSwaps in the editor below.

minimumSwaps has the following parameter(s):

• int arr[n]: an unordered array of integers

Returns

• int: the minimum number of swaps to sort the array

Input Format

The first line contains an integer, , the size of .
The second line contains  space-separated integers .

Constraints

Sample Input 0

```4
4 3 1 2
```

Sample Output 0

```3
```

Explanation 0

Given array
After swapping  we get
After swapping  we get
After swapping  we get
So, we need a minimum of  swaps to sort the array in ascending order.

Sample Input 1

```5
2 3 4 1 5
```

Sample Output 1

```3
```

Explanation 1

Given array
After swapping  we get
After swapping  we get
After swapping  we get
So, we need a minimum of  swaps to sort the array in ascending order.

Sample Input 2

```7
1 3 5 2 4 6 7
```

Sample Output 2

```3
```

Explanation 2

Given array
After swapping  we get
After swapping  we get
After swapping  we get
So, we need a minimum of  swaps to sort the array in ascending order.

SOLUTION

## Repeated String | HackerRank Solution | Python Programming| Solution

There is a string, , of lowercase English letters that is repeated infinitely many times. Given an integer, , find and print the number of letter `a`‘s in the first  letters of the infinite string.

Example

The substring we consider is , the first  characters of the infinite string. There are  occurrences of `a` in the substring.

Function Description

Complete the repeatedString function in the editor below.

repeatedString has the following parameter(s):

• s: a string to repeat
• n: the number of characters to consider

Returns

• int: the frequency of `a` in the substring

Input Format

The first line contains a single string, .
The second line contains an integer, .

Constraints

• For  of the test cases, .

Sample Input

Sample Input 0

```aba
10

```

Sample Output 0

```7

```

Explanation 0
The first  letters of the infinite string are `abaabaabaa`. Because there are  `a`‘s, we return .

Sample Input 1

```a
1000000000000

```

Sample Output 1

```1000000000000

```

Explanation 1
Because all of the first  letters of the infinite string are `a`, we return .

SOLUTION:

## Jumping-on-the-clouds | Solution | hackerRank | Python

There is a new mobile game that starts with consecutively numbered clouds. Some of the clouds are thunderheads and others are cumulus. The player can jump on any cumulus cloud having a number that is equal to the number of the current cloud plus  or . The player must avoid the thunderheads. Determine the minimum number of jumps it will take to jump from the starting postion to the last cloud. It is always possible to win the game.

For each game, you will get an array of clouds numbered  if they are safe or  if they must be avoided.

Example

Index the array from . The number on each cloud is its index in the list so the player must avoid the clouds at indices  and . They could follow these two paths:  or . The first path takes  jumps while the second takes . Return .

Function Description

Complete the jumpingOnClouds function in the editor below.

jumpingOnClouds has the following parameter(s):

• int c[n]: an array of binary integers

Returns

• int: the minimum number of jumps required

Input Format

The first line contains an integer , the total number of clouds. The second line contains  space-separated binary integers describing clouds  where .

Constraints

Output Format

Print the minimum number of jumps needed to win the game.

Sample Input 0

```7
0 0 1 0 0 1 0

```

Sample Output 0

```4

```

Explanation 0:
The player must avoid  and . The game can be won with a minimum of  jumps:

Sample Input 1

```6
0 0 0 0 1 0

```

Sample Output 1

```3

```

Explanation 1:
The only thundercloud to avoid is . The game can be won in  jumps:

Solution

## Between Two Sets | HackerRank | Solution | Python Programming

There will be two arrays of integers. Determine all integers that satisfy the following two conditions:

1. The elements of the first array are all factors of the integer being considered
2. The integer being considered is a factor of all elements of the second array

These numbers are referred to as being between the two arrays. Determine how many such numbers exist.

Example

There are two numbers between the arrays:  and .
, ,  and  for the first value.
,  and ,  for the second value. Return .

Function Description

Complete the getTotalX function in the editor below. It should return the number of integers that are betwen the sets.

getTotalX has the following parameter(s):

• int a[n]: an array of integers
• int b[m]: an array of integers

Returns

• int: the number of integers that are between the sets

Input Format

The first line contains two space-separated integers,  and , the number of elements in arrays  and .
The second line contains  distinct space-separated integers  where .
The third line contains  distinct space-separated integers  where .

Constraints

Sample Input

```2 3
2 4
16 32 96

```

Sample Output

```3

```

Explanation

2 and 4 divide evenly into 4, 8, 12 and 16.
4, 8 and 16 divide evenly into 16, 32, 96.

4, 8 and 16 are the only three numbers for which each element of a is a factor and each is a factor of all elements of b.

Solution:

```aa,bb=0,0
n,m=map(int,raw_input().split())
a=map(int,raw_input().split())
b=map(int,raw_input().split())
ct=0
for i in xrange(max(a),min(b)+1):
for j in a:
if i%j!=0:
break
else:
for k in b:
if k%i!=0:
break
else:
ct+=1
print ct```

## Blog

Staircase HackerRank Problem solution-Using Python

Staircase detail

This is a staircase of size :

```   #
##
###
####

```

Its base and height are both equal to . It is drawn using `#` symbols and spaces. The last line is not preceded by any spaces.

Write a program that prints a staircase of size .

Function Description

Complete the staircase function in the editor below.

staircase has the following parameter(s):

• int n: an integer

Print

Print a staircase as described above.

Input Format

A single integer, , denoting the size of the staircase.

Constraints

.

Output Format
Print a staircase of size  using `#` symbols and spaces.Note:
The last line must have  spaces in it.

Solution:
def staircase(n):
for x in range(1,n+1):
print(” “*(n-x),end =””) print(“#”*x)

if name == ‘main‘:
n = int(input().strip())
staircase(n)

Staircase HackerRank Problem solution-Using Python

Staircase detail

This is a staircase of size :

```   #
##
###
####

```

Its base and height are both equal to . It is drawn using `#` symbols and spaces. The last line is not preceded by any spaces.

Write a program that prints a staircase of size .

Function Description

Complete the staircase function in the editor below.

staircase has the following parameter(s):

• int n: an integer

Print

Print a staircase as described above.

Input Format

A single integer, , denoting the size of the staircase.

Constraints

.

Output Format
Print a staircase of size  using `#` symbols and spaces.Note:
The last line must have  spaces in it.

Solution:
def staircase(n):
for x in range(1,n+1):
print(” “*(n-x),end =””) print(“#”*x)

if name == ‘main‘:
n = int(input().strip())
staircase(n)

Number Line Jumps– HackerRank solution using Python

You are choreographing a circus show with various animals. For one act, you are given two kangaroos on a number line ready to jump in the positive direction (i.e, toward positive infinity).

• The first kangaroo starts at location  and moves at a rate of  meters per jump.
• The second kangaroo starts at location  and moves at a rate of  meters per jump.

You have to figure out a way to get both kangaroos at the same location at the same time as part of the show. If it is possible, return `YES`, otherwise return `NO`.

Example

After one jump, they are both at , (, ), so the answer is `YES`.

Function Description

Complete the function kangaroo in the editor below.

kangaroo has the following parameter(s):

• int x1, int v1: starting position and jump distance for kangaroo 1
• int x2, int v2: starting position and jump distance for kangaroo 2

Returns

• string: either `YES` or `NO`

Input Format

A single line of four space-separated integers denoting the respective values of , , , and .

Constraints

Sample Input 0

```0 3 4 2
```

Sample Output 0

```YES
```

Explanation 0

The two kangaroos jump through the following sequence of locations:

From the image, it is clear that the kangaroos meet at the same location (number  on the number line) after same number of jumps ( jumps), and we print `YES`.

Sample Input 1

```0 2 5 3
```

Sample Output 1

```NO
```

Explanation 1

The second kangaroo has a starting location that is ahead (further to the right) of the first kangaroo’s starting location (i.e., ). Because the second kangaroo moves at a faster rate (meaning ) and is already ahead of the first kangaroo, the first kangaroo will never be able to catch up. Thus, we print NO.

Solution:

```
def kangaroo(x1, v1, x2, v2):
if(x1==x2):
return("YES")
if(x1>x2):
if(v1>=v2):
return("NO")
if(x2>x1):
if(v2>=v1):
return("NO")
x1=x1+v1
x2=x2+v2
print(x1,x2)
return kangaroo(x1,v1,x2,v2)
```

Number Line Jumps– HackerRank solution using Python

You are choreographing a circus show with various animals. For one act, you are given two kangaroos on a number line ready to jump in the positive direction (i.e, toward positive infinity).

• The first kangaroo starts at location  and moves at a rate of  meters per jump.
• The second kangaroo starts at location  and moves at a rate of  meters per jump.

You have to figure out a way to get both kangaroos at the same location at the same time as part of the show. If it is possible, return `YES`, otherwise return `NO`.

Example

After one jump, they are both at , (, ), so the answer is `YES`.

Function Description

Complete the function kangaroo in the editor below.

kangaroo has the following parameter(s):

• int x1, int v1: starting position and jump distance for kangaroo 1
• int x2, int v2: starting position and jump distance for kangaroo 2

Returns

• string: either `YES` or `NO`

Input Format

A single line of four space-separated integers denoting the respective values of , , , and .

Constraints

Sample Input 0

```0 3 4 2
```

Sample Output 0

```YES
```

Explanation 0

The two kangaroos jump through the following sequence of locations:

From the image, it is clear that the kangaroos meet at the same location (number  on the number line) after same number of jumps ( jumps), and we print `YES`.

Sample Input 1

```0 2 5 3
```

Sample Output 1

```NO
```

Explanation 1

The second kangaroo has a starting location that is ahead (further to the right) of the first kangaroo’s starting location (i.e., ). Because the second kangaroo moves at a faster rate (meaning ) and is already ahead of the first kangaroo, the first kangaroo will never be able to catch up. Thus, we print NO.

Solution:

```
def kangaroo(x1, v1, x2, v2):
if(x1==x2):
return("YES")
if(x1>x2):
if(v1>=v2):
return("NO")
if(x2>x1):
if(v2>=v1):
return("NO")
x1=x1+v1
x2=x2+v2
print(x1,x2)
return kangaroo(x1,v1,x2,v2)
```