Posts
Showing posts from July, 2017
Counting setbits of number from 1 to N (Asked in Amazon)
- Get link
- X
- Other Apps
#include<stdio.h> #include<conio.h> int countSetBitsUtil(int); int main(){ int i; int count=0; int n; scanf("%d",&n); for(i=1;i<=n;i++){ count+=countSetBitsUtil(i); } printf("%d",count); } int countSetBitsUtil(int i){ int counter=0; while(i>=1){ if(i%2==1){ counter++; } i=i/2; } return counter; } This method is for only beginners. Stay tuned for the Optimum solution...
Java Substring Comparisons HackerRank Solution
- Get link
- X
- Other Apps
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static String getSmallestAndLargest(String s, int k) { String smallest = ""; String largest = ""; int j=0; ArrayList<String> al=new ArrayList<String>(); for(;j<s.length()-k;j++){ al.add(s.substring(j,j+k)); } al.add(s.substring(j)); smallest=al.get(0); largest=al.get(0); /*for(String i:al){ System.out.println(i); }*/ ...
- Get link
- X
- Other Apps
Grading Students HackerRank solution import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { static int[] solve(int[] grades){ int arr[]=new int[grades.length]; for(int i=0;i<grades.length;i++){ if((grades[i]%5==0)){ arr[i]=grades[i]; } else{ for(int j=grades[i]+1;j<=grades[i]+2;j++){ if(j%5==0){ if(j...