Given an even number N. The task is to consider numbers from 1 to N^{2}, split them into N groups of the equal sum.**Examples**:

Input:N = 2Output:{1, 4}, {2, 3} Two groups of equal sum are 1, 4 and 2,3Input:N = 4Output:{ 1, 16} { 2, 15} { 3, 14} { 4, 13} { 5, 12} { 6, 11} { 7, 10} { 8, 9}

**Approach:** Formula for sum of first N^{2} numbers: **Sum = (N ^{2} * (N^{2} + 1))/ 2**.

Therefore, the sum of each group would be =

**(N**

^{2}+ 1)* N^{2}/ 2Let us consider pairs of the following type (1, N

^{2}), (2, N

^{2}-1) and so on.

Since N

^{2}is an even number, each group can be made using exactly N/2 such pairs.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to print N groups of equal sum` `void` `printGroups(` `int` `n)` `{` ` ` `int` `x = 1;` ` ` `int` `y = n * n;` ` ` `// No. of Groups` ` ` `for` `(` `int` `i = 1; i <= n; i++) {` ` ` `// n/2 pairs` ` ` `for` `(` `int` `j = 1; j <= n / 2; j++) {` ` ` `cout << ` `"{ "` `<< x << ` `", "` `<< y << ` `"} "` `;` ` ` `x++;` ` ` `y--;` ` ` `}` ` ` `cout << endl;` ` ` `}` `}` `// Driver code` `int` `main()` `{` ` ` `int` `n = 4;` ` ` `printGroups(n);` ` ` `return` `0;` `}` |

## Java

`// Java implementation of the above approach` `import` `java.io.*;` `class` `GFG {` ` ` `// Function to print N groups of equal sum` `static` `void` `printGroups(` `int` `n)` `{` ` ` `int` `x = ` `1` `;` ` ` `int` `y = n * n;` ` ` `// No. of Groups` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i++) {` ` ` `// n/2 pairs` ` ` `for` `(` `int` `j = ` `1` `; j <= n / ` `2` `; j++) {` ` ` `System.out.print(` `"{ "` `+ x + ` `", "` `+ y + ` `"} "` `);` ` ` `x++;` ` ` `y--;` ` ` `}` ` ` `System.out.println();` ` ` `}` `}` `// Driver code` ` ` `public` `static` `void` `main (String[] args) {` ` ` `int` `n = ` `4` `;` ` ` `printGroups(n);` ` ` `}` `}` `// This code is contributed by shs` |

## Python3

`# Python implementation of the above approach` `# Function to print N groups of equal sum` `def` `printGroups(n) :` ` ` ` ` `x ` `=` `1` ` ` `y ` `=` `n ` `*` `n` ` ` ` ` `# No. of Groups` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `) :` ` ` ` ` `# n/2 pairs` ` ` `for` `j ` `in` `range` `(` `1` `, n ` `/` `/` `2` `+` `1` `) :` ` ` ` ` `print` `(` `"{"` `,x,` `","` `,y,` `"}"` `,end ` `=` `" "` `)` ` ` ` ` `x ` `+` `=` `1` ` ` `y ` `-` `=` `1` ` ` ` ` `print` `()` ` ` ` ` `# Driver code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` ` ` `n ` `=` `4` ` ` ` ` `# Function call` ` ` `printGroups(n)` `# This code is contributed by Ryuga` |

## C#

`// Java implementation of the` `// above approach` `using` `System;` `class` `GFG` `{` ` ` `// Function to print N groups` `// of equal sum` `static` `void` `printGroups(` `int` `n)` `{` ` ` `int` `x = 1;` ` ` `int` `y = n * n;` ` ` `// No. of Groups` ` ` `for` `(` `int` `i = 1; i <= n; i++)` ` ` `{` ` ` `// n/2 pairs` ` ` `for` `(` `int` `j = 1; j <= n / 2; j++)` ` ` `{` ` ` `Console.Write(` `"{ "` `+ x + ` `", "` `+ y + ` `"} "` `);` ` ` `x++;` ` ` `y--;` ` ` `}` ` ` `Console.WriteLine();` ` ` `}` `}` `// Driver code` `public` `static` `void` `Main ()` `{` ` ` `int` `n = 4;` ` ` `printGroups(n);` `}` `}` `// This code is contributed by shs` |

## PHP

`<?php` `// PHP implementation of the` `// above approach` `// Function to print N groups` `// of equal sum` `function` `printGroups(` `$n` `)` `{` ` ` `$x` `= 1;` ` ` `$y` `= ` `$n` `* ` `$n` `;` ` ` `// No. of Groups` ` ` `for` `(` `$i` `= 1; ` `$i` `<= ` `$n` `; ` `$i` `++)` ` ` `{` ` ` `// n/2 pairs` ` ` `for` `(` `$j` `= 1; ` `$j` `<= ` `$n` `/ 2; ` `$j` `++)` ` ` `{` ` ` `echo` `"{ "` `, ` `$x` `, ` `", "` `, ` `$y` `, ` `" } "` `;` ` ` `$x` `++;` ` ` `$y` `--;` ` ` `}` ` ` `echo` `"\n"` `;` ` ` `}` `}` `// Driver code` `$n` `= 4;` `printGroups(` `$n` `);` ` ` `// This code is contributed by shs` `?>` |

## Javascript

`<script>` `// Javascript implementation of the above approach` `// Function to prlet N groups of equal sum` `function` `prletGroups(n)` `{` ` ` `let x = 1;` ` ` `let y = n * n;` ` ` `// No. of Groups` ` ` `for` `(let i = 1; i <= n; i++) {` ` ` `// n/2 pairs` ` ` `for` `(let j = 1; j <= n / 2; j++) {` ` ` `document.write(` `"{ "` `+ x + ` `", "` `+ y + ` `"} "` `);` ` ` `x++;` ` ` `y--;` ` ` `}` ` ` `document.write(` `"<br/>"` `);` ` ` `}` `}` `// driver program` ` ` ` ` `let n = 4;` ` ` `prletGroups(n);` ` ` `</script>` |

**Output:**

{ 1, 16} { 2, 15} { 3, 14} { 4, 13} { 5, 12} { 6, 11} { 7, 10} { 8, 9}

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price.

In case you wish to attend live classes with industry experts, please refer **Geeks Classes Live**