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 10 months ago.
Improve this question
49 5 8 14 3 7 6 21
It gives the rank of the smallest number whose remainder is 0 after dividing by 7 in the above given index.
create the algorithm.
I WAS TRY BUT I FAİLED.. I'M YET NEW TO JAVA ..
Here's some hints:
To test if an integer named x is divisible by 7:
if (x % 7 == 0) {
// x is divisible by 7
}
To enumerate all the items in an array name arr
for (int i = 0; i < arr.length; i++) {
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I need to populate an array with random numbers from -50 to 50 and need to use Math.random(). Here is my current code:
double[] randomNums = new double[5];
for (int i = 0; i < randomNums.length; i++) {
randomNums[i] = 100 * Math.random() - 50;
}
for (double i: randomNums) {
System.out.println(i + ',');
}
Output:
-5.836717454677796,
44.07635593282988,
23.650145270722884,
93.00810678750743,
54.0536237451922
Why is this going above 50?
You are adding the value of the char ,, which has value 44 to each double. You can test this using:
System.out.println(0 + ',');
Output: 44
To fix this you can simply remove the + ','
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have an array of even number of elements, I have to select n/2( n=array size), pairs and calculate their GCD such that the sum of their GCD is max, given once we use those elements from array, we cannot use them again.
Example1: `
Input : 8 24 12 16
Output : 20
Explanation: We select two pairs (8,16) and (12,24) as sum of their GCD is maximum.
If we choose some other pairs, say (8,12) and (24,16), sum of their GCD will be 4+4 =8.
Example 2:
Input : 12 10 36 25 36 16
Output: 45
Explanation: We select the following 3 pairs : (36,36), (10,25) and (12,16) as the sum of their GCD is
36+5+4 = 45.
Our Approach:
for i in range(0,n):
max = 0;
for j in range(i+1,n):
temp = gcd(a[i], a[j]) // standard func to find GCD
if(max<temp):
store i and j
store max gcd every time and finally make a[i] and a[j] =0 to mark the visited elements
Edited
Constraint: max number of elements = 20, a[i]< 10^9.
Can you suggest an algorithm to optimally satisfy the above testcases in the least time complexity?
Because my approach is failing on multiple testcases.
This is a comment but I am not allowed to post comment yet.
It is not good to solve this problem by looking for the largest gcd.
Take [8,9,24,36], the largest gcd is gcd(24,36) = 12, that will get you gcd(24,36) + gcd(8,9) = 12+1 =13.
However, the largest sum is given by gcd(8,24) + gcd(9,36) = 8+9 = 17.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
List<Integer> list = Arrays.asList(1, 2, 4, 5,7, 8, 9);
int n = 3;
When user input integer as 3 then program should result as 2 and 4 in an above given list.
You can use Collectors.partitioningBy:
Map<Boolean, List<Integer>> m = list.stream()
.filter(i -> i != n)
.collect(Collectors.partitioningBy(i -> i > n));
Integer lower = m.get(false).get(m.get(false).size() - 1);
Integer higher = m.get(true).get(0);
Additional check whether both m.get(false) and m.get(true) are not empty might be needed.
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 4 years ago.
Improve this question
I have been working with while loops however I cant seem to understand how the remainder works inside this code.
int a = 10;
while( a <= 1000 && a % 100 != 0){
System.out.println("a = " + a);
a = a + 10;
}
a & 100 != 0
Performs bitwise and , then compares the result to 0. It will be false even in the first iteration, since 10 & 100 = 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 4 years ago.
Improve this question
I'm java beginner, This is my code.
int [] arr = {1,3,4,5,5,6};
I want to get each array values separately for calculation. can you please help me.
For example: I want first two digits (array index 0 and 1) only how to get that.
to get the first and second item, do like this:
int [] arr = {1,3,4,5,5,6};
System.out.println("pos 0: "+arr[0]+" pos 1: "+arr[1]);
by doing arr[n-1], you select the 'n'th value in the array.
now, if you want to multiply these first two values..:
int result = arr[0] * arr[1]; // 1 * 3
System.out.println(result) // 3
For example: I want to multiply first two digit (1*3) after that (4*5) like that.. so I want to get these values separately.
To do this,
int [] arr = {1,3,4,5,5,6};
int ans;
for(int i=0;i<6;i+=2){
ans = arr[i]*arr[i+1];
System.out.println(""+ans);
}