Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int c[] = new int[n];
for(int c_i=0; c_i < n; c_i++){
c[c_i] = in.nextInt();
}
Arrays.sort(c);
int t=0;
for (int i=0;i<n-1;i++){
if(c[i]==c[i+1]){
t++;
i++;
}
}
System.out.println(t);
}
}
When I am removing i++ from if condition and making i=i+2 in for loop, the output is changing for certain test cases. Can someone explain me the reason as in both conditions i is incrementing by 2.
The i++ inside your loop's body is only performed if c[i]==c[i+1], so i is incremented by 1 in some iterations and by 2 in other iterations.
On the other hand, the loop's increment is always performed, so if the loop's increment is changed to i+=2 (and the i++ inside the loop's body is removed), i would be incremented by 2 in every iteration.
Therefore
for (int i=0;i<n-1;i++){
if(c[i]==c[i+1]){
t++;
i++;
}
}
is not equivalent to
for (int i=0;i<n-1;i+=2){
if(c[i]==c[i+1]){
t++;
}
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 days ago.
Improve this question
I need to sort by cakes made then if if equal by alphabetical order
I need this sorting method to firstly sort my cakesMade which is an int and then secondly by name if cakesmade is equal. The code is working for the cakesMade, I just cant seem to get the second sort by name to work. Could someone please help
public int bubbleSort (){
for (int i = 0; i < team.size(); i++){
for (int j = 0; j < team.size() - 1;j++ ){
Employee t1 = team.get(j);
Employee t2 = team.get(j+1);
if (t1.getCakesMade() < t2.getCakesMade()) {
team.set(j, t2);
team.set(j + 1, t1);
} else if (t1.getCakesMade() == t2.getCakesMade()) {
if (t1.getName().compareTo(t2.getName()) < 0){
team.set(j, t2);
team.set(j + 1, t1);
}
}
}
}
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to print a certain triangle in Java by using Nested Loops and I am having difficulty. Could somebody give me a hand or just show me how it's done?
The triangle is:
123456654321
1234554321
12344321
123321
1221
11
I can print a triangle like
123456
12345
1234
123
12
1
Though I'm not sure how to reverse and make my loops count down afterwards.
This works:
public class Main {
public static void main(String args[]) {
int n = 6;
while (n > 0) {
for (int i = 1; i <= n; i++) {
System.out.print(i);
}
for (int i = n; i > 0; i--) {
System.out.print(i);
}
System.out.println("");
n--;
}
}
}
The second for loop assigns n to the iterator int i, executes the statements within the curly brackets, then decrements i using i-- until the condition i > 0 is no longer true.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
public class MultiTest1 {
public static void main(String[] args) {
int [][] lottogroups = {{1111,2222,3333,4444},{6666,7777,8888,9999},{11111,121212,131313}};
for(int i = 0;i < lottogroups.length ;i++){
System.out.println("Group :"+i);
for(int j = 0;j < lottogroups[i].length ;j++){
System.out.println(" Value "+j+" = "+lottogroups[i][j]);
}
}
}
}
There is a simple trick to find out whether a number is even, etc.
The % operator helps to see whether a number is divisible by a certain number. Even number are divisible by 2.
if (lottogroups[i][j] % 2 == 0) // number is even
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
public int[] factors(int n) {
int count[] = new int[n];
for (int i = 1; i <= count.length; i++) {
count[i] = (count.length % i);
}
return count;
}
}
Anyone have an idea how to list all the factors?
set int i = 0; as array starts from 0
Factor can only be considered if value % i==0 so add if condition
You need one more variable to increment array Index as you can't use i for that.So add one more variable to increment array index to add factor.
Think more on your code, debug especially if possible,check out the values ,add print statements if needed to see what's going on and that's it you will definitely win the task.
Other than that if you can use SOF you can use Google as well :)
You should use a List instead of array. Since you can't initialize the array without knowing the size.
Then you can try something like following
public List<Integer> factors(int n) {
List<Integer> list = new ArrayList<>(); // initialize the list
for (int i = 1; i <= n; i++) {
if (n % i == 0) { // decide the factor.
list.add(i); // now i is a factor, needs to add to list
}
}
return list; // return list contains factors.
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an array named numbers that I want to populate with a for loop:
int[] numbers;
for ( int i = 0; i <=10; i++)
{
// want to populate the array with a sequence of 0-10
}
How can I populate the 11 values generated from the above for loop into my array?
If you are using Java 7 or lower, do this:
int[] numbers = new int[11];
for ( int i = 0; i <=10; i++)
{
numbers[i] = i;
}
For Java 8, there is a more concise way to do this:
int[] numbers = IntStream.rangeClosed(0, 10).toArray()
First you need to define what numbers is, you have only declared it.
int[] numbers = new int[11];
Then insert the values you want.
for ( int i = 0; i <=10; i++)
{
numbers[i] = i;
}