We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#!/bin/python3importmathimportosimportrandomimportreimportsys## Complete the 'fairCut' function below.## The function is expected to return an INTEGER.# The function accepts following parameters:# 1. INTEGER k# 2. INTEGER_ARRAY arr#deffairCut(k,arr):# Write your code heren=len(arr)ifn-k<k:k=n-karr.sort()is_even_n=n%2==0is_even_k=k%2==0indices=[]if(is_even_nandis_even_k)or(notis_even_nandis_even_k):x=(n+1)// 2foriinrange(k// 2):indices.append(x-(2*i+1))indices.append(x+(2*i+1))elif(is_even_nandnotis_even_k)or(notis_even_nandnotis_even_k):x=(n+1)// 2indices=[x]foriinrange(k// 2):indices.append(x-(2*(i+1)))indices.append(x+(2*(i+1)))s1=[]s2=[]foriinrange(n):ifi+1inindices:s1.append(arr[i])else:s2.append(arr[i])total_sum=0foriins1:forjins2:total_sum+=abs(i-j)returntotal_sumif__name__=='__main__':fptr=open(os.environ['OUTPUT_PATH'],'w')first_multiple_input=input().rstrip().split()n=int(first_multiple_input[0])k=int(first_multiple_input[1])arr=list(map(int,input().rstrip().split()))result=fairCut(k,arr)fptr.write(str(result)+'\n')fptr.close()
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Fair Cut
You are viewing a single comment's thread. Return to all comments →
Python Solution