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.

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)

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.

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 .

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:

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

The elements of the first array are all factors of the integer being considered

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

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.

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.