Print N distinct numbers following the given operations
Last Updated :
09 Nov, 2021
Given an integer N which is always an even number, our task is to print N distinct numbers following the given conditions:
- The first half numbers are even whereas the other half numbers are odd
- Sum of the elements of first half numbers and sum of elements of second half numbers should be equal
Print the array if the above conditions satisfy otherwise output “-1”.
Examples:
Input: N = 4
Output: 2 4 1 5
Explanation:
Given number 4 we are required to print 4 numbers. First Half = 2, 4 and their sum is 6, other half = 1, 5 and their sum is also 6.
Input: N = 22
Output: -1
Explanation:
It is not possible to print the required array.
Approach:
To solve the problem mentioned above we have to observe that the integer N has to be a multiple of 4.
- We know that the sum of the first N/2 even numbers will be even, so if the sum of the other N/2 integers is also even then N/2 must be even, because the sum of an odd number of odd integers is always odd.
- If N/2 is even then N is a multiple of 4, so if n is not divisible by 4 then the answer is “-1”, otherwise, there will be a possible array.
- For printing the array we will consider two parts such that the first half that is N/2 elements will be simply multiples of 2 and the other half will be multiple of 2 – 1. For the last element in the array, we will calculate the integer by applying the direct formula N + N / 2 – 1 because we are supposed to make the sum for two halves equal.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool printArr( int n)
{
if (n % 4 == 0) {
for ( int i = 1; i <= n / 2; i++)
cout << i * 2 << ' ' ;
for ( int i = 1; i < n / 2; i++)
cout << i * 2 - 1 << ' ' ;
cout << n + n / 2 - 1 << '\n' ;
}
else
cout << "-1" ;
}
int main()
{
int n = 22;
printArr(n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void printArr( int n)
{
if (n % 4 == 0 )
{
for ( int i = 1 ; i <= n / 2 ; i++)
System.out.print(i * 2 + " " );
for ( int i = 1 ; i < n / 2 ; i++)
System.out.print(i * 2 - 1 + " " );
System.out.println(n + n / 2 - 1 );
}
else
System.out.print( "-1" );
}
public static void main(String[] args)
{
int n = 22 ;
printArr(n);
}
}
|
Python3
def printArr(n):
if (n % 4 = = 0 ):
for i in range ( 1 , (n / 2 ) + 1 ):
print (i * 2 , end = " " )
for i in range ( 1 , n / 2 ):
print (i * 2 - 1 , end = " " )
print (n + n / 2 - 1 , end = "\n" )
else :
print ( "-1" )
n = 22
printArr(n)
|
C#
using System;
public class GFG{
static void printArr( int n)
{
if (n % 4 == 0)
{
for ( int i = 1; i <= n / 2; i++)
Console.Write(i * 2 + " " );
for ( int i = 1; i < n / 2; i++)
Console.Write(i * 2 - 1 + " " );
Console.WriteLine(n + n / 2 - 1);
}
else
Console.Write( "-1" );
}
public static void Main(String[] args)
{
int n = 22;
printArr(n);
}
}
|
Javascript
<script>
function printArr(n)
{
if (n % 4 == 0) {
for ( var i = 1; i <= n / 2; i++)
document.write( i * 2 + ' ' );
for ( var i = 1; i < n / 2; i++)
document.write( i * 2 - 1 + ' ' );
document.write( n + n / 2 - 1 + '<br>' );
}
else
document.write( "-1" );
}
var n = 22;
printArr(n);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...