Skip to content

Instantly share code, notes, and snippets.

@AnkDos
Created April 6, 2019 07:03
Show Gist options
  • Save AnkDos/f65d4720c453ff61928161d57b5f9ede to your computer and use it in GitHub Desktop.
Save AnkDos/f65d4720c453ff61928161d57b5f9ede to your computer and use it in GitHub Desktop.
# CREATED BY ANKDOS
# A PROGRAM THAT TAKES Nth HIGHEST NUMBER (only n , not element) IN THE ARRAY AND RETURNS HOW MANY TIMES ITS REPEATING
def sort_dec (ar) :
swap_count = 0
i = 0
while True :
while i < len(ar)-1 :
if ar[i] < ar[i+1] :
temp = ar[i+1]
ar[i+1] = ar[i]
ar[i] = temp
swap_count += 1
i += 1
if swap_count == 0 :
break
swap_count = 0
i = 0
return ar
def searchncount (ar , n_highest_ele) :
count_reps = 0
target_found = False
i = 0
ar = sort_dec(ar)
count_change = 0
ele = 0
while i < len(ar) :
if i != len(ar)-1 :
if ar[i] != ar[i+1] :
count_change += 1
if count_change == n_highest_ele - 1 and target_found == False:
target_found = True
ele = ar[i+1]
if target_found == True and ele == ar[i] :
count_reps = count_reps + 1
if i == len(ar) - 1 and ele == ar[i] :
count_reps += 1
i += 1
if count_reps == 0 and n_highest_ele == 1 :
ele = ar[0]
return str(count_reps) + " " + str(ele)
ar = [3,1,2,4,5,4,4,6,7,6,7,4,6,7,8,7,9,2,3,11,12,32,17,19,17,17,19,32,11,12,5,8]
print (searchncount(ar , 3)) # HERE WE ARE CALCULATING HOW MANY TIMES THE 3rd HIGHEST ELEMENT IS BEEN REPEATED
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment