A non-empty array A consisting of N integers is given.
A peak is an array element which is larger than its neighbors. More precisely, it is an index P such that 0 < P < N − 1, A[P − 1] < A[P] and A[P] > A[P + 1].
For example, the following array A:
A[0] = 1
A[1] = 2
A[2] = 3
A[3] = 4
A[4] = 3
A[5] = 4
A[6] = 1
A[7] = 2
A[8] = 3
A[9] = 4
A[10] = 6
A[11] = 2
has exactly three peaks: 3, 5, 10.
We want to divide this array into blocks containing the same number of elements. More precisely, we want to choose a number K that will yield the following blocks:
A[0], A1, ..., A[K − 1],
A[K], A[K + 1], ..., A[2K − 1],
...
A[N − K], A[N − K + 1], ..., A[N − 1].
What's more, every block should contain at least one peak. Notice that extreme elements of the blocks (for example A[K − 1] or A[K]) can also be peaks, but only if they have both neighbors (including one in an adjacent blocks).
The goal is to find the maximum number of blocks into which the array A can be divided.
Array A can be divided into blocks as follows:
one block (1, 2, 3, 4, 3, 4, 1, 2, 3, 4, 6, 2). This block contains three peaks.
two blocks (1, 2, 3, 4, 3, 4) and (1, 2, 3, 4, 6, 2). Every block has a peak.
three blocks (1, 2, 3, 4), (3, 4, 1, 2), (3, 4, 6, 2). Every block has a peak. Notice in particular that the first block (1, 2, 3, 4) has a peak at A[3], because A[2] < A[3] > A[4], even though A[4] is in the adjacent block.
However, array A cannot be divided into four blocks, (1, 2, 3), (4, 3, 4), (1, 2, 3) and (4, 6, 2), because the (1, 2, 3) blocks do not contain a peak. Notice in particular that the (4, 3, 4) block contains two peaks: A[3] and A[5].
The maximum number of blocks that array A can be divided into is three.
Write a function:
class Solution { public int solution(int[] A); }
that, given a non-empty array A consisting of N integers, returns the maximum number of blocks into which A can be divided.
If A cannot be divided into some number of blocks, the function should return 0.
For example, given:
A[0] = 1
A[1] = 2
A[2] = 3
A[3] = 4
A[4] = 3
A[5] = 4
A[6] = 1
A[7] = 2
A[8] = 3
A[9] = 4
A[10] = 6
A[11] = 2
the function should return 3, as explained above.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [1..100,000];
each element of array A is an integer within the range [0..1,000,000,000].
My Understanding of the problem :
Each sub array should contain at least one peak
An element which forms a peak can be in an Adjacent sub array.
Return max possible sub arrays
My Question
Consider Main Array : [0,1,0,1,0]
Possible sub arrays as per understanding : [0,1] [0,1,0]
Each subarray has a peak.
Subarray 1 [0,1] has peak element shared with adjacent array [0,1,0].
Subarray 2 [0,1,0] contains peak 0<1>0.
So max possible sub arrays are 2 but a test case in Codility returns max possible sub arrays as 1.
Below is my code
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
int count=0,size=A.length;
if(size<2)
return 0;
System.out.println(Arrays.toString(A));
for(int i=1;i<size-1;i++){
if(A[i-1]<A[i] && A[i]>A[i+1]){
count++;
i++;
}
}
return count;
}
}
Test case which failed in Codility click here
I believe there is a gap in my understanding. Any help would be helpful :)
https://app.codility.com/demo/results/training5KP2PK-P4M/
https://github.com/niall-oc/things/blob/master/codility/peaks.py
Breaking an array into even parts is another way of factorizing the length of the array.
Array of length 12
[0,1,0,1,0,0,0,1,0,0,1,0,]
Factors of 12.
The square root of 12 is 3.464... so start with 3 and irterate down to 1 and divide each number into 12. This gives you a set of numbers {1, 2, 3, 4, 6, 12}.
Process
Because of how a peak is defined you cannot have 12 peaks in this array so remove 12 from the set. Starting d as the largest number divide the array int d parts. And check each part has a peak, if so then d is the maximum number of equal parts to all contain at least one peak. If not so then iterate to the next largest divisor and try that until you find a solution.
First of all you need to be congratulated to write a concise program to calculate the number of peaks. But the question is not to count the peaks.
It is to find the number of equal sized array and each having at least one peak. And a peak cannot be the first or last element as stated in the problem 0 < P < N − 1
Now quoting your question:
Consider Main Array : [0,1,0,1,0]
Possible sub arrays as per understanding : [0,1] [0,1,0] Each subarray has a peak. Subarray 1 [0,1] has peak element shared with adjacent array [0,1,0]. Subarray 2 [0,1,0] contains peak 0<1>0.
So max possible sub arrays are 2 but a test case in Codility returns max possible sub arrays as 1.
I see below issues:
your sub array sizes are not equal
array [0,1] does not have a peak
So, the array cannot be divided in equal parts each having a peak and hence only one array remains [0,1,0,1,0].
I have declared a Scanner object named Scan
I want to prompt the user to enter however many items they like:
ie: Enter items: 1 2 6 4 3 12
how do I count how many numbers were entered? For example, the output from above should be 6, since there are 6 numbers
I have tried
int count = 0;
while(Scan.hasNextInt()){
count++ };
You can split the line on whitespace and get the number of parts.
final String line = Scan.nextLine();
if(line.trim().isEmpty()){
System.out.println("Nothing entered");
} else {
final String[] parts = line.split("\\s+");
System.out.println(parts.length);
}
Demo: https://ideone.com/zgr6zM
To convert the parts to an int array, you can use Arrays.stream and .mapToInt.
final int[] nums = Arrays.stream(parts).mapToInt(Integer::parseInt).toArray();
Demo: https://ideone.com/WevrTw
You need to actually call scanner.nextInt() or your scanner's "pointer" will never move past the first number.
jshell> while(scanner.hasNextInt()) {
...> count++;
...> scanner.nextInt();
...> }
1 2 3 4 5 6
jshell> count
count ==> 6
You can read in numbers as follows.
2 3 12 3 1 2
2 29 122 q
The main point is that hasNextInt will continue reading from the console until a non-integer is entered.
Scanner scan = new Scanner(System.in);
List<Integer> numbs = new ArrayList<>();
System.out.println("Enter ints followed by any letter to quit");
while (scan.hasNextInt()) {
numbs.add(scan.nextInt());
}
System.out.println("The following " + numbs.size() + " numbers were entered.");
System.out.println(numbs);
The above sample input would print.
The following 9 numbers were entered.
[2, 3, 12, 3, 1, 2, 2, 29, 122]
This is my code:
class Main{
public static void main(String args[]){
System.out.println("Enter the value of N: ");
Scanner sc = new Scanner(System.in);
int n1 = sc.nextInt();
int max = 0, min = 0;
if(n1<=50){
for(int i=1;i<=n1;i++){
for(int j=1;j<=n1;j++){
max = n1*i;
min = (max-n1)+1;
if(i%2!=0){
while(max<=min){
System.out.print(max);
max--;
}
}
if(i%2==0){
while(min<=min){
System.out.print(min);
min++;
}
}
}
System.out.println("");
}
}
else
System.out.print("Invalid Value of n1");
}
}
the problem is to print a zigzag matrix like
if we enter n=4 then the output should be like:
4 3 2 1
5 6 7 8
12 11 10 9
13 14 15 16
and if we enter 3 it should come like
3 2 1
4 5 6
9 8 7
now in the above code its going to a infinite loop
Considering it is most likely some kind of a homework, I won't hand you a solution as it takes away the learning process. Instead, I'll just give you some hints.
You need 2 nested for loops, the outer one for rows and the inner one for columns.
Find out what the max and min numbers in a given row are. They are connected to the row number.
If the row number is odd, start with the max number and go down. If it is odd, start from the min number and go up.
I would like to know that as I am learning Java from tutorials and there is a program for rolling the dice 1000 times and printing its frequency.
import java.util.Random;
public class RollDicewitharray {
public static void main(String[] args) {
Random r=new Random();
int arr[]= new int[7];
System.out.println("diceNo.\tFrequency");
for (int roll = 1; roll < 1000; roll++) {
++arr[1+r.nextInt(6)]; /* this line */
}
for (int i = 1; i < arr.length; i++) {
System.out.println(i+"\t"+arr[i]);
}
}
To summarise all, this program simulates rolling a six-sided dice 1000 times and records the occurrences of each number rolled.
public static void main(String[] args) {
Random r=new Random();
int arr[]= new int[7]; //Craete an array with 7 int elements
System.out.println("diceNo.\tFrequency");
for(int roll=1;roll<1000;roll++){ //Loop 1000 times
++arr[1+r.nextInt(6)]; //Randomly pick arr[1] to
} //arr[6] and plus one to it
for(int i=1;i<arr.length;i++){
System.out.println(i+"\t"+arr[i]); //Print occurrence of 1-6
}
}
Breaking the following code:
++arr[1+r.nextInt(6)]; //r.nextInt(6) will be evaluated first:
r.nextInt(6) returns random value of (0-5), so you have:
++arr[1+(random 0 to 5)]; //+1 will be evaluated next:
So you are generating a random value of 1-6. Next you add 1 to the array:
++arr[random 1 to 6]; //+1 to arr[1] or arr[2] or arr[3] or arr[4] or arr[5] or arr[6]
It can now be interpreted as:
arr[1] +=1; //or
arr[2] +=1; //or
arr[3] +=1; //or
arr[4] +=1; //or
arr[5] +=1; //or
arr[6] +=1;
So after running your program, if your array looks like:
[0] [1] [2] [3] [4] [5] [6] Array index
+---+---+---+---+---+---+---+
| 0 |175|170|165|170|165|175| <-- arr
+---+---+---+---+---+---+---+
It means 1 was rolled 175 times,
2 was rolled 170 times,
3 was rolled 165 times,
and so on..
r.nextInt(6) produces a random integer between 0 and 5. Adding 1 to it gives you a random dice roll between 1 and 6.
The arr array is used to count the number of times each dice roll occurred, so ++arr[1+r.nextInt(6)] increments the count for the current roll.
When the first loop is done, arr[1] holds the number of 1s, arr[2] the number of 2s, and so on.
1 + r.nextInt(6) draws a random number from 1 to 6 inclusive.
++arr[1 + r.nextInt(6)]; increments that element of the array arr.
So a frequency distribution of dice-rolls is built up. The zeroth element of the array is not used. Which is why it's set up with 7 elements. Wasteful perhaps? You tell me.
r.nextInt(6) - get the random number < 6;
arr[1+r.nextInt(6)] - get an item from the array arr with index 1 + random number from step 1;
++arr[1+r.nextInt(6)] increase the item from step 2 by 1
This line is equivalant to:
int number = r.nextInt(6); // Generates a random number between 0 and 6(exclusive)
int index = number + 1; // Since the faces of dice has value from 1 to 6
int value = arr[index];
and increases this number by 1;