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++
#include <bits/stdc++.h>
using namespace std;
void Swap_counts( int N)
{
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()
{
int N = 4;
Swap_counts(N);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void main(String[] args)
{
int N = 4 ;
Swap_counts(N);
}
public static void Swap_counts( int N)
{
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):
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)
if __name__ = = "__main__" :
N = 4
swap_counts(N)
|
C#
using System;
public class GFG {
public static void Main()
{
int N = 4;
Swap_counts(N);
}
public static void Swap_counts( int N)
{
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);
}
}
}
|
Javascript
function swapCounts(N) {
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);
}
}
function main() {
let N = 4;
swapCounts(N);
}
main();
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...