public int sum67(int[] nums) {
int sum=0;
for(int n:nums){
if(n!=6&&n!=12&&n!=7)
sum=sum+n;
}
return sum;
}
Return the sum of the numbers in the array, except ignore sections of numbers starting with a 6 and extending to the next 7 (every 6 will be followed by at least one 7). Return 0 for no numbers.
sum67([1, 2, 2]) → 5
sum67([1, 2, 2, 6, 99, 99, 7]) → 5
sum67([1, 1, 6, 7, 2]) → 4
sum67([1, 2, 2, 6, 99, 99, 7]) → 5
public int sum67(int[] nums) {
int sum=0;
int len=nums.length;
for(int i=0;i<len;i++){
if(nums[i]!=6&&nums[i]!=7){
sum=sum+nums[i];
}
else if(nums[i]==6&&nums[i+1]==7){
sum=sum+nums[i];
}
}
return sum;
}
Expected Run
sum67([1, 2, 2]) → 5 5 OK
sum67([1, 2, 2, 6, 99, 99, 7]) → 5 203 X
sum67([1, 1, 6, 7, 2]) → 4 10 X
sum67([1, 6, 2, 2, 7, 1, 6, 99, 99, 7]) → 2 204 X
sum67([1, 6, 2, 6, 2, 7, 1, 6, 99, 99, 7]) → 2 204 X
sum67([2, 7, 6, 2, 6, 7, 2, 7]) → 18 12 X
sum67([2, 7, 6, 2, 6, 2, 7]) → 9 6 X
sum67([1, 6, 7, 7]) → 8 7 X
sum67([6, 7, 1, 6, 7, 7]) → 8 13 X
sum67([6, 8, 1, 6, 7]) → 0 15 X
sum67([]) → 0 0 OK
sum67([6, 7, 11]) → 11 17 X
sum67([11, 6, 7, 11]) → 22 28 X
sum67([2, 2, 6, 7, 7]) → 11 10 X
other tests
X
Do you know the answer to this case?1+1+7+1=10
1 1 6 2 3 4 5 8 9 7 1
public class Sum67 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] ar = { 1, 2, 2, 6, 99, 99, 7, 3, 9 };
System.out.println("value is" + sum67(ar));
}
public static int sum67(int[] nums) {
int sum = 0;
int len = nums.length;
int sixPos=0;
int sevenPos=0;
int sum1=0;
int sum2=0;
for (int i = 0; i < len; i++) {
if (nums[i] == 6){
sixPos=i;
}
if (nums[i] ==7){
sevenPos=i;
}
}
for (int i = 0; i < sixPos; i++) {
sum1=sum1+nums[i];
}
for (int i = sevenPos; i < len; i++) {
sum2=sum2+nums[i];
}
return sum1+sum2;
}
}
something like above?
public int sum67(int[] nums) {
int sum = 0;
int len = nums.length;
int sixPos=0;
int sevenPos=0;
int sum1=0;
int sum2=0;
for (int i = 0; i < len; i++) {
if (nums[i] == 6){
sixPos=i;
}
if (nums[i] ==7){
sevenPos=i;
}
}
for (int i = 0; i < sixPos; i++) {
sum1=sum1+nums[i];
}
for (int i = sevenPos; i < len; i++) {
sum2=sum2+nums[i];
}
return sum1+sum2;
}
failing few testsExpected Run
sum67([1, 2, 2]) → 5 5 OK
sum67([1, 2, 2, 6, 99, 99, 7]) → 5 12 X
sum67([1, 1, 6, 7, 2]) → 4 11 X
sum67([1, 6, 2, 2, 7, 1, 6, 99, 99, 7]) → 2 26 X
sum67([1, 6, 2, 6, 2, 7, 1, 6, 99, 99, 7]) → 2 32 X
sum67([2, 7, 6, 2, 6, 7, 2, 7]) → 18 24 X
sum67([2, 7, 6, 2, 6, 2, 7]) → 9 24 X
sum67([1, 6, 7, 7]) → 8 8 OK
sum67([6, 7, 1, 6, 7, 7]) → 8 21 X
sum67([6, 8, 1, 6, 7]) → 0 22 X
sum67([]) → 0 0 OK
sum67([6, 7, 11]) → 11 18 X
sum67([11, 6, 7, 11]) → 22 29 X
sum67([2, 2, 6, 7, 7]) → 11 11 OK
other tests
X
public class Sum67 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//int[] ar = { 1, 2, 2, 6, 99, 99, 7, 3, 9 };
int[] ar = {1, 1, 6, 2, 3, 4, 5, 8, 9, 7, 1};//sixPos 2 sevenPos 9
System.out.println("value is" + sum67(ar));
}
public static int sum67(int[] nums) {
int sum = 0;
int len = nums.length;
int sixPos=0;
int sevenPos=0;
int sum1=0;
int sum2=0;
for (int i = 0; i < len; i++) {
if (nums[i] == 6){
sixPos=i;
System.out.println("sixPos is-->"+sixPos);
}
if (nums[i] ==7){
sevenPos=i;
System.out.println("sevenPos is-->"+sevenPos);
}
}
for (int i = 0; i < sixPos; i++) {
sum1=sum1+nums[i];
}
for (int i = sevenPos+1; i < len; i++) {
sum2=sum2+nums[i];
}
System.out.println("sum1 is-->"+sum1);
System.out.println("sum2 is-->"+sum2);
return sum1+sum2;
}
}
xpected Run
sum67([1, 2, 2]) → 5 4 X
sum67([1, 2, 2, 6, 99, 99, 7]) → 5 5 OK
sum67([1, 1, 6, 7, 2]) → 4 4 OK
sum67([1, 6, 2, 2, 7, 1, 6, 99, 99, 7]) → 2 19 X
sum67([1, 6, 2, 6, 2, 7, 1, 6, 99, 99, 7]) → 2 25 X
sum67([2, 7, 6, 2, 6, 7, 2, 7]) → 18 17 X
sum67([2, 7, 6, 2, 6, 2, 7]) → 9 17 X
sum67([1, 6, 7, 7]) → 8 1 X
sum67([6, 7, 1, 6, 7, 7]) → 8 14 X
sum67([6, 8, 1, 6, 7]) → 0 15 X
sum67([]) → 0 0 OK
sum67([6, 7, 11]) → 11 11 OK
sum67([11, 6, 7, 11]) → 22 22 OK
sum67([2, 2, 6, 7, 7]) → 11 4 X
other tests
X
You can probably get away with a single pass over the array if you have a boolean that indicates whether you're between a 6 and a 7. The pseudocode of the loop could look like this:let me check on above
If the current element is a 6, set the boolean to true.
If the boolean is false, add the current element to the sum.
If the current element is a 7, set the boolean to false.
public class Sum67 {
public static void main(String[] args) {
// TODO Auto-generated method stub
// int[] ar = { 1, 2, 2, 6, 99, 99, 7, 3, 9 };
// int[] ar = {1, 1, 6, 2, 3, 4, 5, 8, 9, 7, 1};//sixPos 2 sevenPos 9
int[] ar = { 1, 2, 2 };// sixPos 0 sevenPos 0
System.out.println("value is" + sum67(ar));
}
public static int sum67(int[] nums) {
int sum = 0;
int len = nums.length;
int sixPos = 0;
int sevenPos = 0;
int sum1 = 0;
int sum2 = 0;
int sumExclude = 0;
for (int i = 0; i < len; i++) {
if (nums[i] == 6) {
sixPos = i;
System.out.println("sixPos is-->" + sixPos);
}
if (nums[i] == 7) {
sevenPos = i;
System.out.println("sevenPos is-->" + sevenPos);
}
}
for (int i = 0; i < sixPos; i++) {
sum1 = sum1 + nums[i];
}
for (int i = sevenPos + 1; i < len; i++) {
sum2 = sum2 + nums[i];
}
System.out.println("sum1 is-->" + sum1);
System.out.println("sum2 is-->" + sum2);
if (sixPos == 0 && sevenPos == 0) {
for (int i = 0; i < len; i++) {
sumExclude = sumExclude + nums[i];
}
return sumExclude;
} else if (sixPos > 0 && sevenPos > 0) {
return sum1 + sum2;
} else {
return 0;
}
}
}
But in a free online U.C.S.D. course
public class Sum67 {
public static void main(String[] args) {
// TODO Auto-generated method stub
// int[] ar = { 1, 2, 2, 6, 99, 99, 7, 3, 9 };
// int[] ar = {1, 1, 6, 2, 3, 4, 5, 8, 9, 7, 1};//sixPos 2 sevenPos 9
int[] ar = { 1, 2, 2 };// sixPos 0 sevenPos 0
System.out.println("value is" + sum67(ar));
}
public static int sum67(int[] nums) {
/*
* If the current element is a 6, set the boolean to true.
If the boolean is false, add the current element to the sum.
If the current element is a 7, set the boolean to false.*/
int sum=0;
boolean ind=false;
for(int i=0;i<nums.length;i++){
if(nums[i]==6){
ind=true;
}
if(ind==false){
sum+=nums[i];
}
if(nums[i]==7){
ind=true;
}
}
return sum;
}
}
Expected Run
sum67([1, 2, 2]) → 5 5 OK
sum67([1, 2, 2, 6, 99, 99, 7]) → 5 5 OK
sum67([1, 1, 6, 7, 2]) → 4 2 X
sum67([1, 6, 2, 2, 7, 1, 6, 99, 99, 7]) → 2 1 X
sum67([1, 6, 2, 6, 2, 7, 1, 6, 99, 99, 7]) → 2 1 X
sum67([2, 7, 6, 2, 6, 7, 2, 7]) → 18 9 X
sum67([2, 7, 6, 2, 6, 2, 7]) → 9 9 OK
sum67([1, 6, 7, 7]) → 8 1 X
sum67([6, 7, 1, 6, 7, 7]) → 8 0 X
sum67([6, 8, 1, 6, 7]) → 0 0 OK
sum67([]) → 0 0 OK
sum67([6, 7, 11]) → 11 0 X
sum67([11, 6, 7, 11]) → 22 11 X
sum67([2, 2, 6, 7, 7]) → 11 4 X
other tests
X
If the current element is a 6, set the boolean to true.
If the boolean is false, add the current element to the sum.
If the current element is a 7, set the boolean to false
public int sum67(int[] nums) {int sum=0;
boolean ind=false;
for(int i=0;i<nums.length;i++){
if(nums[i]==6){
ind=true;
}
if(ind==false){
sum+=nums[i];
}
if(nums[i]==7){
ind=false;
}
}
return sum;
}
public int sum67(int[] nums) {int sum=0;
boolean ind=false;
for(int i=0;i<nums.length;i++){
if(nums[i]==6){
ind=true;
}
if(ind==false){
sum+=nums[i];
}
if(nums[i]==7&&ind==true){
ind=false;
}
}
return sum;
}
Title | # Comments | Views | Activity |
---|---|---|---|
mapAB Challlenge | 35 | 84 | |
Unable to start eclipse ? | 17 | 82 | |
unix in java example | 9 | 37 | |
micro services spring boot application error | 3 | 23 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
15 Experts available now in Live!