Showing posts with label Hip Hip Array. Show all posts
Showing posts with label Hip Hip Array. Show all posts

Wednesday, June 29, 2022

Hip Hip Array

 Hip Hip Array

Problem Statement
You will be given an array of N numbers. Your task is to first reverse the array (first number becomes last, 2nd number becomes 2nd from the last and so on) and then print the sum of the numbers at even indices and print the product of the numbers at odd indices.




import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework

// don't change the name of this class
// you can add inner classes if needed
class Main 
{
    public static void main (String[] args) 
    {
     Scanner sc=new Scanner(System.in);
     int sum=0;
     long product=1;
     int N=sc.nextInt();
     if(N>0)
     {
     int arr[]=new int[N];
     //array to store new elements
     int arr1[]=new int[N];
     for(int i=0;i<N;i++)
     {
         arr[i]=sc.nextInt();
     }
     for(int i=0;i<N;i++)
     {
         arr1[i]=arr[(N-1)-i];
     }
     for(int i=1;i<N;i+=2)
     {
         sum=sum+arr1[i];
     }
        System.out.print(sum+" ");
    for(int i=0;i<N;i+=2)
    {
        product=product*arr1[i];
    }
    System.out.print(product);
     
    }
}}

ads vert

Basic HTML Tables - Layout, HTML Tables, Attributes, Aside, Footer, Tr tag, Td tag, Th tag, Tbody

  Basic HTML Tables - Layout, HTML Tables, Attributes, Aside, Footer, Tr tag, Td tag, Th tag, Tbody < table >      < thead >    ...