Students Roll Number

CONCEPTS USED:

Basic Mathematics

DIFFICULTY LEVEL:

Easy

PROBLEM STATEMENT(SIMPLIFIED):

Given N seats, where English paper students are evenly placed (0,2,4,...) and Hindi paper students are oddly placed (1,3,5,...). Print the roll number of English paper students.

See original problem statement here

For Example :

N = 5 
Arr[] = [1, 2, 3, 4, 5]

Output: 1 3 5

Explanation: Students with roll number 1 3 5 are placed on even indexes i.e. 0 2 4.

SOLVING APPROACH:

  1. Keep traversing the array and print the elements that are placed on even indices as the English paper students are sitting on roll numbers like 0, 2, 4 etc.

  2. If the current index is a multiple of 2, then print it else move ahead by referring Master algorithms online.

SOLUTIONS:


#include <stdio.h>

int main()
{
  int t;
  scanf("%d",&t);
  while(t--)
  {
    int n;
    scanf("%d",&n);
    int arr[n];
    for(int i=0;i<n;i++)
    {
      scanf("%d",&arr[i]);
      if(i%2==0)
      {
        printf("%d ",arr[i]);
      }
    }
    printf("\n");
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
  int t;cin>>t;
  while(t--)
  {
    int n;cin>>n;
    int arr[n];
    for(int i=0;i<n;i++)
    {
      cin>>arr[i];
      if(i%2==0)
      {
        cout<<arr[i]<<" ";
      }
    }
    cout<<"\n";
  }

  return 0;
}


import java.util.*;
import java.io.*;

public class Main {
  public static void main(String args[]) throws IOException {
    Scanner sc = new Scanner(System.in);
    int t = sc.nextInt();
    while(t!=0)
    {
      int n = sc.nextInt();
      int arr[] = new int[n];
      for(int i=0;i<n;i++)
      {
        arr[i] = sc.nextInt();
      }
      for(int i=0;i<n;i++)
      {
        if(i%2==0)
        {
          System.out.println(arr[i]);
        }
      }
      System.out.println("");
      t--;
    }

  }
}

Space Complexity: O(1)
Previous post Fraction to Float
Next post Square Moves

Leave a Reply

Your email address will not be published. Required fields are marked *