0

Help with homework

I tried to find solution and logic in task but I didn't. My task is: Write the program which printout the first temperature which is repeating or if it's not the printout "NO TEMPERATURE IS REPEATING". Input is a array of N element. Elements of array is temperature change. INPUT: FIRST LINE: it's entered number of elements N SECOND LINE: elements of array OUTPUT: The first temperature which is repeating or if it's not the printout "NO TEMPERATURE IS REPEATING". Examples: INPUT: 4 1 1 10 -9 OUTPUT: 12 EXPLANATION: C: +1 +1 +10 –9 | +1 +1 +10 –9 |… | +1 +1 +10 –9 T: 0 +1 +2 +12 +3 | +4 +5 +15 +6 |… |+12 +13 +23 +14 C– change, T – temperature Example 2: INPUT: 3 3 -1 5 OUTPUT: NO TEMPERATURE IS REPEATING EXPLANATION: C: +3 -1 +5 | +3 -1 +5 |… T: 0 +3 +2 +7 | +10 +9 +14 |… Elements of array can be between -10^9 and 10^9 Time limit: 1s

10th Jul 2019, 12:06 PM
Cлaвeн Ђервида
Cлaвeн Ђервида - avatar
20 Answers
+ 2
Cлaвeн Ђервида Hm...I need to think about it.
10th Jul 2019, 1:43 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
Tried asking a classmate
10th Jul 2019, 12:09 PM
Lexfuturorum
Lexfuturorum - avatar
+ 1
You can try this for larger inputs on your pc: https://code.sololearn.com/cvZppd63JuQd/?ref=app Maybe you need to change the data type: long instead of integer
10th Jul 2019, 1:59 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
You only need to add a scanner. import java.util.Scanner; Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int[] arr = new int[n]; for(int i = 0; i < arr.length; i++){ arr[i] = scan.nextInt(); }
10th Jul 2019, 2:07 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
It is a code which returns the first repeating number in an array. I would say using hashing is the best way to do it. The rest of your homework should be not a problem for you. You can also use another language if you are not familiar with java.
10th Jul 2019, 2:30 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
You can try my code again but I would prefer Diegos solution. https://code.sololearn.com/cvZppd63JuQd/?ref=app
10th Jul 2019, 5:17 PM
Denise Roßberg
Denise Roßberg - avatar
0
Lexfuturorum Well, no body know to solve it.
10th Jul 2019, 12:13 PM
Cлaвeн Ђервида
Cлaвeн Ђервида - avatar
0
Here is a small code for getting the first repeating number. https://code.sololearn.com/c4RlQustL6Ga/?ref=app
10th Jul 2019, 1:37 PM
Denise Roßberg
Denise Roßberg - avatar
0
Denise Roßberg I know that. It's easy. But we have test cases and array has 999 999 999 elements
10th Jul 2019, 1:41 PM
Cлaвeн Ђервида
Cлaвeн Ђервида - avatar
0
I have been thinking and trying 1 week but nothing.😔
10th Jul 2019, 1:45 PM
Cлaвeн Ђервида
Cлaвeн Ђервида - avatar
0
Denise Roßberg I have to create a array it's not a input I input example: 2 -9999999999 999999998 OUTPUT is -9999999999
10th Jul 2019, 2:02 PM
Cлaвeн Ђервида
Cлaвeн Ђервида - avatar
0
I don't know so much Java but do you just input and elements of array in this last code?
10th Jul 2019, 2:09 PM
Cлaвeн Ђервида
Cлaвeн Ђервида - avatar
0
I added a scanner for user input. https://code.sololearn.com/cvZppd63JuQd/?ref=app Enter a number for n and then some values. 4 4 33 55 33
10th Jul 2019, 2:22 PM
Denise Roßberg
Denise Roßberg - avatar
0
Denise Roßberg OK, but this is not solution,is it?
10th Jul 2019, 2:24 PM
Cлaвeн Ђервида
Cлaвeн Ђервида - avatar
0
Denise Roßberg I have 2 array if You don't understand. I create second array by fist array. Example 4 1 1 10 -9 Array 2: Frist time: 1 2 12 3 Second time 1 2 12 3 4 5 15 6 .... 1 2 12 3 4 4 15 6 7 8 18 9 10 11 21 12 ^ ^
10th Jul 2019, 2:37 PM
Cлaвeн Ђервида
Cлaвeн Ђервида - avatar
0
And what has your input to do with theese two arrays?
10th Jul 2019, 2:45 PM
Denise Roßberg
Denise Roßberg - avatar
0
Denise Roßberg First array is temperature changes Start temperature is 0 4 1 1 10 -9 First change is 1 0+1=1 Second change is 1+1=2 Third change is 2+10=12 Four change is 12-9=3 And again from start of first array charging temperature. But it some logic to find when it's not possible to be repeated.
10th Jul 2019, 2:50 PM
Cлaвeн Ђервида
Cлaвeн Ђервида - avatar
0
Interesting challenge. Here's how I would solve it. 1. Calculate all partial sums. Input: [1, 1, 10, -9] Partial sums: [1, 2, 12, 3] 2. Let N be the last partial sum (i.e. the sum of all the elements). Calculate the residue of all elements (including the starting 0) modulo N (if N<0 take the absolute value). Ps = [1, 2, 12, 3] mod3 = [1, 2, 0, 0] There won't be a repeated temperature if and only if all residues are different. Finding the smallest repeated numbers is easy. I leave it up to you.
10th Jul 2019, 3:00 PM
Diego
Diego - avatar
0
Can you explain how I get 12 in Your first comment
10th Jul 2019, 3:24 PM
Cлaвeн Ђервида
Cлaвeн Ђервида - avatar
0
prefix sum i binary searchas rezultat
2nd Mar 2020, 8:09 PM
Slavisa
Slavisa - avatar