DEV Community

Shashi Vardhan
Shashi Vardhan

Posted on

5 1

Popular Contest

Codeground is hosting the World's largest coding competition. There are N participants in the contest sitting in a single row.

The energy of i’th participant from left is A[i]. To raise the popularity of the contest, some rules are added.

A participant numbered i will challenge other participant numbered j if and only if j is ahead of him (j>i) and the distance between i and j (j-i) is prime.

The contest popularity value of participant i challenging participant j is A[j]-A[i]. The total contest popularity value of the competition is sum of popularity value of all such challenges.

Given the energy of all participants, you have to find the total popularity value.

INPUT

The first line contains N, the number of participants. The next line contains N space separated integers representing the energy of all the participants.

OUTPUT

Print a single line containing the total popularity value.

*CONSTRAINTS
*

1<=N<=1000

1 <=A[i]<= 1000000000

EXPLANATION OF SAMPLE

1 Sample input has N as 7 and participants energy as 24 6 8 10 12 14. 2. The contest popularity based on rules described is as below:

j-i val diff

5-0=5 12-2=10

3-0=3 8-2 = 6

2-0=2 6-2 = 4

6-1=5 14-4 = 10

4-1=3 10-4 = 6

3-1=2 8-4 = 4

5-2=3 12-6 = 6

4-2=2 10-6 = 4

6-3=3 14-8= 6

5-3=2 12-8 = 4

6-4=2 14-10=4

Total 64

Sample Input

7 2 4 6 8 10 12 14

Sample Output

64

Code(python 3)

def solution(n,A):

    sum=0

    for i in range(n):

        for j in range(n):

            if(j>i):

                num=j-i

                if(prime(num)):

                    sum=sum+(A[j]-A[i])

    return sum

def prime(n):

    if n==1:

        return False

    for i in range(2,int(n/2)+1):

        if n%i==0:

            return False

    return True

n=int(input())

A=list(map(int,input().split()))[:n]

print(solution(n,A))
Enter fullscreen mode Exit fullscreen mode

More at : https://onlylang.blogspot.com/

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay