Urgent.News

What's breaking now, across hundreds of outlets.

Tech

Array Division | CSES - Algoritmit ongelmanratkaisussa

https://cses.fi/alon/task/1085 #include #include #include using namespace std ; int main ( ) { ios_base :: sync_with_stdio ( false ) ; cin . tie ( nullptr ) ; int n , k ; cin >> n >> k ; vector arr ( n ) ; for ( int i = 0 ; i > arr [ i ] ; } long long lo = * max_element ( arr . begin ( ) , arr . end ( ) ) , hi = 0 ;

The problem at hand concerns the efficient division of an array into subarrays, with the objective of minimizing the maximum sum among those subarrays. This challenge is presented on the CSES platform, a renowned resource for algorithmic problem-solving. The source code provided implements a solution through binary search, a powerful technique for tackling such optimization problems.

To begin, the program initializes several variables and reads the input: an integer 'n' representing the size of the array, followed by 'k', the desired number of subarrays. Then, it populates a vector 'arr' with 'n' elements, each reflecting an integer value from the input. The next step involves determining the search bounds for the binary search algorithm.

The lower bound, 'lo', is assigned the maximum value within the array, while the upper bound, 'hi', is set to zero. If the array size 'n' equals 'k', the maximum element 'lo' is immediately printed as the answer and the program terminates.

For arrays where 'n' is not equal to 'k', the program proceeds to calculate the sum of all elements, storing it in 'hi'. If 'k' equals 1, indicating that the array should be left unsplit, the value of 'hi' is printed, and the program concludes. If neither of these conditions are met, the program enters a binary search loop. In each iteration, it calculates the midpoint 'mid' of the current search range defined by 'lo' and 'hi'.

Here, the goal is to ascertain if it's feasible to partition the array into 'k' or fewer subarrays, with each subarray's sum not exceeding 'mid'.

Within the binary search loop, a new variable 'currSum' is introduced to track the sum of elements within the current subarray being considered. Additionally, a counter 'subArrCnt' is initialized to 1, representing the number of subarrays formed thus far. The program then iterates through the array elements. For each element, it checks if adding it to 'currSum' would exceed 'mid'.

If so, it increments 'subArrCnt', resetting 'currSum' to zero, effectively marking the start of a new subarray. If the number of subarrays exceeds 'k' at any point, the loop breaks out early, as further refinement of 'mid' would not yield a valid solution.

Upon completing the iteration, the program assesses whether the number of subarrays 'subArrCnt' is less than or equal to 'k'. If true, it updates 'minSum' to 'mid' and narrows the search range by setting 'hi' to 'mid - 1', narrowing down to smaller potential values for the maximum subarray sum. Conversely, if 'subArrCnt' exceeds 'k', it implies that 'mid' is too small, and the search range is adjusted by setting 'lo' to 'mid + 1'.

This process repeats until 'lo' and 'hi' converge, at which point 'minSum' holds the smallest possible maximum subarray sum that satisfies the problem's constraints. Finally, the program outputs this value, providing the solution to the division problem.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Tech

Is a cross-encoder reranker worth the latency? Measured on 2,908 FastAPI doc chunks

Tested on: FastAPI English docs (154 Markdown files, 2,908 heading-aware chunks), sentence-transformers 6.3.0, transformers 5.24.1, torch 2.15.0 CPU (1 torch thread, 2-core GitHub runner)…

  • Reranking top 20 candidates improves MRR from 0.710 to 0.889 on FastAPI doc chunks
  • Latency increases proportionally with candidate depth, costing 1,706 ms per query at 20 candidates
  • Reranking benefits natural-language queries but may not improve identifiers and paraphrases

Shrinking an encrypted photo vault on the phone: AES-256-GCM plus HEVC, and no cloud

Most photo vault apps solve one problem: hiding pictures behind a PIN. We wanted to solve two at once on a phone that is already full: keep private photos encrypted on the device, and make that…

  • PhotoVault encrypts photos using AES-256-GCM on device without cloud
  • Lossless mode stores original files, Compact mode uses HEVC re-encode
  • Secure Share transfers vault between devices without cloud involvement

Anyone with an invite link can join your team. Who else has the link?

Anyone with an invite link can join your team. Who else has the link? Team invites are a common feature in apps built with AI tools: a link that lets someone join your workspace.

  • Invite links allow anyone to join your workspace if guessable or never expiring.
  • Short, readable tokens are easier for attackers to guess.
  • Set expiration time, single-use, and low default role for invites.

Kapper 2.1: already delightful for humans, now rocket fuel for agents

Since Kapper 1.6 , we've shipped six releases — and somewhere along the way, the way people write SQL changed. Most SQL is now written by an agent.

  • Kapper 2.1 improves speed with cacheable templates and optimized query path
  • Self-describing errors introduced in 2.0.0 provide clearer messages for humans and agents

Meet in the Middle | CSES - Algoritmit ongelmanratkaisussa

https://cses.fi/alon/task/1628 #include #include #include #include using namespace std ; int dp [ 41 ] [ 41 ] ; int n ; long long x ; long long arr [ 41 ] ; vector getSubsetSum ( int l , int r ) { int…

  • CSES problem-solving platform presents C++ solution
  • Dynamic programming and binary search techniques used
  • Program calculates ways to partition array into subsets with specific sum

More from Sunday, 29 November 2026 →