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)
- X–;
- 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++ 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 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);
}
}
} |
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# 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 |
// 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(); |
2
Time Complexity: O(1)
Auxiliary Space: O(1)