Find the row up to which there are at least K stars in the Diamond Pattern
Last Updated :
11 Feb, 2022
Given two integers N and K, where N represents a diamond pattern with (2 * N) -1 rows, the task is to find the index of the first row up to which there are at least K stars in a diamond pattern.
Please note that the value of K will always have a definite answer.
Examples:
Input: N = 3 , K = 8
Output: 4
Explanation: The first 4 rows contain a total of 8 stars.
*
* *
* * *
* *
*
Input: N = 5, K = 5
Output: 3
Naive Approach: The given problem can be solved by simply iterating over each row and maintaining the count of the number of stars in each row. Print the first two where the count of stars exceeds K.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void get( int N, int K)
{
int sum = 0, ans;
for ( int i = 1; i <= 2 * N - 1; i++) {
if (i <= N) {
sum += i;
}
else {
sum += 2 * N - i;
}
if (sum >= K) {
ans = i;
break ;
}
}
cout << ans << endl;
}
int main()
{
int N = 3, K = 8;
get(N, K);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void get( int N, int K)
{
int sum = 0 , ans = 0 ;
for ( int i = 1 ; i <= 2 * N - 1 ; i++) {
if (i <= N) {
sum += i;
}
else {
sum += 2 * N - i;
}
if (sum >= K) {
ans = i;
break ;
}
}
System.out.print(ans + "\n" );
}
public static void main(String[] args)
{
int N = 3 , K = 8 ;
get(N, K);
}
}
|
Python3
def get(N, K):
sum = 0 ;
ans = 0 ;
for i in range ( 1 , 2 * N):
if (i < = N):
sum + = i;
else :
sum + = 2 * N - i;
if ( sum > = K):
ans = i;
break ;
print (ans);
if __name__ = = '__main__' :
N = 3 ;
K = 8 ;
get(N, K);
|
C#
using System;
public class GFG
{
static void get ( int N, int K)
{
int sum = 0, ans = 0;
for ( int i = 1; i <= 2 * N - 1; i++) {
if (i <= N) {
sum += i;
}
else {
sum += 2 * N - i;
}
if (sum >= K) {
ans = i;
break ;
}
}
Console.Write(ans + "\n" );
}
public static void Main(String[] args)
{
int N = 3, K = 8;
get (N, K);
}
}
|
Javascript
<script>
function get(N, K)
{
let sum = 0, ans;
for (let i = 1; i <= 2 * N - 1; i++) {
if (i <= N) {
sum += i;
}
else {
sum += 2 * N - i;
}
if (sum >= K) {
ans = i;
break ;
}
}
document.write(ans);
}
let N = 3, K = 8;
get(N, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized using binary search on the value of the number of rows from [0, 2*n-1]. Follow the steps below to solve the problem:
- Initialize variables start = 0, end = (2 * N) – 1 and, ans = 0.
- Follow these steps while the value of the start is less than the end.
- Calculate mid which is equal to (start + end) / 2
- Count the number of stars till mid.
- If the number of stars till mid are greater than or equal to K, store the mid into the variable and move towards the left of mid by end = mid-1
- Else, move right of mid by start = mid+1
- Return ans which is the required value.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int count_rows( int n, int k)
{
int start = 1, end = 2 * n - 1, ans = 0;
while (start <= end) {
int mid = start - (start - end) / 2;
int stars_till_mid = 0;
if (mid > n) {
int l_stars = 2 * n - 1 - mid;
int till_half = (n * (n + 1)) / 2;
stars_till_mid
= till_half + ((n - 1) * (n)) / 2
- ((l_stars) * (l_stars + 1)) / 2;
}
else {
stars_till_mid = mid * (mid + 1) / 2;
}
if (k <= stars_till_mid) {
ans = mid;
end = mid - 1;
}
else
start = mid + 1;
}
return ans;
}
int main()
{
int N = 3, K = 8;
cout << count_rows(N, K);
}
|
Java
import java.util.*;
class GFG{
static int count_rows( int n, int k)
{
int start = 1 , end = 2 * n - 1 , ans = 0 ;
while (start <= end) {
int mid = start - (start - end) / 2 ;
int stars_till_mid = 0 ;
if (mid > n) {
int l_stars = 2 * n - 1 - mid;
int till_half = (n * (n + 1 )) / 2 ;
stars_till_mid
= till_half + ((n - 1 ) * (n)) / 2
- ((l_stars) * (l_stars + 1 )) / 2 ;
}
else {
stars_till_mid = mid * (mid + 1 ) / 2 ;
}
if (k <= stars_till_mid) {
ans = mid;
end = mid - 1 ;
}
else
start = mid + 1 ;
}
return ans;
}
public static void main(String[] args)
{
int N = 3 , K = 8 ;
System.out.print(count_rows(N, K));
}
}
|
Python3
def count_rows(n, k):
start = 1
end = 2 * n - 1
ans = 0
while (start < = end):
mid = start - (start - end) / / 2
stars_till_mid = 0
if (mid > n):
l_stars = 2 * n - 1 - mid
till_half = (n * (n + 1 )) / 2
stars_till_mid = till_half + \
((n - 1 ) * (n)) / 2 - ((l_stars) * (l_stars + 1 )) / 2
else :
stars_till_mid = mid * (mid + 1 ) / 2
if (k < = stars_till_mid):
ans = mid
end = mid - 1
else :
start = mid + 1
return ans
if __name__ = = "__main__" :
N = 3
K = 8
print (count_rows(N, K))
|
C#
using System;
class GFG{
static int count_rows( int n, int k)
{
int start = 1, end = 2 * n - 1, ans = 0;
while (start <= end) {
int mid = start - (start - end) / 2;
int stars_till_mid = 0;
if (mid > n) {
int l_stars = 2 * n - 1 - mid;
int till_half = (n * (n + 1)) / 2;
stars_till_mid
= till_half + ((n - 1) * (n)) / 2
- ((l_stars) * (l_stars + 1)) / 2;
}
else {
stars_till_mid = mid * (mid + 1) / 2;
}
if (k <= stars_till_mid) {
ans = mid;
end = mid - 1;
}
else
start = mid + 1;
}
return ans;
}
public static void Main()
{
int N = 3, K = 8;
Console.Write(count_rows(N, K));
}
}
|
Javascript
<script>
const count_rows = (n, k) => {
let start = 1, end = 2 * n - 1, ans = 0;
while (start <= end)
{
let mid = start - parseInt((start - end) / 2);
let stars_till_mid = 0;
if (mid > n)
{
let l_stars = 2 * n - 1 - mid;
let till_half = (n * (n + 1)) / 2;
stars_till_mid = till_half + ((n - 1) * (n)) / 2 -
((l_stars) * (l_stars + 1)) / 2;
}
else
{
stars_till_mid = mid * (mid + 1) / 2;
}
if (k <= stars_till_mid)
{
ans = mid;
end = mid - 1;
}
else
start = mid + 1;
}
return ans;
}
let N = 3, K = 8;
document.write(count_rows(N, K));
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...