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).
In Java, write a program that given an array A[1...n] and a value S finds 0...
In Java, write a program that given an array A[1...n] and a value S finds 0 < i < j < n such that A[i] + A[j] = S
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],...
Problem 1: Given an array A[0 ... n-1], where each element of the array represents a...
Problem 1: Given an array A[0 ... n-1], where each element of the array represents a vote in the election. Assume that each vote is given as integers representing the ID of the chosen candidate. Write the code determining who wins the election. Problem 2: How do we find the number which appeared maximum number of times in an array? ( Use Java and an original code )
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.
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 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...
Need these written in Java script please Problem 1: Given an array A[0 ... n-1], where...
Need these written in Java script please Problem 1: Given an array A[0 ... n-1], where each element of the array represents a vote in the election. Assume that each vote is given as integers representing the ID of the chosen candidate. Write the code determining who wins the election. Problem 2: How do we find the number which appeared maximum number of times in an array?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT