Giter Club home page Giter Club logo

cardspermutation's Introduction

  • Please note there's an issue running the repl.it on mobile phones. If interested in playing with the repl.it please do it on a computer

CardsPermutation

Cards Permutation

first ever fully accepted solution to this problem written in javascript!

Overview

Alice was given the n integers from 1 to n. She wrote all possible permutations in increasing lexicographical order, and wrote each permutation in a new line. For example, for n = 3, there are 6 possible permutations:

  • [1,2,3]
  • [1,3,2]
  • [2,1,3]
  • [2,3,1]
  • [3,1,2]
  • [3,2,1]

She then chose one permutation among them as her favorite permutation.

After some time, she forgot some elements of her favorite permutation. Nevertheless, she still tried to write down its elements. She wrote a 0 in every position where she forgot the true value.

She wants to know the sum of the line numbers of the permutations which could possibly be her favorite permutation, i.e., permutations which can be obtained by replacing the 0s. Can you help her out?

Since the sum can be large, find it modulo 10⁹ + 7.

Input Format

The first line contains a single integer n.

The next line contains n space-separated integers a₁,a₂,...,aₙ denoting Alice's favorite permutation with some positions replaced by 0.

Constraints

  • 1 ≤ n ≤ 3 * 10⁵
  • 0 ≤ aᵢ ≤ n

Subtask

  • For ~33% of the total points, n ≤ 5000

Output Format

Print a single line containing a single integer denoting the sum of the line numbers of the permutations which could possibly be Alice's favorite permutation.

Sample Input 0

4
0 2 3 0

Sample Output 0

23

Explanation 0

The possible permutations are [1,2,3,4] and [4,2,3,1]. The permutation [1,2,3,4] occurs on line 1 and the permutation [4,2,3,1] occurs on line 22. Therefore the sum is 1 + 22 = 23.

Sample Input 1

4
4 3 2 1

Sample Output 1

24

Explanation 1

There is no missing number in the permutation. Therefore, the only possible permutation is [4,3,2,1], and it occurs on line 24. Therefore the sum is 24.

Try it Out

If you'd care to play around with this code please check out the corresponding repl.it. And if you'd care to follow or get in touch here is my HackerRank profile.

cardspermutation's People

Contributors

xanimo avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.