Amazon Interview Question for SDE1s

• 0

Country: India

Comment hidden because of low score. Click to expand.
1
of 1 vote

``````public int solve(int[] array){
if(array.length<=1){
return 0;
}

int mod = (int)(1e9+7);
int sum = 0;
int pre_sum = array[0];
for(int i=1;i<array.length;i++){
sum += (pre_sum%array[i]);
pre_sum+=array[i];
sum %= mod;
pre_sum %= mod;
}

int suffix_sum = array[array.length-1];
for(int i=array.length-2;i>=0;i--){
sum += (suffix_sum%array[i]);
suffix_sum += array[i];
sum %= mod;
suffix_sum %= mod;
}

return sum;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

can you explain me the idea behind your solution

Comment hidden because of low score. Click to expand.
0
of 0 vote

The solution is for sorted array.

Comment hidden because of low score. Click to expand.
0
of 0 vote

Have anyone solved it ?

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````public class SumOfIntegers {
public static void main(String[] args) {
int[] arr = new int[] {1,2,3};
long sum = 0;
for(int i=0;i<arr.length;i++){
int k= (i+1)%3;
sum+=arr[i]%arr[k];
sum+=arr[k]%arr[i];
}

sum+=sum%(Math.pow(10, 9)+7);

System.out.println(sum);
}``````

}

Comment hidden because of low score. Click to expand.
0

Can you explain the logic in detail?

Comment hidden because of low score. Click to expand.
0
of 0 vote

public class SumOfIntegers {
public static void main(String[] args) {
int[] arr = new int[] {1,2,3};
long sum = 0;
for(int i=0;i<arr.length;i++){
int k= (i+1)%3;
sum+=arr[i]%arr[k];
sum+=arr[k]%arr[i];
}

sum+=sum%(Math.pow(10, 9)+7);

System.out.println(sum);
}
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````import java.util.*;
public class Solution
{
public static void main(String[] args)
{
int[] A = new int[] {1, 2, 3};
int n = A[A.length-1];
long sum = 0;
for(int i = 1; i <= n; i++)
{
int q = n / i;
int r = n % i;
sum += (long)((i*(i-1)/2)*q + r*(r+1)/2);
}
System.out.println(sum % (long)(Math.pow(10, 9)+7));
}
}``````

Comment hidden because of low score. Click to expand.
-2
of 2 vote

import java.util.*;
public class Solution
{
public static void main(String[] args)
{
int[] A = new int[] {1, 2, 3};
int n = A[A.length-1];
long sum = 0;
for(int i = 1; i <= n; i++)
{
int q = n / i;
int r = n % i;
sum += (long)((i*(i-1)/2)*q + r*(r+1)/2);
}
System.out.println(sum % (long)(Math.pow(10, 9)+7));
}
}

Comment hidden because of low score. Click to expand.
0

Please dont post solutions without understanding question.

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.