TCS: [Python Question] Find possible ways with a maximum difference between daily temperature | Python-3 Solution by APDaga

I came across a Python 3 question by HackerRank asked for Tata Consultancy Services [TCS] technical round. 

Here, I am providing my solution to the problem "Find possible ways with a maximum difference between daily temperature" with the intention to help you learn and understand Python 3 in a better way.

Please make use of my blog posts for learning purposes only and feel free to ask your questions in the comment box below in case of any doubt.

Click Here to check out all Python Tutorials by APDaga.

Recommended Python Courses:
1. Udemy: Top Python Courses
2. Udemy: 2021 Complete Python Bootcamp From Zero to Hero in Python
3. Udemy: Learn Python Programming Masterclass
4. Coursera: Python for Everybody
5. LinkedIn: Become a Python Developer
6. edX: Programming for Everybody (Getting Started with Python)
7. edureka: Python Programming Certification Training
8. Eduonix: Mighty Python Bundle

Python Problem Description:

The daily temperatures of 'N' days are recorded as array elements a[]. for the data analysis of the temperatures given, it is required to find 2 days' temperatures such that their difference is maximum. 

The task here is to find a value T, where T constitutes the possible ways of choosing two days so as to get the maximum difference between temperatures of 2 days.




For example 1:

Input:

  • 6 -> Value of N
  • {45, 45, 15, 22, 15, 47} -> a[], Elements a[0] to a[N-1], where each input element is seperated by a new line.

Output:

  • 2

Explanation:

  • From the input given above:
  • Number of days the temperature is recorded: 6
  • The maximum difference in temperature between 2 days if we choose 47 and 15 will be, i.e. 47 - 15 = 32
  • Possible ways to choose 47 and 15 are:
    • Choose a[5] and a[4]
    • Choose a[5] and a[2]

  • There are 2 ways to choose the days with maximum temperature difference.
    Hence, the output is 2.




For example 2:

Input:

  • 6 -> Value of N
  • {30, 12, 45, 45, 12, 12} -> a[], Elements a[0] to a[N-1], where each input element is seperated by a new line.

Output:

  • 6

Explanation:

  • From the input given above:
  • Number of days the temperature is recorded: 6
  • The maximum difference in temperature between 2 days if we choose 45 and 12 will be, i.e. 45 - 12 = 33
  • Possible ways to choose 45 and 12 are:
    • Choose a[2] and a[1]
    • Choose a[2] and a[4]
    • Choose a[2] and a[5]
    • Choose a[3] and a[1]
    • Choose a[3] and a[4]
    • Choose a[3] and a[5]

  • There are 6 ways to choose the days with maximum temperature difference.
    Hence, the output is 6.


Constraints:

  • 0 < N <= 100
  • 0 < a[i] <= 60


Input format for testing:

The candidate has to write the code to accept two input(s):

  • First Input: Accept values for N (positive integer number).
  • Second Input: Accept N number of positive integer values (a[]), where each value is separated by a new line.

Output format for testing:

  • The output should be a positive integer number (check the output in Example 1 and Example 2).
  • Additional messages in the output will result in the failure of test cases.

Instructions:

  • The system does not allow any kind of hard-coded input value/values.
  • The written program code by the candidate will be verified against the inputs that are supplied from the system.

Python 3 Solution:







Output:

[Case-1] Output:

max_diff = 32
count = 2


[Case-2] Output:

max_diff = 33
count = 6


--------------------------------------------------------------------------------

Keep learning and make good use of what you learn. 
Feel free to share your thoughts about this post in the comment box below. 
and always remember
"With great power & knowledge comes great responsibility"

--------------------------------------------------------------------------------

Click here for a free Python Tutorial (Basic to Advance).
&
Click here to learn Handwriting Analysis in detail for free.
&
Click here to see solutions for all Machine Learning Coursera Assignments.
&
Click here to see more codes for Raspberry Pi 3 and similar Families.
&
Click here to see more codes for NodeMCU ESP8266 and similar Families.
&
Click here to see more codes for Arduino Mega (ATMega 2560) and similar Families.

Feel free to ask doubts in the comment section. I will try my best to answer it.
If you find this helpful by any means, like, comment, and share the post.
This is the simplest way to encourage me to keep doing such work.

Thanks & Regards,
-Akshay P Daga

إرسال تعليق (0)
أحدث أقدم