170030964 WEEK 7.docx - WEEK 7 170030964 HACKERRANK...

This preview shows page 1 - 13 out of 23 pages.

WEEK 7 170030964 HACKERRANK PROGRAMS: Java Interface class MyCalculator implements AdvancedArithmetic{ public int divisor_sum(int n){ int sum=0; for(int i=1;i<=n;i++) { if(n%i==0) sum+=i; } return sum; } }
Image of page 1

Subscribe to view the full document.

Java Method Overriding void getNumberOfTeamMembers() { System.out.println("Each team has 11 players in " + getName()); }
Image of page 2
Java Method Overriding 2 (Super Keyword) String temp=super.define_me();
Image of page 3

Subscribe to view the full document.

Minimum Loss import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long [] vals = new long [n]; HashMap<Long,Integer> map = new HashMap<Long,Integer>(); for(int i = 0 ; i< n ; i++){ vals[i] = sc.nextLong(); } for (int o = 0 ; o < n ; o++){ map.put(vals[o],o); }
Image of page 4
Arrays.sort(vals); long min = vals[1]-vals[0]; for (int k = 0 ; k < n-1 ; k++){ if (min > vals[k+1]-vals[k] && map.get(vals[k])-map.get(vals[k+1]) > 0){ min = vals[k+1]-vals[k]; } } System.out.println(min); } } Short Palindrome #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h>
Image of page 5

Subscribe to view the full document.

#define MAX 1000000007 int main() { int i, j; long long result = 0; int total[26] = {0}; int count[26] = {0}; long long count2[26][26] = {0}; char s[1000001]; scanf("%s", s); for (i=0; s[i]!='\0'; i++) { ++total[s[i]-'a']; } for (i=0; s[i]!='\0'; i++) { ++count[s[i]-'a']; for (j=0; j<26; j++) { result = (result+count2[s[i]-'a'][j]*(total[j]-count[j]))%MAX; count2[s[i]-'a'][j] += count[j]; } --count2[s[i]-'a'][s[i]-'a']; } printf("%lld", result); return 0; }
Image of page 6
Ice Cream Parlor #include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> int main() { int t,m,n,i,j,a[10000]; scanf("%d",&t); while (t--) { scanf("%d",&m); scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",&a[i]);
Image of page 7

Subscribe to view the full document.

for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(a[i]+a[j]==m) printf("%d %d\n",i+1,j+1); } } } return 0; } Count Luck #include <stdio.h> #include <stdlib.h> #define V(i,j) ((((i) >= 0 && (i) < r) && ((j) >= 0 && (j) < c) && (A[(i)][(j)] != 'X')) ? 1 : 0 )
Image of page 8
char** A; int r,c,K; struct LOC { int x; int y; }start,end; void visit(int x,int y,int turns) { int ways = 0; A[x][y] = 'X'; if((x == end.x) && (y == end.y)){ if(turns == K) printf("Impressed\n"); else printf("Oops!\n"); } if(V(x-1,y)) ways++; if(V(x,y+1)) ways++; if(V(x,y-1)) ways++; if(V(x+1,y)) ways++; if(ways > 1) ++turns;
Image of page 9

Subscribe to view the full document.

if(V(x-1,y)) visit(x-1,y,turns); if(V(x,y+1)) visit(x,y+1,turns); if(V(x,y-1)) visit(x,y-1,turns); if(V(x+1,y)) visit(x+1,y,turns); } void release() { for(int i = 0; i < r; ++i) free(A[i]); free(A); } int main() { int t,i,j,ans; scanf("%d",&t); while(t--) { scanf("%d %d",&r,&c); A = (char**)malloc(r * sizeof(char*)); for(i = 0; i < r; ++i) A[i] = (char*)malloc((c+1) * sizeof(char)); for(i = 0; i < r; ++i) scanf("%s",A[i]); scanf("%d",&K);
Image of page 10
for(i = 0; i < r; ++i) for(j = 0; j < c; ++j) if(A[i][j] == '*') end.x = i, end.y = j; else if(A[i][j] == 'M') start.x = i, start.y = j; visit(start.x,start.y,0); release(); } return 0; } Simplified Chess Engine import java.io.BufferedReader; import java.io.IOException;
Image of page 11

Subscribe to view the full document.

import java.io.InputStreamReader;
Image of page 12
Image of page 13
  • Spring '17
  • prsad
  • Void type, Referee, Diagonal system of control, Stanley Rous, Tree traversal

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern