Rearrange the array to maximize the number of primes in prefix sum of the array
Given an array arr[] of 1’s and 2’s, the task is to re-arrange the array in such a way that the prefix sum of the rearranged array has the maximum number of primes. Note that there can be multiple answers to it.
Examples:
Input: arr[] = {1, 2, 1, 2, 1}
Output: 2 1 1 1 2
The prefix sum array is {2, 3, 4, 5, 7} which has {2, 3, 5, 7} as primes
which is the maximum possible.
Input: arr[] = {1, 1, 2, 1, 1, 1, 2, 1, 1}
Output: 2 1 1 1 1 1 1 1 2
Approach: The problem can be solved with two observations, one is the first prime is 2, and all other primes after that are odd numbers (All odd numbers are not prime). Hence simply fill the first position with 2 if there are any, and then fill an odd number of ones, and then fill the remaining 2’s. At the end insert the only 1 left (if the initial number of ones were even).
In doing so, we start from 2 and end at an odd number by adding an odd number of 1’s and then by adding 2’s to it, we jump from an odd number to another odd number which maximizes the probability of primes.
Algorithm:
Step 1: Initialize two variables ones and twos to 0 to count the number of “ones” and “twos” in the input array.
Step 2: Loop through the input array a from index 0 to n-1.
a. If the current element at index i is 1, increment “ones” by 1. Otherwise, if the current element is 2, increment “twos” by 1.
Step 3: In order to maintain track of the current index of the rearranged array, initialise the index variable “ind” to 0.
Step 4: If there is at least one 2 in the input array, set the first element of the rearranged array to 2 and increment “ind” by 1.
Step 5: See if there are more even or fewer odd “ones” than ones. Subtract one from “ones” and set the boolean variable evenOnes to true if the result is even. In any other case, set “evenOnes” to false.
Step 6: Fill up the rearranged array with ones until the count of “ones” becomes odd.
Step 7: Fill up the rearranged array with “twos” except for the last position.
Step 8: Set the final element of the rearranged array to 1 if “evenOnes” is true.
Step 9: Print the rearranged array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void solve( int a[], int n)
{
int ones = 0, twos = 0;
for ( int i = 0; i < n; i++) {
if (a[i] == 1)
ones++;
else
twos++;
}
int ind = 0;
if (twos)
a[ind++] = 2;
bool evenOnes = (ones % 2 == 0) ? true : false ;
if (evenOnes)
ones -= 1;
for ( int i = 0; i < ones; i++)
a[ind++] = 1;
for ( int i = 0; i < twos - 1; i++)
a[ind++] = 2;
if (evenOnes)
a[ind++] = 1;
for ( int i = 0; i < n; i++)
cout << a[i] << " " ;
}
int main()
{
int a[] = { 1, 2, 1, 2, 1 };
int n = sizeof (a) / sizeof (a[0]);
solve(a, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void solve( int a[], int n)
{
int ones = 0 , twos = 0 ;
for ( int i = 0 ; i < n; i++) {
if (a[i] == 1 )
ones++;
else
twos++;
}
int ind = 0 ;
if (twos > 0 )
a[ind++] = 2 ;
boolean evenOnes = (ones % 2 == 0 ) ? true : false ;
if (evenOnes)
ones -= 1 ;
for ( int i = 0 ; i < ones; i++)
a[ind++] = 1 ;
for ( int i = 0 ; i < twos - 1 ; i++)
a[ind++] = 2 ;
if (evenOnes)
a[ind++] = 1 ;
for ( int i = 0 ; i < n; i++)
System.out.print(a[i] + " " );
}
public static void main(String[] args)
{
int a[] = { 1 , 2 , 1 , 2 , 1 };
int n = a.length;
solve(a, n);
}
}
|
Python
def solve(a, n):
ones, twos = 0 , 0
for i in range (n):
if (a[i] = = 1 ):
ones + = 1
else :
twos + = 1
ind = 0
if (twos):
a[ind] = 2
ind + = 1
if ones % 2 = = 0 :
evenOnes = True
else :
evenOnes = False
if (evenOnes):
ones - = 1
for i in range (ones):
a[ind] = 1
ind + = 1
for i in range (twos - 1 ):
a[ind] = 2
ind + = 1
if (evenOnes):
a[ind] = 1
ind + = 1
for i in range (n):
print (a[i],end = " " )
a = [ 1 , 2 , 1 , 2 , 1 ]
n = len (a)
solve(a, n)
|
C#
using System;
class GFG
{
static void solve( int []a, int n)
{
int ones = 0, twos = 0;
for ( int i = 0; i < n; i++)
{
if (a[i] == 1)
ones++;
else
twos++;
}
int ind = 0;
if (twos > 0)
a[ind++] = 2;
bool evenOnes = (ones % 2 == 0) ? true : false ;
if (evenOnes)
ones -= 1;
for ( int i = 0; i < ones; i++)
a[ind++] = 1;
for ( int i = 0; i < twos - 1; i++)
a[ind++] = 2;
if (evenOnes)
a[ind++] = 1;
for ( int i = 0; i < n; i++)
Console.Write(a[i] + " " );
}
static public void Main ()
{
int []a = { 1, 2, 1, 2, 1 };
int n = a.Length;
solve(a, n);
}
}
|
PHP
<?php
function solve( $a , $n )
{
$ones = 0; $twos = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $a [ $i ] == 1)
$ones ++;
else
$twos ++;
}
$ind = 0;
if ( $twos )
$a [ $ind ++] = 2;
$evenOnes = ( $ones % 2 == 0) ? true : false;
if ( $evenOnes )
$ones -= 1;
for ( $i = 0; $i < $ones ; $i ++)
$a [ $ind ++] = 1;
for ( $i = 0; $i < $twos - 1; $i ++)
$a [ $ind ++] = 2;
if ( $evenOnes )
$a [ $ind ++] = 1;
for ( $i = 0; $i < $n ; $i ++)
echo $a [ $i ], " " ;
}
$a = array ( 1, 2, 1, 2, 1 );
$n = count ( $a );
solve( $a , $n );
?>
|
Javascript
<script>
function solve(a, n)
{
let ones = 0, twos = 0;
for (let i = 0; i < n; i++)
{
if (a[i] == 1)
ones++;
else
twos++;
}
let ind = 0;
if (twos > 0)
a[ind++] = 2;
let evenOnes = (ones % 2 == 0) ? true : false ;
if (evenOnes)
ones -= 1;
for (let i = 0; i < ones; i++)
a[ind++] = 1;
for (let i = 0; i < twos - 1; i++)
a[ind++] = 2;
if (evenOnes)
a[ind++] = 1;
for (let i = 0; i < n; i++)
document.write(a[i] + " " );
}
let a = [ 1, 2, 1, 2, 1 ];
let n = a.length;
solve(a, n);
</script>
|
Time Complexity: O(n), where n is the size of the given array
Auxiliary Space: O(1), as no extra space is used
Last Updated :
25 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...