Counting Swaps for Equal Sum Subarrays
Last Updated :
30 Nov, 2023
Given an integer N, consider that there is an array of first N natural numbers, the task is to output the number of swaps such that there should be two subarrays of equal sum.
Note: Each number must be a part of only one subarray. Formally, no number should be there such that it is not a part of any subarray.
Examples:
Input: N = 4
Output: 2
Explanation: Consider array of first N naturals is A[] = {1, 2, 3, 4}. Then, there are two possible swaps, which divides first N natural numbers into two equal sum subarrays.
- First swap: Swap A[0] and A[2], then A[] = {3, 2, 1, 4}. Then A[] can be divided into two subarrays as {3, 2} and {1, 4}. Each subarray has sum equal to 5.
- First swap: Swap A[1] and A[4], then A[] = {1, 4, 3, 2}. Then A[] can be divided into two subarrays as {1, 4} and {3, 2}. Each subarray has sum equal to 5.
There are two possible swaps. Therefore, output is 2.
Input: N = 2
Output: 0
Explanation: It can be verified that there are no valid swaps according to the given problem statement.
Approach: Implement the idea below to solve the problem
The problem is based on the Mathematics and can be solved using the mathematical observations.
Steps were taken to solve the problem:
- If (N%4 == 0 || N%4 == 1), then output 0.
- Else declare a variable let say Cnt and initialize it equal to N*(N+1)/2
- Divide Cnt by 4
- Declare a variable let say X and initialize it equal to Sqrt(2*cnt) + 1
- While((X*(X+1))/2 > cnt)
- Cnt -= (X*(X+1))/2
- Declare a variable let say Ans and initialize it equal to N-X
- If(cnt == 0)
- Ans += X*(X-1)/2 + (N-X)*(N-X-1)/2
- Output the value stored in Ans. n
Below is the implementation of the above approach:
C++
// C++ code to imlement the approach
#include <bits/stdc++.h>
using namespace std;
// Function to calculate the number of swaps
void Swap_counts(int N)
{
// At these conditions, no swaps will be possible
if (N % 4 == 1 || N % 4 == 2)
cout << 0 << endl;
else {
int cnt = N * (N + 1) / 4;
int X = (int)sqrt(2 * cnt) + 1;
while ((X * (X + 1)) / 2 > cnt)
X--;
cnt -= (X * (X + 1)) / 2;
int ans = N - X;
if (cnt == 0)
ans += (X * (X - 1)) / 2
+ ((N - X) * (N - X - 1)) / 2;
cout << ans << endl;
}
}
int main()
{
// Input
int N = 4;
// Function call
Swap_counts(N);
return 0;
}
Java
// Java code to imlement the approach
import java.util.*;
public class Main {
// Driver Function
public static void main(String[] args)
{
// Inputs
int N = 4;
// Function calls
Swap_counts(N);
}
public static void Swap_counts(int N)
{
// At this conditions no
// swaps will be possible
if (N % 4 == 1 || N % 4 == 2)
System.out.println(0);
else {
int cnt = N * (N + 1);
cnt /= 4;
int X = (int)Math.sqrt(2 * cnt) + 1;
while ((X * (X + 1)) / 2 > cnt)
X--;
cnt -= (X * (X + 1)) / 2;
int ans = N - X;
if (cnt == 0)
ans += X * (X - 1) / 2
+ (N - X) * (N - X - 1) / 2;
System.out.println(ans);
}
}
}
Python3
import math
def swap_counts(N):
# At these conditions, no swaps will be possible
if (N % 4 == 1 or N % 4 == 2):
print(0)
else:
cnt = N * (N + 1) // 4
X = int(math.sqrt(2 * cnt)) + 1
while ((X * (X + 1)) // 2 > cnt):
X -= 1
cnt -= (X * (X + 1)) // 2
ans = N - X
if (cnt == 0):
ans += (X * (X - 1)) // 2 + ((N - X) * (N - X - 1)) // 2
print(ans)
# Driver Function
if __name__ == "__main__":
# Inputs
N = 4
# Function calls
swap_counts(N)
#This code is contributed by Rohit Singh
C#
// C# code to implement the approach
using System;
public class GFG {
// Driver Function
public static void Main()
{
// Inputs
int N = 4;
// Function calls
Swap_counts(N);
}
public static void Swap_counts(int N)
{
// At this conditions no
// swaps will be possible
if (N % 4 == 1 || N % 4 == 2)
Console.WriteLine(0);
else {
int cnt = N * (N + 1);
cnt /= 4;
int X = (int)Math.Sqrt(2 * cnt) + 1;
while ((X * (X + 1)) / 2 > cnt)
X--;
cnt -= (X * (X + 1)) / 2;
int ans = N - X;
if (cnt == 0)
ans += X * (X - 1) / 2
+ (N - X) * (N - X - 1) / 2;
Console.WriteLine(ans);
}
}
}
// This code is contributed by ragul21
JavaScript
// Function to calculate the number of swaps
function swapCounts(N) {
// At these conditions, no swaps will be possible
if (N % 4 === 1 || N % 4 === 2) {
console.log(0);
} else {
let cnt = (N * (N + 1)) / 4;
let X = Math.floor(Math.sqrt(2 * cnt)) + 1;
while ((X * (X + 1)) / 2 > cnt) {
X--;
}
cnt -= (X * (X + 1)) / 2;
let ans = N - X;
if (cnt === 0) {
ans += (X * (X - 1)) / 2 + (N - X) * (N - X - 1) / 2;
}
console.log(ans);
}
}
// Driver function
function main() {
// Inputs
let N = 4;
// Function call
swapCounts(N);
}
// Run the program
main();
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Count of subarrays with digit sum equals to X Given an array arr[] of length N and integer X, the task is to count no of subarrays having digit sum equal to X. Examples: Input: arr[] = {10, 5, 13, 20, 9}, X = 6Output: 2Explanation: There are two subarrays which is having digit sum equal to 6. {10, 5} => (1 + 0) + 5 = 6 and {13 , 20} => (1
9 min read
Count Subarrays having Sum K Given an array arr[] of postive and negative integers, the objective is to find the number of subarrays having a sum exactly equal to a given number k.Examples: Input : arr[] = [10, 2, -2, -20, 10], k = -10Output : 3Explanation: Subarrays: arr[0...3], arr[1...4], arr[3...4] have sum equal to -10.Inp
8 min read
Count Subarrays having Sum K Given an array arr[] of postive and negative integers, the objective is to find the number of subarrays having a sum exactly equal to a given number k.Examples: Input : arr[] = [10, 2, -2, -20, 10], k = -10Output : 3Explanation: Subarrays: arr[0...3], arr[1...4], arr[3...4] have sum equal to -10.Inp
8 min read
Count of subarrays with sum at least K Given an array arr[] of size N and an integer K > 0. The task is to find the number of subarrays with sum at least K.Examples: Input: arr[] = {6, 1, 2, 7}, K = 10 Output: 2 {6, 1, 2, 7} and {1, 2, 7} are the only valid subarrays.Input: arr[] = {3, 3, 3}, K = 5 Output: 3 Approach: For a fixed left
6 min read
Count of subarrays with sum at least K Given an array arr[] of size N and an integer K > 0. The task is to find the number of subarrays with sum at least K.Examples: Input: arr[] = {6, 1, 2, 7}, K = 10 Output: 2 {6, 1, 2, 7} and {1, 2, 7} are the only valid subarrays.Input: arr[] = {3, 3, 3}, K = 5 Output: 3 Approach: For a fixed left
6 min read
Count of subarrays whose sum is a perfect square Given an array arr[] with positive and negative elements, the task is to count all subarrays whose sum is a perfect square. Examples: Input: arr[] = {2, 3, -5, 6, -7, 4}; Output: 5 Explanation: Subarrays {2, 3, -5}, {-5, 6}, {3, -5, 6}, {3, -5, 6, -7, 4} and {4} with sum is 0, 1, 4, 1 and 4 respecti
10 min read