Question

In: Computer Science

Problem: Given an integer array consisting of only 0’s and 1’s and a value k that...

Problem: Given an integer array consisting of only 0’s and 1’s and a value k that denotes distance, determine if all the 1’s are at least k spaces away from each other.

Details:

  • The distance between each 1 in the array can be greater than or equal to k places. It doesn’t have to be exactly k
  • Assume the array will only contain 0’s and 1’s – no need to do validation checking
  • If k is greater than the size of the array you can return false since this is impossible to do
  • If k is 0 then all the elements in the array must be 1’s
  • Assume k will be a non-negative number
  • Write using simple Java algorithms

Sample Input:

Input: {1,1,0,1,0,1}, k=2

Output: False

Input: {1,1,1,1,1}, k=0

Output: True

Input: {1,0,0,1,0,1}, k=1

Output: True

Solutions

Expert Solution


public class kSpace {
  
   public static boolean validateKspace(int ar[], int k) {
       if(k>ar.length)
           return false;
       int n=-1;
       for(int i=0;i<ar.length;i++) {
           if(ar[i]==1) {
               if(n==-1) {
                   n=0;
               }else {
                   if(n<k)
                       return false;
                   else {
                       n=0;
                   }
               }
           }else {
               n+=1;
           }
       }
       return true;
   }

   public static void main(String[] args) {
       int ar1[]={1,1,0,1,0,1};
       System.out.println(validateKspace(ar1, 2));
       int ar2[]={1,1,1,1,1};
       System.out.println(validateKspace(ar2, 0));
       int ar3[]= {1,0,0,1,0,1};
       System.out.println(validateKspace(ar3, 1));

   }

}


Related Solutions

Given an array of integers, and given a specific value k (not equal to 0), produce...
Given an array of integers, and given a specific value k (not equal to 0), produce all unique pairs of values in the array which differ by k. For example, if the array has [1,4,9,12, 6, 15, 5, 13,17] and k=3, the answer would be (1,4 ) ( 9,12), ( 9,6), (12,15). If k=4, the answer would be (1,5), (9,5), (13,17), (9.13). Notice that you do not print the same answer twice, for instance (9,13), and (13,9).
Given a positive integer k and an array A[1..n] that contains the quiz scores of n...
Given a positive integer k and an array A[1..n] that contains the quiz scores of n students in ascending order, design a divide and conquer algorithm to efficiently count the number of students that have quiz scores in (100(i − 1)/k, 100i/k] for integers 1 ≤ i ≤ k. Let group i be the set of students with quiz scores in (100(i − 1)/k, 100i/k] for integers 1 ≤ i ≤ k. The counting result should be stored in G[1..k],...
Project: Given a string s and an integer array indices of the same length. The string...
Project: Given a string s and an integer array indices of the same length. The string s will be shuffled such that the character at the i th position moves to indices[i] in the shuffled string. Return the shuffled string. Example: Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3] Output: "leetcode" Explanation: As shown, "codeleet" becomes "leetcode" after shuffling. You need to do: Create a class called ShuffledStringApp. Keeping class StringOperation and IO in util package. In this project, you will...
Given that the square matrix, A is nilpotent (Ak = 0 for some positive integer k)....
Given that the square matrix, A is nilpotent (Ak = 0 for some positive integer k). If A is n by n, show that An = 0.
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array...
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time. Return that integer. Input: arr = [1,2,2,6,6,6,6,7,10] Output:
Problem 5 Given a table of n integers and an integer k, make a program in...
Problem 5 Given a table of n integers and an integer k, make a program in C++ that: a) Read n b) Read the table of numbers. c) Determine the number of elements in the table less than k d) Determine the number of elements in the table equal to k e) Determine the number of elements in the table greater than k f) That displays the values found
Given an array A[1..n], with distinct values and k with 1 ≤ k ≤ n. We...
Given an array A[1..n], with distinct values and k with 1 ≤ k ≤ n. We want to return the k smallest element of A[1.....n], in non-decreasing order. For example: A = [5, 4, 6, 2, 10] and k = 4, the algorithm returns [2, 4, 5, 6]. There are at least the following four approaches: a. heapify A and then extract k elements one by one b. sort the array (e.g. using MergeSort or HeapSort) and then read the...
Write a function maxFun() that returns the maximum value in any given integer array. Determine the...
Write a function maxFun() that returns the maximum value in any given integer array. Determine the function parameters (complete the area shown in ___________. ___________maxFun(____________________________) { }
rogram that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary.
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x // 2Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string.Ex: If the input is:6the output is:110Your program must define and call the following two functions. The function integer_to_reverse_binary() should return a string of 1's...
Problem: Given an integer k, find the two closest integers in absolute difference and the whole...
Problem: Given an integer k, find the two closest integers in absolute difference and the whole product equals k+1 or k+2. Return the two integers in any order. Write the code in java. You can assume the integer value will be between [1,10^9] You can multiple a divisor with itself. Description of a sample run 1: The input number 8 hence, k+1 is 9 and k+2 is 10 The divisors for 9 are: 1,3,9 and for 10: 1,2,5,10 For both...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT