We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. All Contests
  2. HourRank 25
  3. The Strange Function
  4. Discussions

The Strange Function

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 7 Discussions, By:

votes

Please Login in order to post a comment

  • skpro19
    4 years ago+ 1 comment

    Can someone please explain the solution to problem 3 in a bit more detail. I am not able to understand it from the editorial.

    4|
    Permalink
  • Stomach_ache
    4 years ago+ 0 comments

    There's a mistake in the editorial, where should be .

    1|
    Permalink
  • karansha
    4 years ago+ 0 comments

    Test cases 9 onwards are giving timeout errors. Anybody?

    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    
     class FuncData {
        String index;
        long gcd, sum, max, func;
        
        public FuncData(String index, long gcd, long sum, long max, long func){
            this.index = index;
            this.gcd = gcd;
            this.sum = sum;
            this.max = max;
            this.func = func;
        }
        
        public long getFunc(){
            return this.func;
        }
    
    }
    
    public class Solution {
        
        public static long GCD(int[] a){
            long result = Math.abs(a[0]);
            
            for(int i=1;i<a.length;i++){
                if(result>Math.abs(a[i])){
                    result = GCD(result,Math.abs(a[i]));
                }        
                else{
                    result = GCD(Math.abs(a[i]), result);
                }
            }
            
            return result;
        }
        
        //this method assumes a>b. If that is not the case, then we'd have to swap the numbers.
        public static long GCD(long a, long b){
            if(a == 0){
                return b;
            }
            else if(b==0){
                return a;
            }
            
            if(a==b){
                return a;
            }
            
            if(a<b){
                return GCD(a, b%a);
            }
            else{
                return GCD(a%b, b);
            }
            
        }       
        
        public static long sum(int[] a){
            int sum = 0;
            for(int i=0; i<a.length; i++){
                sum = sum + a[i];
            }
            
            return sum;
        }
        
        public static long max(int[] a){
            int max = a[0];
            
            for(int i=1;i<a.length;i++){
                if(a[i]>max){
                    max = a[i];
                }
            }
            return max;
        }
        
        public static long min(int[] a){
            int min = a[0];
            
            for(int i=1;i<a.length;i++){
                if(a[i]<min){
                    min = a[i];
                }
            }
            return min; 
        }
    
        public static long maxFuncValue(List<FuncData> funcData){
            long maxValue = Integer.MIN_VALUE;
            for(FuncData fd: funcData){
                if(fd.getFunc() > maxValue){
                    maxValue = fd.getFunc();
                }
            }
            return maxValue; 
        }
    
        public static long maximumValue(int[] a) {
    
            List<FuncData> funcData = BuildData(a);
            
            return maxFuncValue(funcData);
        }
        
        public static int[] getArrayFromIndexes(int i, int j, int[] a){
            int[] array = new int[j-i+1];
            for(int k=i, l=0; k<=j; k++, l++){
                array[l] = a[k-1];
            }
            return array;
        }
        
        public static List<FuncData> BuildData(int[] a){
            
            List<FuncData> list = new ArrayList<FuncData>();
            
            for(int i=1;i<=a.length;i++){
                for(int j=i;j<=a.length;j++){
                    int[] temp = getArrayFromIndexes(i,j,a);
                    list.add(new FuncData (i + "," + j,
                                          GCD(temp),
                                          sum(temp),
                                          max(temp),
                                          GCD(temp) * (sum(temp) - max(temp))));
                }
            }
            
            return list;
        }
    
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int n = in.nextInt();
            int[] a = new int[n];
            for(int a_i = 0; a_i < n; a_i++){
                a[a_i] = in.nextInt();
            }
            long result = maximumValue(a);
            System.out.println(result);
            in.close();
        }
    }
    
    0|
    Permalink
  • 2997ms
    4 years ago+ 0 comments

    I was wondering why my rank disappeared. I didn't cheat.

    0|
    Permalink
  • anirudhkannan_v1
    4 years ago+ 1 comment

    PARTIAL BRUTE FORCE SOLUTION FOR 10.5 POINTS 30 PERCENT FIRST SUBTASK

    FIND ALL POSSIBLE SUBARRAYS AND AND APPLY THE FUNCTION DEFINITION AS PER QUESTION.

    from math import gcd
    def func1(a1,b1):
        return gcd(a1,b1)
    def func(a,n):
        gcd1=a[0]
        for i in range(1,n):
            gcd1=func1(gcd1,a[i])
        sum1=0
        maxi=max(a)
        for i in range(n):
            sum1+=a[i]
        ans=gcd1*(sum1-maxi)
        return ans
    n=int(input())
    a=list(map(int,input().split()))
    ans=-1
    for i in range(n):
        for j in range(i,n):
            ans=max(ans,func(a[i:j+1],j-i+1))
    print(ans)
    

    github:- https://github.com/anirudhkannanvp/HACKERRANK

    FOR ANY QUERIES MSG ME ON HACKERRANK OR GITHUB

    -1|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature