Skip to content
Related Articles

Related Articles

Generate minimum sum sequence of integers with even elements greater
  • Last Updated : 31 Jan, 2019

Given an integer N, the task is to generate a sequence of N positive integers such that:

  • Every element at the even position must be greater than the element succeeding it and the element preceding it i.e. arr[i – 1] < arr[i] > arr[i + 1]
  • Sum of the elements must be even and minimum possible (among all the possible sequences).

Examples:

Input: N = 4
Output: 1 2 1 2

Input: N = 5
Output: 1 3 1 2 1

Approach: In order to get the sequence with the minimum sum possible, the sequence must be of the form 1, 2, 1, 2, 1, 2, 1 … and for cases when the sum of the sequence is not even, any 2 from the sequence can be changed to a 3 to make the sum of the sequence even.



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to print the required sequence
void make_sequence(int N)
{
  
    // arr[] will hold the sequence
    // sum variable will store the sum
    // of the sequence
    int arr[N + 1], sum = 0;
  
    for (int i = 1; i <= N; i++) {
  
        if (i % 2 == 1)
            arr[i] = 1;
        else
            arr[i] = 2;
  
        sum += arr[i];
    }
  
    // If sum of the sequence is odd
    if (sum % 2 == 1)
        arr[2] = 3;
  
    // Print the sequence
    for (int i = 1; i <= N; i++)
        cout << arr[i] << " ";
}
  
// Driver Code
int main()
{
    int N = 9;
  
    make_sequence(N);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of above approach
  
class GFG
{
  
// Function to print the required sequence
static void make_sequence(int N)
{
  
    // arr[] will hold the sequence
    // sum variable will store the sum
    // of the sequence
    int[] arr = new int[N + 1];
    int sum = 0;
  
    for (int i = 1; i <= N; i++) 
    {
        if (i % 2 == 1)
            arr[i] = 1;
        else
            arr[i] = 2;
  
        sum += arr[i];
    }
  
    // If sum of the sequence is odd
    if (sum % 2 == 1)
        arr[2] = 3;
  
    // Print the sequence
    for (int i = 1; i <= N; i++)
        System.out.print(arr[i] + " ");
}
  
// Driver Code
public static void main(String[] args)
{
    int N = 9;
    make_sequence(N);
}
}
  
// This code is contributed by iAyushRaj.

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 implementation of above approach
  
# Function to print the required sequence
def make_sequence( N):
  
    # arr[] will hold the sequence
    # sum variable will store the sum
    # of the sequence
    arr = [0] * (N + 1)
    sum = 0
  
    for i in range(1, N + 1):
  
        if (i % 2 == 1):
            arr[i] = 1
        else:
            arr[i] = 2
  
        sum += arr[i]
  
    # If sum of the sequence is odd
    if (sum % 2 == 1):
        arr[2] = 3
  
    # Print the sequence
    for i in range(1, N + 1):
        print(arr[i], end = " ")
  
# Driver Code
if __name__ == "__main__":
      
    N = 9
    make_sequence(N)
  
# This code is contributed by ita_c

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of above approach
using System;
  
class GFG
{
  
// Function to print the required sequence
public static void make_sequence(int N)
{
  
    // arr will hold the sequence
    // sum variable will store the sum
    // of the sequence
    int[] arr = new int[N + 1];
    int sum = 0;
  
    for (int i = 1; i <= N; i++)
    {
        if (i % 2 == 1)
            arr[i] = 1;
        else
            arr[i] = 2;
  
        sum += arr[i];
    }
  
    // If sum of the sequence is odd
    if (sum % 2 == 1)
        arr[2] = 3;
  
    // Print the sequence
    for (int i = 1; i <= N; i++)
        Console.Write(arr[i] + " ");
}
  
// Driver Code
public static void Main()
{
    int N = 9;
  
    make_sequence(N);
}
}
  
// This code is contributed by iAyushRaj.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of above approach 
  
// Function to print the required sequence 
function make_sequence($N
  
    // arr[] will hold the sequence 
    // sum variable will store the sum 
    // of the sequence 
    $arr = array();
    $sum = 0; 
  
    for ($i = 1; $i <= $N; $i++) 
    
        if ($i % 2 == 1) 
            $arr[$i] = 1; 
        else
            $arr[$i] = 2; 
  
        $sum += $arr[$i]; 
    
  
    // If sum of the sequence is odd 
    if ($sum % 2 == 1) 
        $arr[2] = 3; 
  
    // Print the sequence 
    for ($i = 1; $i <= $N; $i++) 
        echo $arr[$i], " "
  
// Driver Code 
$N = 9; 
make_sequence($N); 
  
// This code is contributed by Ryuga
?>

chevron_right


Output:

1 3 1 2 1 2 1 2 1

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :