Learn DSADSAContests
coding75 logo
LeetCode POTDPOTDSheets
coding75 Pro
coding75 logo
Dashboard
Learn DSA
Course RoadmapFlow
DSA Topic TreeNew πŸš€
Contest SolutionsPractice
Practice Sheets
MasterclassesLive πŸ‘¨πŸ»β€πŸ’»

coding75 ProPRO

Live Classes & Placement Guidance

coding75 logo

The premier developer platform for mastering Data Structures & Algorithms, exploring contest editorials, building ATS-ready resumes, and accelerating your tech career.

Connect & Community

DSA & Contests

  • Learn DSA
  • Contest Solutions
  • LeetCode POTDDaily
  • Practice Sheets
  • MasterclassesSoon

Interview Prep

  • Portfolio Projects
  • CS Fundamentals
  • System DesignSoon
  • Interview ExperiencesSoon
  • Mock InterviewsSoon

Career & Pro

  • Jobs & Internships
  • Resume BuilderATS
  • coding75 Pro
  • Submit Feedback
Β© 2026coding75β€’crackDSAβ„’β€’Maa Lalita Edtech Private Limited. All Rights Reserved.
Privacy PolicyTerms & ConditionsContact Support
Registered Office: Kanpur 208021β€’Regional Office: Indiranagar, Bangalore, 560008
Maa Lalita Edtech Private Limited
Contests/Codeforces/Codeforces Round 957/C. Gorilla and Permutation
PrevNext
CodeforcesCodeforces
Specialist
Codeforces Round 957

C. Gorilla and Permutation

Math
Loading...
Solve on Codeforces

Problem Statement

Official algorithmic problem description and constraints.

Open on Codeforces

Gorilla and Noobish_Monk found three numbers π‘›, π‘š, and π‘˜ (π‘š<π‘˜). They decided to construct a permutation† of length π‘›.

For the permutation, Noobish_Monk came up with the following function: π‘”(𝑖) is the sum of all the numbers in the permutation on a prefix of length π‘– that are not greater than π‘š. Similarly, Gorilla came up with the function π‘“, where π‘“(𝑖) is the sum of all the numbers in the permutation on a prefix of length π‘– that are not less than π‘˜. A prefix of length π‘– is a subarray consisting of the first π‘– elements of the original array.

For example, if π‘›=5, π‘š=2, π‘˜=5, and the permutation is [5,3,4,1,2], then:

  • 𝑓(1)=5, because 5β‰₯5; π‘”(1)=0, because 5>2;
  • 𝑓(2)=5, because 3<5; π‘”(2)=0, because 3>2;
  • 𝑓(3)=5, because 4<5; π‘”(3)=0, because 4>2;
  • 𝑓(4)=5, because 1<5; π‘”(4)=1, because 1≀2;
  • 𝑓(5)=5, because 2<5; π‘”(5)=1+2=3, because 2≀2.

Help them find a permutation for which the value of (βˆ‘π‘–=1𝑛𝑓(𝑖)βˆ’βˆ‘π‘–=1𝑛𝑔(𝑖)) is maximized.

Solutions & Walkthrough

Detailed video explanations, mathematical intuition, and clean C++ implementation code.

Connecting secure classroom stream...
Back to Codeforces Round 957
Previous ProblemNext Problem