Segregate groups of first N numbers having GCD equals to 1
Last Updated :
05 Oct, 2021
Given a number N. The task is to group all the numbers from 1 to N such GCD of all the numbers in each group is 1 as the number of groups must be minimized.
Example:
Input: N = 3
Output:
1 2 3
Explanation:
gcd(1, 2, 3) = 1
Input: N = 6
Output:
1 2
3 4
5 6
Explanation:
gcd(1, 2) = 1
gcd(3, 4) = 1
gcd(5, 6) = 1
Approach: As we know, the fact that GCD of every consecutive number is 1. So we will use this fact to solve this problem. Below are the steps:
- If the number is 3, then only one group is possible {1, 2, 3} whose GCD(1, 2, 3) is 1.
- Else Iterate(say i) over [1, N/2] and form groups using (2*i – 1, 2*i) and store them in Array of Vectors(say arr[][]).
- The groups stored in the arr[][] are the desired groups.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printGroups(vector< int > A[], int N)
{
for ( int i = 1; i <= N / 2 + 1; i++) {
for ( int j = 0; j < A[i].size(); j++) {
cout << A[i][j] << ' ' ;
}
cout << endl;
}
}
void formGroups( int N)
{
int i;
if (N == 3) {
cout << "1 2 3" ;
return ;
}
vector< int > arr[N / 2 + 2];
for (i = 1; i <= N / 2; i++) {
arr[i].push_back(2 * i - 1);
arr[i].push_back(2 * i);
}
if (N & 1) {
arr[i].push_back(N);
}
printGroups(arr, N);
return ;
}
int main()
{
int N = 10;
formGroups(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void printGroups(Vector<Integer> A[],
int N)
{
for ( int i = 1 ; i <= N / 2 + 1 ; i++)
{
for ( int j = 0 ; j < A[i].size(); j++)
{
System.out.print(A[i].get(j) + " " );
}
System.out.println();
}
}
static void formGroups( int N)
{
int i;
if (N == 3 )
{
System.out.print( "1 2 3" );
return ;
}
@SuppressWarnings ( "unchecked" )
Vector<Integer> []arr = new Vector[N / 2 + 2 ];
for ( int k = 0 ; k < arr.length; k++)
arr[k] = new Vector<Integer>();
for (i = 1 ; i <= N / 2 ; i++)
{
arr[i].add( 2 * i - 1 );
arr[i].add( 2 * i);
}
if ((N & 1 ) != 0 )
{
arr[i].add(N);
}
printGroups(arr, N);
return ;
}
public static void main(String[] args)
{
int N = 10 ;
formGroups(N);
}
}
|
Python3
def printGroups(A, N):
for i in range ( 0 , N / / 2 ):
for j in range ( len (A[i])):
print (A[i][j], end = ' ' )
print ()
def formGroups(N):
if (N = = 3 ):
print ( "1 2 3" )
return
arr = []
for i in range ( 1 , N / / 2 + 1 ):
P = []
P.append( 2 * i - 1 )
P.append( 2 * i)
arr.append(P)
if (N & 1 ):
arr.append(N)
printGroups(arr, N)
return
if __name__ = = "__main__" :
N = 10
formGroups(N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void printGroups(List< int > []A,
int N)
{
for ( int i = 1; i <= N / 2 + 1; i++)
{
for ( int j = 0; j < A[i].Count; j++)
{
Console.Write(A[i][j] + " " );
}
Console.WriteLine();
}
}
static void formGroups( int N)
{
int i;
if (N == 3)
{
Console.Write( "1 2 3" );
return ;
}
List< int > []arr = new List< int >[N / 2 + 2];
for ( int k = 0; k < arr.Length; k++)
arr[k] = new List< int >();
for (i = 1; i <= N / 2; i++)
{
arr[i].Add(2 * i - 1);
arr[i].Add(2 * i);
}
if ((N & 1) != 0)
{
arr[i].Add(N);
}
printGroups(arr, N);
return ;
}
public static void Main(String[] args)
{
int N = 10;
formGroups(N);
}
}
|
Javascript
<script>
function printGroups(A,N)
{
for (let i = 1; i <= N / 2 + 1; i++)
{
for (let j = 0; j < A[i].length; j++)
{
document.write(A[i][j] + " " );
}
document.write( "<br>" );
}
}
function formGroups(N)
{
let i;
if (N == 3)
{
document.write( "1 2 3" );
return ;
}
let arr = new Array(Math.floor(N / 2) + 2);
for (let k = 0; k < arr.length; k++)
arr[k] = [];
for (i = 1; i <= N / 2; i++)
{
arr[i].push(2 * i - 1);
arr[i].push(2 * i);
}
if ((N & 1) != 0)
{
arr[i].push(N);
}
printGroups(arr, N);
return ;
}
let N = 10;
formGroups(N);
</script>
|
Output:
1 2
3 4
5 6
7 8
9 10
Time Complexity: O(N), where N is the given number.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...