Tap here to switch tabs
Problem
Submissions

Problem: Maximize Alternating Sum Using Swaps

Maximize Alternating Sum Using Swaps lets you swap values at specified index pairs any number of times. Return the largest possible alternating sum, where even positions add values and odd positions subtract them.

Statement

You are given an integer array nums and a 2D2\text{D} integer array swaps, where swaps[i] =[pi,qi]= [p_i, q_i] indicates that you can swap the elements at indices pip_i and qiq_i any number of times.

The alternating sum of an array nums of length nn is defined as:

Return the maximum possible alternating sum of nums after performing any number of swaps.

Constraints:

  • 1≤1 \leq nums.length ≤105\leq 10^5

  • 1≤1 \leq nums[i] ≤109\leq 10^9

  • 0≤0 \leq swaps.length ≤105\leq 10^5

  • swaps[i].length =2= 2

  • 0≤0 \leq swaps[i][0], swaps[i][1] << nums.length

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Maximize Alternating Sum Using Swaps

Maximize Alternating Sum Using Swaps lets you swap values at specified index pairs any number of times. Return the largest possible alternating sum, where even positions add values and odd positions subtract them.

Statement

You are given an integer array nums and a 2D2\text{D} integer array swaps, where swaps[i] =[pi,qi]= [p_i, q_i] indicates that you can swap the elements at indices pip_i and qiq_i any number of times.

The alternating sum of an array nums of length nn is defined as:

Return the maximum possible alternating sum of nums after performing any number of swaps.

Constraints:

  • 1≤1 \leq nums.length ≤105\leq 10^5

  • 1≤1 \leq nums[i] ≤109\leq 10^9

  • 0≤0 \leq swaps.length ≤105\leq 10^5

  • swaps[i].length =2= 2

  • 0≤0 \leq swaps[i][0], swaps[i][1] << nums.length