Programs on Arrays

  1. Given a list of N (1 <= N <= 50000) integer. You are required to compute the number of pairs of indexes whose values are the same, and also the maximum distance between two indexes with equal values. N will be given in the first line of the input. N lines follow each one with one integer from the list. If no identical pairs are found, print NA, otherwise print a single line with two integers, the number of identical pairs, and the maximum distance between indexes with equal values.

    Sample Input1:
    6
    1
    2
    3
    2
    4
    2
    Sample Output1:
    2 4
    Sample Input2:
    4
    1
    2
    3
    4
    Sample Output2:
    NA

    Click Here for Solution

  2. The e-commerce company ‘Easy Shopping’ displays some specific products for its premium customers on its user interface. The company shortlisted a list of N products. The list contains the price of each product. The company will select random products for display. The selection criteria states that only those products whose price is a perfect cube number will be selected for display. The selection process is automated and is done by the company’s system. The company wishes to know the total count of the products selected for display.
    Write an algorithm to find the count of products that will be displayed.

    Input Format:
    The first line of the input consists of an integer – numProducts, representing the number of products(N).
    The second line consists of N space-separated integers – price0, price1,…,priceN-1 representing the product price of the N shortlisted products.
    Output Format:
    Print an integer representing the number of products that will be selected for display.
    Output Format:
    0<=numProducts<=10^6
    0<=price<=10^6
    Sample Input:
    6
    125 216 54 81 48 343
    Sample Output:
    3

    Click Here for Solution

  3. You are given an array Arr of N integers. You need to find the length of longest Peak Subarray of the given array Arr.
    Peak Array: Array A of length K is called as a peak Array, if following condition holds true:
    A[1]<A[2]<A[3]<….<A[K].
    Formally any strictly increasing array is called a Peak Array.
    Subarray: Subarray of any array is a contiguous part of the array (with length greater than zero).
    Your task is to find the length of the longest peak subarray of a given array.
    Note: Array of length 1 is always a Peak Array.

    Input Format:
    First line of input, single integer value N
    Second line of input, contains N integers with space-separation for Array Arr.
    Output Format:
    Print an integer that represents the length of longest peak subarray.
    Sample Input:
    5
    3 3 9 8 4
    Sample Output:
    2
  4. Suppose, for a stock, we have a series of N daily price quotes. For each day, the task is to find how many such continuous days (including current day) are there prior to the current day where the price of the stock was less than or equal to the price on the current day.
    Hint: Start counting continuously from the current day to all days before it; stop when the stock is higher than the current day.
    For example, 6 day’s stock prices are as follows: {100, 60, 70, 65, 80, 85}

    Day 1 2 3 4 5 6
    Stock Price 100 60 70 65 80 85
    Output 1 1 2 1 4 5
    Input Format:
    First input, the candidate has to write the code to accept a single integer value N denoting the number of days.
    Second input, the code should accept N number of integer values separated by a space.
    Output Format:
    Output should be N integer values from the list separated by a single space character.
    Constraints:
    2 <= N <= 100 0 <= L[i] <= 100000
    Sample Input:
    6
    100 60 70 65 80 85
    Sample Output:
    1 1 2 1 4 5

    Click Here for Solution

  5. Kenny loves Coca Cola. There are ‘n’ different shops in his colony from which he can buy coca cola. Each shop has different rate for the same coca cola bottle. Kenny decided to buy his favorite cool drink for ‘m’ continuous days. Each day Kenny has ‘p’ money in his hand. Find out, from how many shops, Kenny can buy his favorite cool drink each day.

    Function Description
    Complete the function FindTotalShops()
    Parameters
    • input1 – Total number of shops in the colony
    • input2 – Rate of cool drink in each shop
    • input3 – Number of days, Kenny is going to buy the cool drink
    • input4 – Total money, Kenny has with him to buy cool drink, each day
    Return
    Number of shops, in which, Kenny can buy cool drink, each day.
    Sample Input
    5
    3 10 8 6 11
    4
    1 10 3 11
    Sample output
    0 4 1 5

    Click Here for Solution

  6. Given an integer array as input perform the following operations on the array, in the below specified sequence:

    1. Find the minimum number in the array
    2. Add the minimum number to each element of the array
    3. Multiply the minimum number to each element of the resultant array.
    Function Description
    Implement the function MinPlusProduct()
    Parameters
    • int input1: represents number of elements in array
    • int input2[]: represents an array contains elements
    Return
    Return the resultant array after performing sequence of steps.
    Sample Input1
    4
    1 5 6 9
    Sample output1
    2 6 7 10
    Sample Input2
    5
    10 87 63 42 2
    Sample output2
    24 178 130 88 8

    Click Here for Solution

  7. Given an array of numbers, you are expected to return the sum of the longest sequence of negative numbers in the array. If there are no negative numbers in the array. You are expected to return -1.
    In this question’s scope, the number 0 should be considered as positive.
    Note: If there are more than one group of elements in the array having the longest sequence of negative numbers, you are expected to return the total sum of all of those negative numbers.

    Function Description
    Implement function SumOfLongestNegativeNumberSequence()
    Parameters
    • int input1: represents the number of elements in the array
    • int input2[]: represents the array of integers
    Return
    Return an integer
    Sample Input1
    16
    -13 -15 13 19 18 14 -5 11 -13 32 -5 -1 -77 78 -79
    Sample output1
    -116
    Sample Input2
    13
    22 25 7 9 0 21 16 26 18 94 62 14 100
    Sample output2
    -1
    Sample Input3
    16
    31 -33 -25 -97 13 18 -13 -15 -17 7 4 32 -9 -7 78 -79
    Sample output3
    -200

    Click Here for Solution